Ladybug Shield Alpha V2 – Testing the pH

Tags

,

Spoiler alert…wait for it…the pH circuit works!  Talk about exciting.  I truly enjoy learning all the skills and different disciplines that goes into building a working pH sensor…math, science, electronics, designing, laying out, and building a PCB, writing the software,..and most importantly – the people I learn from along the way.

Ladybug Shield V2

Here it is – a thing of beauty – a fully populated Ladybug Shield Alpha V2 hooked into an Arduino:

IMG 3326

 

I got to use my Zallus controlled reflow oven.  I really like the Zallus (toaster) oven controller.  Heck, I really like the idea of popping a PCB in a toaster oven after carefully placing soldering past and SMT parts.  It is so Easy Bake Oven…I can’t help but smile.

The Goal

The goal of this post is to compare the results of pH circuit tests of the Ladybug Shield with those from an Atlas-Scientific EZO pH stamp.

Thanks to Those That Went Before

Thanks to Chris Gammell and his Contextual Electronics courses I was able to get to this point where I have pretty much finished the firmware and hardware design for the pH portion of the Ladybug shield.

Thanks to Ryan @SparkysWidgets for embracing/helping us followers of his minipH open source product and project.  I find it amazing to learn so much from all that he has shared.  My hope is this effort is a positive contribution to the minipH effort.

Open Source

I will be discussing the contents of the Arduino sketches available at this GitHub location.

Calibrate

pH probes lose their vitality over time and eventually need to be replaced.  In the mean time, the slope of the line mapping pH mV to pH value needs to be calibrated.

IdealpHProbeOutput

I use 2 points to determine the slope of the pH mV/pH value line.  The two points come from reading the pH when the probe is in pH 4 and pH 7 calibrated solutions.  The step between pH values for an ideal probe is 59.16mV.

The calibration sketch I used is located at this GitHub location.

When the probe was in pH 4 calibration solution, the reading was 155mV averaged over 11,551 samples with a standard deviation of .49.  When the probe was in pH 7, the reading was -29mV averaged over 3,859 samples with a standard deviation of .40.  

I felt the variability between readings to be smaller than expected.  I don’t have a sound reasoning other than I expected more noise between samples. Maybe < 1standard deviation is what is expected.  The results were expected.

The pH slope = y/x = (155 – -29)/(7-4) = 61.33.

Now when pH values are calculated, a step value of 61.33mV will be used instead of 59.16mV.

Read pH

See the calcpH() function in the Arduino sketch.

(note: I evolved this part from Ryan – SparkysWidgets – minipH.ino.  Thanks Ryan!).   Converting the mV value from the ADC into a pH value involves two steps:

  • compensate for the offset of the pH 7 mV reading of the probe from the ideal of 0 mV
  • figure out how many steps away the mV reading is from pH 7.
Since figuring out how many steps away the mV value is from pH 7 assumes the ideal pH 7 = 0mV, start with adjusting the mV value to compensate for the pH 7 mV value.  From calibration, the mV value for pH 7 for the probe I used was -29mV.
 
When I put the probe into pH 4 calibration solution – this time to read the pH and not calibrate – the mV reading from the ADC was 153mV.  Given the offset, the mV value when pH 7 mV = 0 is 153 – -29 = 153 + 29 = 182.  Calibration provided the probe’s step value at 61.33mV.  The pH value of a 153mV reading given this probe = 7 – 182/61.33 = 4.03.  Wow!  That’s a darn good measurement.

Compare with Atlas-Scientific EZO pH Stamp

 I had an older Atlas-Scientific pH Stamp so I purchased the EZO pH Stamp.  I have to say I am very impressed with it.  With the older pH stamp I had to send a command several times to make sure it pH Stamp reacted.  This was not a challenge with the EZO ph Stamp.  Unpacking the EZO and first use is a pleasurable experience that requires no additional electronic knowledge.  In short, if you want a “high” quality pH sensor that works with an Arduino, the EZO is worth a look.

I compared the readings from the EZO pH Stamp with the Ladybug Shield for two solutions:

  • White distilled vinegar
  • Baking soda added to filtered water
  EZO Ladybug % difference
Vinegar 2.56 2.67 4.21
Baking Soda 7.94 7.85 1.14
 
The difference in readings is acceptable.

What’s Next

Work on the pH measurement circuit and firmware is almost done.  I have learned a lot as I traveled this journey.  From the science and math behind a pH measurement to the electronics used to detect a voltage that can then be interpreted in a pH value.  I got a lot of help from folks like Chris Gammell and Ryan @SparkysWidgets.

I haven’t added temperature adjustment.  I’ll do this in an upcoming post.

 

Thanks for reading this far.  Please find many things to smile about.

 

A Lettuce LED System – Part 2: The Constant Current Source

Tags

In my previous setup I used an LED driver, the LDD-1000H to provide 1A constant current to a series of LEDs.  Armed with knowledge gained from the latest Contextual Electronics course, I have the confidence to build a constant current source to up to 1A to the red and blue LEDs I discussed in the previous post, 

The Goal

The goal of this post is to design a circuit to provide a constant 1A current to the red and blue LEDs.

Thanks to Those That Went Before

Thanks to Chris Gammell for his Contextual Electronics course and mentoring.  Chris walked us through designing a current sink which was easy for me to see how I could modify the design to act as a current source for the two LEDs used in this project.  It was an achievement for me to take his design and evolve it into an LED current driver.

I found these two videos:

gave me a better understanding of how to think about heat transfer when watts of power need to be dissipated.

Open Source

The LTSpice IV circuit model that is discussed below is located at this GitHub location (LEDCurrentDriver.asc).

The Kicad schematic is located at this GitHub location.

Constant Current

While it is fine to use a resistor in order not to burn out one of these type of LEDs:

smallLEDs

more powerful LEDs – like the ones used to grow plants – require a constant current.  Up until now, I have been relying on the LDD-1000H LED driver (data sheet):

LDD-1000H

Simple to use, it can drive up to 16 3W LEDs and has a PWM dimming.  However, the challenge with the dimming is it is all or nothing. Either all the LEDs are dimmed or none are.

Circuit Design

It seemed to me the power supply tester circuit we are designing in Contextual Electronics is pretty close to what is needed to provide a constant current to the royal blue and red LEDs. I discussed the project in an earlier post.

Chris has us early on in a design using LTSpice as a way to model a circuit.  Here is the model circuit I came up with in LTSpice:

 

ConstantCurrentForRedBlueLED

I kept the design of the power tester Chris had us working on – the beautifully simple current sink that feeds a  “programmable voltage” into the non-inverting input of an op amp.  Because the op amp is configured as a voltage follower and Vin+ = Vin-, the output voltage controls how much current the MOSFET lets flow through its gate.  Given a known resistor – R1 in the image – and a known programmable voltage – V2 in the image – the current can be adjusted to a range of constant current values. 

Voltage Drop

Red LED

Here is a graph of the voltage drop across the XP-E2 red LED:

voltageDropAcrossRedLED

The first thing it points out is how much more efficient red LEDs are to the other colors.  The voltage drop at 1A is ~ 2.65V.

Royal Blue LED

The XT-E2’s voltage drop for the royal blue:

RoyalBlueVoltageDrop

shows the voltage drop at 1A to be ~3.25V.

Resistor

When the current is at 1A, the voltage drop is tiny .1V.

MOSFET

The voltage drop across the MOSFET is what is left over.  Assuming 9V Wall Wart, the voltage drop across the MOSFET is 3V (9-3.25-2.65-.1)

Schematic

The schematic differs from the LTSpice IV model by adding a voltage regulator and a POT:

LED Schematic

Varying the Constant Current

The amount of constant current is controlled by adjusting the voltage into the non-inverting op amp.  The load resistor = .1Ω.  When the current = 1A, the voltage = .1V (V=IR).  I’ll use a potentiometer (POT) to adjust Vin+ to the op amp (R2 in the diagram):

Pasted Image 2 21 15 2 44 AM

as the bottom leg of a voltage divider to supply 0 to .1V to the non-inverting op amp.  The top of the voltage divider (Vin) is 5V.  I want Vout to range from 0 to .1 V. The ratio (Vout/Vin) is 1/50.  I already have a POT that goes from 0 – 5K that I’d like to use.  Vout = Vin(R2/R1+R2) -> .1 =  -> 1/50 = R2/R2+R1 -> 50R2 = R2 +R1 R1 = 49R2 => R2 = 5K, R1 = 49*5000 = 245K.  Checking the math:  .1V = 5(5K/245K+5K) -> .1V = 5(5/250) = 5/50 = 1/10 = .1V.

In order for R2 to go from 0 – 5K, R1 = 245KΩ.

Picking Parts

MOSFET

In our Contextual Electronics course, Chris walked us through finding an N-Channel MOSFET based on:

  • Price
  • Package – TO-220 DPAK – very common for power MOSFETS.
  • SOA (Safe Operating Area) – the maximum power the MOSFET can dissipate.
  • RDS(ON) – the resistance from drain to source when enough voltage is applied to the MOSFET’s gate such that current is fully flowing.  Note: While knowing the RDS(ON) is useful, I didn’t end up using it directly since the value is taken into account by the SOA graph.
Characteristics:
  • a VGS(th) of no more than 5V to allow 1A of current to flow.  This means a Logic Level MOSFET.  There is range from where a MOSFET starts turning on the current to when the current is fully flowing.
DigiKey Filters:
  • N-Channel MOSFET
  • buy at quantities of 1
  • Logic Level Gate
  • all the TO-220 packages
Sorting by price, the first one in the list from NXP Semiconductors (dd) can handle 41W.  The data sheet includes the SOA graph:
Pasted Image 2 24 15 4 33 AM
 
This MOSFET will be fine for the continuous 1A of drain current with a voltage drop of 3V (3W) 
 
  • Power >= 6V

Heat Sink

I found an inexpensive TO-220 heat sink on digikey To see if this heat sink would work, I followed the formula Bil Herd went over in his video about heat sinks:

BilHerdHeatSinkFormula

I’ll start with the same ambient temperature (38˚C).  Next add the thermal resistance * 3W (amount of power needed to be dissipated).  The listing on digikey shows the thermal resistance to be 24˚C/W.  3 * 24 = 72˚C.  I’m skipping the thermal resistance of case to sink.  The junction to case of the NXP MOSFET (data sheet).  Shows a typical JC thermal resistance to be 3.1˚C/W (note: While 0˚C = 273.15˚K, 1C up/down = 1K up/down so 3K = 3C heat up for each watt).  Rounding to 3˚C/W, 3*3 = 9˚C.  Adding this up:

38+72+9=119˚C.  The data sheet states the maximum rating for this MOSFET is 175˚C.  This heat sink should work.

Resistor

Looking back at the LTSpice IV simulation, I = V/R, I = .1/.1 = 1.  The resistor’s value is .1Ω.  The amount of power burned is P = IV = 1*.1 = .1W.  We need a resistor that is .1Ω and can withstand burning at least .1W.  .1W is not that high so I am choosing an inexpensive SMT 0805 package resistor (digikey link).  I like to use 0805 sized caps and resistors.

Op Amp

 I will use an MCP6241 for the op amp because I have some in stock.  According to the data sheet, the positive voltage rail can go up to 5.5V.  Other characteristics are not that important.

Voltage Regulator

I need a voltage regulator that takes 9V as input and outputs 6V.  The voltage regulator I use in the Ladybug shield should work fine.  I use a 7805 sot-89 packaged voltage regulator (data sheet).

What’s Next

I’m off to order parts so that I can build a prototype.  Thanks for reading this far.  Please find many things to smile about.

 

A Lettuce LED Light System – part 1: the LEDs

Tags

,

I showed my current hydro lettuce setup in a previous post. I want to explore setting up the optimal LED setup for growing just one leafy plant.  My current setup can hold up to 9 plants.  Most setups I have seen wish to accommodate many more than 9.  I’m interested in starting with one and adding as needs expand.  Is there an effective and efficient way to build an LED setup for leafy plants in which it starts with one and then incrementally adds on more?

In this post I explore part 1 of building an LED setup for one lettuce plant. – the LEDs.  Once I am able to test it out, I will consider a future effort that connects single LED systems together to support more lettuce plants.

The Goal

The goal of this post is to review the circuit design of a single LED setup for growing lettuce.

Let There Be Light

Blue And Red

There are several lighting technologies that I could choose from.  I choose LEDs because they are effective, efficient, becoming more and more affordable, and researched.  My current design uses a white and red LED.  For this LED setup I will use blue and red LED lights.  Blue and red lighting is known to be effective for enhancing photosynthesis.

The photosynthesis process of leafy plants respond to blue and red light at the following wavelengths:

RedAndBlueWavelengths

My LED choice will be based on the wavelengths:

  • Blue: around 430-455nm range
  • Red: around 635-640nm range
I will use LEDs that are close to these wavelengths if they are good quality and easy to obtain. 

Manufacturer And Part

I chose Cree LEDs because I wanted to start with “the best” to avoid variances in results because of lower quality.

Red

For red I will be using what I already have, the XP-E2 3w red LED (data sheet).  Since the XP-E2 comes in a variety of wavelengths, the image below shows the red along with the other LED offerings:

Red Wavelength
The red wavelength is slightly lower than idea at a little more than 630nm, but close enough.

Blue

I will be using the royal blue XT-E2 LED:
RoyalBlueWavelangth
The royal blue wavelength seems to be within the ideal range.

Distributor

I chose to buy in quantities of 6 from LEDGroupSupply.com (links red and royal blue).  I have been happy with previous purchases.  While I haven’t done much research at this point on sourcing, I assume the prices paid are at the high end.  Since this is testing I am opting for time, quality, and piece of mind.

 
That’s it for now.  Thanks for reading this far.  Please find many things to smile about. 
 

Is it OK to Leave a pH Probe in the Nutrient Bath?

Tags

I was blessed to get some great questions as a result of the post I made about my latest hydro lettuce station.  I am learning as I try things out and through getting great questions and feedback like the one from gotbock.

gotbock’s question::

post replyMy Hydro setup

 

 

from gotbock via /r/hydro/ sent 5 hours ago

Do you know if you’re pH and EC probes are designed to be held in your nutrient solution for long time periods? Most meters I’ve used require at least the pH probe to be stored under storage solution (usually saturated KCl) when not in use to maintain the probe. Otherwise they will go out of calibration pretty fast.

________________________________________________________________________________________________________

After researching, I came across this article which helped me better understand the why of gotbock’s point and what I would do differently given the probe stays in a nutrient solution.

ionsonglassmembranepH

The article notes that if the glass membrane is kept in a solution that has plenty of sodium and potassium ions (which a nutrient solution would), these ions will penetrate the membrane along with the hydrogen ions. Now instead of measuring H+, the probe provides measurements of Na+ and K+…so the pH readings are off as you point out. The ions that are a key part of the nutrients will adhere to the glass and off go the readings.

As gotbock notes, the best way to prevent this is to clean off the glass membrane with distilled water and then store in a storage solution.

I have not done enough testing to understand the practical challenge (how inaccurate) the readings will be. Especially since I am monitoring for a range of readings. For example, I’m growing lettuce so I want the pH value to be between 5.5 and 6.5.

I would agree calibration would have to be done more often. I don’t know how much more often. Once I automate this process with the Ladybug Shield, I can choose to calibrate daily/weekly, etc. with the goal of minimizing this challenge.

 

Learnings from Contextual Electronics: Testing a Power Source

Tags

Contextual Electronics has started up and I’m already behind.  Chris decided to get our learning juices flowing by starting with a power source tester.  This is terrific for me since I have been assuming available power to “just work.”  

I find it is very hard for me to follow along with someone’s circuit design unless I have a pretty good grasp of what led to the thought process. It is typical for me to totally miss the point – or valuable insights – at the beginning of a project.  I understand best if I am able to treat each component in the circuit as a character in a story.  The challenge is to understand the motivations and role the character/component plays. This post is my attempt to provide my learning techniques in order to clarify any misconceptions I have regarding the power source tester circuit Chris is helping us build.

My focus has been on measuring voltage.  After all, measuring small changes in voltage is what provides insight into reactions happening in the environment.  In the case of the Ladybug Shield, measuring the change in voltage is how to figure out the pH and/or EC of a nutrient bath – necessary for the healthy growth of hydroponically grown plants. 

The need to characterize a power supply becomes clear when using batteries.  If the Ladybug shield was powered by batteries, how long can it run before the batteries need to be replaced?  How efficiently does the Ladybug shield use the battery?

To answer these questions, Chris is having us build a power supply tester.  Dave Jones’ EEVBlog #102 describes this circuit:

DaveJonesConstantCurrentLoad

I can see the brilliant simplicity of this design (after :-)) Dave and Chris explain it.  A POT is hooked up to a +5V voltage source.  This is fed into Vin+ of an op amp.  The “golden rule” of op amps always applies – the op amp will do whatever it can to get Vin- to = Vin+.  Now the excitement begins.  Set up the the op amp as a voltage follower.  If 1V goes into Vin+, 1V will follow back into the op amp through Vin-.  The MOSFET controls the flow (current) since a MOSFET’s output current is controlled by the voltage at the input.  By inserting a 1Ω resistor, a constant current sink serves to flush the energy out of the power source (I noted a battery in the picture but this could be any voltage source like a DC-DC converter).

Dave has a plot that I found extremely helpful:

DaveEEVBlogPlot

The Y-Axis is the amount of voltage applied to the MOSFET’s gate.  The gate stays shut until the voltage is at least 1.5V. As the voltage is increased on the gate, the MOSFET starts to let current through.  Since the current sink resistor is 1Ω and V=IR where R=1, V = I – so the X-Axis can be interpreted as the amount of constant current at a given Vin+ provided by the dummy load.

Now the question of what does a given load do on the capacity or other characteristics of a power source?  Can be measured.

OK, so all of this was obvious to you.  It wasn’t to me.  That’s one of the things I enjoy about Chris’s style.  These are useful circuits that use common components in ways that bring out the key characteristics of the components.  The circuits tell a story with characters – whose characteristics like the op amp’s golden rules or the MOSFET’s ability to act like a variable resistor to current flow – are pivotal in telling the story.

On to catching up with more of the design and what parts to use!


Thank you for reading this far.  Please find many things to smile about.

Latest Hydro Setup for Growing Lettuce

Tags

,

Update: Growth after three weeks

IMG 3310

Here is a short video on my latest hydro setup.  Right now I’m growing six plants.  The plants were started from seed a little less than three weeks ago.

 

 

I will replace measuring pH and EC as well as adding nutrients once I get the Ladybug Shield ready for the task.

 

Let me know what I could be doing better.

 

 

That’s it for now.  Please find many things to smile about!

Ladybug Shield Alpha V2 – Off to OshPark

Tags

,

Simpson DOHI came to the conclusion in a previous post that the unexpected behavior I was observing while testing the EC rectifying circuit was due to a slight voltage surge that occurs when the circuit for the multiplexer was chosen.

D’OH Wrong.  

The capacitor was not being discharged.  There was no surge.  I wasn’t using the FET correctly to discharge the capacitor prior to taking a Vin+ or ECVout measurement.  This seems like such a basic concept.  In fact, it alarms me that I didn’t reason this out…

Thanks To Those That Went Before

I constantly find myself mentally pounding my head on a table at how little I comprehend about electronics.  These on going investigations continue to shine a light on concepts I thought I understand but in reality were not grasping.  I have been blessed to get guidance and a boost of confidence from Chris Gammell – both through his Contextual Electronics courses and mentoring.

Open Source

  • Updated kicad files for the Ladybug Shield Alpha V2 that I sent to OshPark the other day are located at this GitHub repository.
  • Arduino sketch used to test the circuit available here.

The Circuit

Here is an image of the rectifying circuit:

IMG 3274

the negative feedback of the 1st op amp is not tied to VGND.  This is a visual representation to remind me the op amps, capacitor, and gate are relative to VGND.

And an image of the test circuit:

IMG 3294

Tests

Analog

I am evolving my skills at testing circuits.  The key – and perhaps obvious – thing I was not doing was evaluating the input and output at the same time.  This is common practice with software.  Is the input unexpected, why?  If the input is expected and the output is unexpected, why?  Can the input and output be further isolated to an interaction/result somewhere closer to the input?  

I was analyzing the input and next analyzing the output using one probe.  This works well when analyzing numbers, but not so well when analyzing analog circuits.  Much better is to overlay the input and the output.  As shown in the image above, two probes is and will be a necessity when debugging analog circuits.  Oh – right – two probes are on my scope for a reason :-).

The result?  Repeatable observance of a higher voltage and s l o w discharge at the node where the capacitor is located.

Digital

I wrote an Arduino sketch that calculates the digital value for Vin+ and ECVout.  Recall Vin+ Vpp ~= 500mV.  ECVout is the gain using a 200Ω resistor to simulate an EC probe value.

I use three i/o pins on the Arduino:

  • enable to multiplexer
  • tell the multiplexer to switch between sending in the Vin+ and ECVout waveforms
  • turn on the FET’s gate

The Arduino pin that I was using incorrectly was the sequence of turning on/off the FET so the capacitor is discharged.  What I now do is a discharge of the capacitor before the first reading and immediately after subsequent readings.  Discharging the capacitor means turning on the FET  (gate = HIGH), then immediately turn off (gate=LOW).

The Arduino Sketch

The sketch I used is located at this GitHub location.  Here is a sample result in a sea of runs I made:

| Count: 1000** Vin+ ** Average: 251.22| Std deviation: 2.12

| Count: 1000**  ECVout ** Average: 1576.48|  Std deviation: 2.04

Gain = ECVout/Vin+ = 1576.48/251.22 = 6.28 = 1 + 1K/R(EC)

Where 1K is the feedback resistor of the second op amp (R17 in the Ladybug Shield Alpha V2 schematic).

The simulated resistance (R(EC) ) = 1K/(G-1) = 1000/5.28 = 189Ω.

“close enough” to 200Ω.

Here is the sketch I used:

#include <Statistic.h>
#include <Wire.h>
#include <Adafruit_ADS1015.h>

Adafruit_ADS1015 ads1015;
const byte FET_pin = 8;
const byte mux_out_pin = 7;
const byte mux_enable_pin = 6;
const byte LSB_multiplier = 1;// 2x gain +/- 2.048V 1 bit = 1mV
Statistic myStats_Vin; //from http://playground.arduino.cc/Main/Statistics
Statistic myStats_ECv;
void setup(void)
{
Serial.begin(9600);
pinMode(FET_pin,OUTPUT);
pinMode(mux_out_pin,OUTPUT);
pinMode(mux_enable_pin,OUTPUT);
digitalWrite(FET_pin,LOW); //start with FET as open circuit
digitalWrite(mux_enable_pin,HIGH); //disable multiplexer

ads1015.begin();
//Vpp a bit over 2V
// ads1015.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV
ads1015.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV
// ads1015.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV
// ads1015.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit = 0.25mV
// ads1015.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV
showHelp();
}
void loop()
{
// Handle serial commands
serialHandler();
}
void serialHandler() {
char inChar;
if((inChar = Serial.read())>0) {
switch (inChar) {
case ‘i': // take ECVin+ reading
case ‘I':
readECVin();
showHelp();
break;
case ‘o': // take ECVout reading
case ‘O':
readECVout();
showHelp();
break;
case ‘?':
case ‘h': // Display help
showHelp();
break;
default:
break;
}
}
}
#define VIN 1
#define ECV 2

void readECVin()
{
Serial.println(“readECVin”);
myStats_Vin.clear(); //explicitly start clean
Serial.println(F(“Enter number readings: “));
while (Serial.available () == 0) {
;
}
unsigned int num_readings = Serial.parseInt();
Serial.println(num_readings);
int vIn_value = readVIn(num_readings);
Serial.print(F(“| Count: “));
Serial.print(myStats_Vin.count());
Serial.print(F(“** Vin+ ** Average: “));
Serial.print(myStats_Vin.average());
Serial.print(F(“| Std deviation: “));
Serial.println(myStats_Vin.pop_stdev());
}
void readECVout()
{
Serial.println(“readECVout”);
myStats_ECv.clear(); //explicitly start clean
Serial.println(F(“Enter number readings: “));
while (Serial.available () == 0) {
;
}
unsigned int num_readings = Serial.parseInt();
Serial.println(num_readings);
int vIn_value = readECv(num_readings);
Serial.print(F(“| Count: “));
Serial.print(myStats_ECv.count());
Serial.print(F(“** ECVout ** Average: “));
Serial.print(myStats_ECv.average());
Serial.print(F(“| Std deviation: “));
Serial.println(myStats_ECv.pop_stdev());

}
int readVIn(unsigned int num_readings)
{
digitalWrite(FET_pin,LOW); //stop draining
digitalWrite(mux_enable_pin,LOW); //enable multiplexer
switchTo(VIN);
for (int i=0;i<num_readings;i++) {
int results = readADC();
myStats_Vin.add(results);
}
digitalWrite(mux_enable_pin,HIGH); //disable multiplexer
// digitalWrite(FET_pin,HIGH); //start draining
return myStats_Vin.average();
}
int readECv(unsigned int num_readings)
{
digitalWrite(FET_pin,LOW); //stop draining
digitalWrite(mux_enable_pin,LOW); //enable multiplexer
switchTo(ECV);
for (int i=0;i<num_readings;i++) {
int results = readADC();
myStats_ECv.add(results);
}
digitalWrite(mux_enable_pin,HIGH); //disable multiplexer
// digitalWrite(FET_pin,HIGH); //start draining
return myStats_ECv.average();
}
int adcReading(unsigned int num_readings)
{
discharge(); //discharge capacitor before taking readings
for (int i=0;i<num_readings;i++) {
int results = readADC();
discharge(); //discharge after a reading
myStats_Vin.add(results);
}
return myStats_Vin.average();
}

void switchTo(const byte waveform)
{
Serial.println(“Switch”);
if (waveform == VIN) {
Serial.println(“Reading Vin+”);
digitalWrite(mux_out_pin,LOW); //ECVin+ when pin is low.
}
else {
Serial.println(“Reading ECv”);
digitalWrite(mux_out_pin,HIGH); //ECVout when pin is high.
}
Serial.println(“Settle…”);
delay(1000);
}
void discharge()
{
digitalWrite(FET_pin,HIGH);
//delay(5000);
digitalWrite(FET_pin,LOW);
}
int16_t readADC()
{
int16_t results = ads1015.readADC_Differential_VGND(0)*LSB_multiplier;
Serial.println(results);
return results;
}
const char helpText[] PROGMEM =
“\n”
“Available commands:” “\n”
” ? – shows available comands” “\n”
” i – take ECVin+ reading” “\n”
” o – take ECVout reading” “\n”
;
/*———————————————————–
show command line menu
———————————————————–*/
static void showHelp () {
showString(helpText);
}
static void showString (PGM_P s) {
for (;;) {
char c = pgm_read_byte(s++);
if (c == 0)
break;
if (c == ‘\n’)
Serial.print(‘\r’);
Serial.print(c);
}
}

The End

Well, while I would have hoped I would have figured this out a while ago, the good news is persistence pays off. Hopefully in the future I will be able to better reason and debug analog issues. Although much continues to illude me.

Thanks for reading this far. Please find many things to smile about.

The EC Circuit – Putting Back the Voltage Follower Op Amp in the Rectifier Circuit

Tags

,

In a previous post, I decided to remove the buffer (voltage follower) op amp within the EC’s rectifier circuit.  I did this because I was too lazy to observe what caused a surge in voltage readings when measuring the lower ECVin+ (the “shrunken waveform” that is the voltage input to the EC Gain loop – the protagonist of the EC Circuit). After reviewing with Chris, he did the right thing and “suggested” I stop being lazy.  From a circuit design perspective, keeping the buffer op amp will keep surges coming from the capacitor to mix in with the DC voltage going into the ADC.  This makes sense because the waveform going into an op amp is met with a high impedance while the waveform going out has a low impedance.

The Goal

The goal of this post is to characterize (even better understand!) the voltage surge that occurs when the multiplexer switches what is being measured by the EC rectifier circuit from ECVin+ to ECVout.

Discussion

When I ran an Arduino sketch that measured the value for ECVin+, the majority of the time the readings would indicate a surge in voltage that eventually settled down.  This was most obvious when measuring ECVout (a waveform 6 x that of ECVin+) first. 

Observations

Scope Probe on Vout of Voltage Follower Op Amp

The video below shows the results on my scope on the ADC AIN0 pin (relative to GND):

  • (6 seconds into the video) take 100 samples of ECVout first.  Here is the results the Arduino read:
| Count: 100** ECVout ** Average: 1585.37mV| Std deviation: 0.58
  • (15 seconds into the video) take 5000 samples of ECVin+ after taking the ECVout samples.  The readings started at 1353mV then gradually went down to 249mV.  249mV is the expected value.
 
  • When starting to sample the ADC from my Arduino sketch, the voltage readings for ECVin+ (the shrunken waveform in the EC Circuit) are higher than the DC value should be.  To settle down the DCVin+ DC voltage value, it is best to sample 5,000 times before taking a sample to measure.
  • ECVout measurements do not have an initial voltage surge.

Scope Probe on Vin From Multiplexer

If the multiplexer causes the voltage surge, I expected to observe changes in the waveforms coming into the rectifying op amps.  To observe this, I turn on AC Coupling on the Scope channel.  By doing so, DC biasing will be eliminated.  Since I am still learning – I’ll pause and give my interpretation of how the Scope does DC biasing and what that means.  The scope puts a capacitor at the beginning of the probe circuit. By doing this – since i = C(dv/dt) and DC’s frequency is close to 0, i = 0 for the DC current.  Only the AC current will be measured.  (ok, I’ll look back at this explanation in a year and cringe….always practicing…always learning…).

In this video, the scope’s probe is on the EC rectifier’s input pin (i.e.: the output of the multiplexer) relative to GND.

Next Steps Based On Observations

Given my current understanding of circuit analysis, I’m concluding that the above observations point out to a slight voltage surge that occurs when the circuit for the multiplexer is chosen and the waveform starts flowing through the EC rectifier’s op amps.

The residual of this surge in voltage finds its way into the ADC readings.  To not include this artifact in ADC readings, at least 5000 samples need to be taken prior to taking samples that will be used in measurements.  By throwing out the initial 5,000 samples, artifacts of the voltage surge caused by switching inputs into the EC rectifier are eliminated.

As far as the hardware part of the circuit, I’m putting the voltage follower op amp back into the EC circuit.  Here is the current design (Dare I dream that I can move forward to another Osh Park layout?):

IMG 3274

 

 

Thanks for reading this far.  Please find many things to smile about.

 

Visualizing a Low Pass Filter

Tags

I’ve been using low pass RC filters in the design of the Ladybug Shield.  Up until now, I haven’t had an intuitive feel for what is going on.  Rather I relied on the terminology and topology.  A low pass filter lets waveforms of low frequency through but blocks high frequency waveforms.  It consists of a resistor and capacitor in series.  Perhaps that is all I should know.  Then I started taking the edX course from Berkeley – EE40LX Electronic Interfaces.  They did a great job visualizing a low pass filter.  So great it got me exploring visualizing a bit more.

The Goal

The goal of this post is to solidify why a resistor and capacitor in series acts as a low pass filter

Thanks to Those That Went Before

  • Chris Gammell for covering these topics within his excellent Contextual Electronics courses.  I am thoroughly enjoying the new courses going on now!
  • edX for the EE40LX course.
  • Paul and his excellent DVD downstream tutorials on circuit analysis.

How a Low Pass Filter works

When I learn something in electronics, I like to absorb:

  • The rule of thumb.  I stated this above for the low pass filter.   A low pass filter (RC in series) lets waveforms of low frequency through but blocks high frequency waveforms.  It consists of a resistor and capacitor in series.  If all I ever wanted to do was to sketch together a circuit, I’d stop here…but I don’t really understand why this is true.
  • A take-it-slow sketchplanation of the math and physics.
  • A simulation or prototype that lets me visualize, explore, think about the concept.

Take it Slow Sketchplanation 

This image from an edX video

 

edXLowPassFilter

  • has a clear way of explaining why the low pass filter works.  Since the current through a capacitor is proportional to the change in voltage over time (i = C(dV/dT) ), as the frequency (w in the image) of a waveform goes to 0 – which is the case for a DC circuit – the change in voltage goes to 0, so i = 0.  If there is no current, there is an open circuit.  When the frequency gets to be very high, the change in voltage/time gets higher, i keeps growing and eventually the current rushes to ground – a short circuit.
  • assumes you have prior knowledge of the math/physics behind capacitors.  Stuff like the formula for v(t) and i = C(dv/dt).  The edX course covers this.  I found the DVD downstream of these tutorials on circuit analysis to help me better understand.  There are great books – like the Art of Electronics as well as “Google is your friend.”

So the key to understanding the RC filter from an intuitive math/physics perspective is getting a firm grasp that the current through a capacitor is proportional to the slope of the change in voltage/change in time.  

Low Frequency, Approaching a DC Circuit

As the change in voltage approaches 0 (basically DC), the current through the capacitor approaches 0, the capacitor’s symbol is in full force – an open circuit.  Since there is no load in an open circuit and voltage always has potential, the voltage drop across the capacitor = V(t).  

High Frequency

The higher the waveform’s frequency, the stronger the current, to the point where the current flows through like a short circuit.  In the case of a short circuit, there is no resistance and so no voltage drop across the capacitor.  So high frequency waveforms get filtered out.

Visualize Through Simulation

I just switched over to the Mac version of LTSpice IV.  Once I bumbled through the differences in UI, I prefer the Mac UI to the Windows running under Parallels UI since it doesn’t look like a bitmap image and actions like zoom in/out use the Mac “way” instead of the Windows “way”.  

Here is an image of the simulation I ran:

LTSpiceIVLowPassSimulation

I’m asking the simulation to make two runs.  The first run has a 10Hz Sine wave with an amplitude of 1V.  The second run uses a 1KHz Sine wave for input.  Here are the results.  The blue lines are input, black output:

10Hz1KHzLowPass

When the frequency is 10Hz, the Vout is close to Vin.  When the frequency of Vin is increased to 1K, Vout is close to 0.

Ah yes.  The simulation was very helpful in grasping the basic concepts behind a low pass filter.  Because i = C(dv/dt), when there is little change in voltage, there is little current.  The less current, the more Vout = Vin.  The reverse of course is also true.  The more change in voltage, the higher the current.  The higher the current, the more Vout = short circuit = 0.

 

You probably already had a firm grasp.  I am still gasping for air in the deep end of the electronics pool.  These sort of step backs and HUHs?  Help me better understand core electronic concepts.

 

Thanks for reading this far.  Please find many things to smile about.

 

Back to the EC Circuit – Evaluating the Gain Loop

In this post I take a step back and write down what the goal of the EC Circuit is and how the Ladybug Shield’s EC circuit implements to these goals.  After doing this, I further analyze the circuit from the perspective of the scenario in which the Ladybug Shield will be used.  I then go on to test the circuit using a breadboard populated with the same parts that will be used on the PCB.  The testing will lead to design changes in the EC Circuit.  Back to the EC Gain loop….

Here’s an image of the all important gain loop in the EC circuit:

EC_SignalDesignLadybugShieldAlpha1

EC Gain Loop

 

To find the EC of a nutrient bath using an EC probe:

  • Find the gain (Vout/Vin)
  • Solve for the probe’s resistor value (ReC)
  • Take the reciprocal (1/ReC) and that’s the EC in Siemens

Thanks to Those That Went Before

I will always be thankful to Chris Gammel.  His Contextual Electronics courses and strong mentorship have opened up my abilities in practicing electronics.  Chris has a terrific ability to engage his students in building circuits and in the process learning quite a bit about electronics.

I learned a lot from Ryan’s – SparkysWidgets – open source minipH and miniEC BoBs for measuring the pH and EC.  On top of the availability of the schematics, Ryan has been exceptionally supportive – answering questions when I am stumped, and providing feedback.

Open Source

The kicad schematics and Arduino Sketches can be found at this GitHub location.

Find the Gain

Gain = Vout/Vin+.  The design already figures out Vout by sending EC_SIGNAL through an ADC.  Vin+ has been assumed to be 100mV.  Problems with this assumption:

  • the 200mV Vpp was sourced from the design of others, not based on acquired knowledge on my part.  Perhaps a larger Vpp is possible.  If so, the final result will be more accurate.
  •  not using a measured value means the Gain will most likely be off.  Since this is a gain, the error is magnified.

A Different Vpp for Vin+

The current design sets the Vpp of Vin+ to +/- 200mV.  Now that I better understand how the EC circuit works, is 200mV the “best” Vpp given the design requirements?

I am designing the Ladybug shield to help grow herbs and vegetables.  From the information in this post on EC values for vegetables and herbs, I will restrain the EC Gain loop to determine EC values between  .5mS to 5mS = .0005S to .005 S.  S = 1/R.  So resistance = 1/S.  The range for the variable resistor in the gain loop representing the EC probe that this circuit must support goes from 2KΩ (1/.0005) to 200Ω (1/.005).

When the resistance is 2KΩ, the Gain = 1 + R10/2K = 1 + 1K/2K = 1.5.  When the resistance is 200Ω, the Gain = 1 + 1K/200 = 6.  To be safe, a maximum amplification of 7 will be used.  The op amp’s rails provide 5V of headroom for the Vpp of the incoming waveform.  The maximum Vpp of Vin+ – 5V/7 = 714mV.  To be a bit more conservative, I will use a Vpp for the shrunken waveform of 500mV (instead of the 200mV I have used up to this point).  The EC readings should be improved with a larger Vin+.

Vin+ will be amplified between between 1.5 and 7 times.  Let’s say the reading is off by 100mV.  At 1.5 times this is 150mV.  At 7 times this is 700mV.  Given the potential for a wide variance – even if we could (calculate) the amplitude of the Wien Bridge Oscillator and then voltage divide, the value can vary based on the environment of the components at the time of measurement.  Instead of adding a fixed value, I am going to add measuring the Vin+ as part of the E.C. calculation.

According the MCP6244 datasheet, given the power supplied to the op amp’s rails: V+ = 5V, V- = 0V, the maximum Vout range = 4.65V(5V-.35V) to .35V(0V+.35V) or 4.3V.  Given the op amp’s headroom of 4.3V, the maximum Vpp of Vin+ = 4.3V/7 = 614mV.  To be a bit more conservative, I will shoot for a Vpp of 500mV for the Vin+ of the EC Gain Loop.  The EC readings should be improved with a larger Vin+ Vpp.

Measure Vin+

I have updated the Ladybug Shield’s design to measure Vin+ along with Vout.  To do this, I added the 75LVC1G53 2:1 analog multiplexer (data sheet):

ECCircuitWithSwitch

In a previous post, I discussed the updated design of the rectifier circuit that the Ladybug Shield will use.  Since this waveform also needs to be converted to DC, it needs to be rectified.  I could copy the rectifier circuit.  However, I’d rather have one rectifier circuit that is reused by the waveforms. This way, if there are problems with rectification they are contained within one rectifier circuit.

I added a 10K pull-up resistor to the ~E pin (pin 2).  This way, the switch is off by default.  The switch is turned on or off by sending a digitalWrite() through an Arduino pin.

Test the Circuit – Calculate EC

Time to wire up the components on a breadboard and check out the circuit.

IMG 3270

 

Rectification Observations

I was wrong in a previous post where I decided I needed a dialectic capacitor.  From my current understanding of circuit analysis, it wasn’t memory being held on by the ceramic capacitor – but the challenge I note below.  The circuit was not able to discharge the potential energy on the last op amp.  I changed the resistor back to a capacitor.

I tried three rectification topologies:

  • input into the ADC at point A (image below)
  • input into the ADC at point B (image below)
  • input into the ADC at point B but added a 470KΩ resistor and 1µF capacitor in parallel.
New Rectifier
The ADC measurements made through an Arduino are listed below in order of lowest standard deviation and closest to expected values (recall I am using a resistor to represent the EC probe’s variable resistance input into the feedback loop):
    • point A. 
    • point B with the added parallel RC.  
    • point B with no additional load

Given my current abilities at circuit analysis, when switching from a higher waveform to a lower waveform, the potential energy needs to be discharged, but there is no load before the ADC.  Introducing the 470KΩ resistor allows discharge.  The capacitor then fills in the holes in between the potential peaks that are exposed from the discharge.  If this is true, It seems the circuit would be better off getting rid of voltage follower (i.e.: the last op amp) op amp since it adds complexity and variability.  On the other hand, the voltage follower op amp buffers the ADC from any surges that might occur between the two op amps.

I ran several tests showing measuring at point A was “good enough” and a better choice than the other two.

 
The design of the Ladybug Shield will remove the second (buffer) op amp from the rectifier circuit.  
 
EC measurements using the ADC and an Arduino sketch will use the Vout DC current following the negative feedback loop of the first op amp.

Arduino Sketch

After (carefully!) wiring up the circuit, I ran the Arduino Sketch found at this GitHub location.  To test the accuracy, I put a 200Ω and then a 2KΩ resistor to simulate the EC Probe’s input.

IMG 3271
It is best to view the Arduino Sketch found here.  The Arduino Sketch calculates the EC by:
  • Asking for the number of readings to take of Vin+ and Vout.  This way, the values can be the average of many values.  
  • reading Vin+

int readVIn(unsigned int num_readings)
{
dischargeCapacitor();
switchTo(VIN);
for (int i=0;i<num_readings;i++) {
int results = readADC();
myStats_Vin.add(results);
}
return myStats_Vin.average();
}

The dischargeCapacitor() function sets the Mosfet’s gate to HIGH which drains the capacitor.  The switchTo() function switches the 2:1 gate to be either read either Vin+ or Vout.  Next comes a loop that reads the ADC for as many readings as I requested to be averages.  A 3rd party library is used to return the average value.

  • reading Vout – very similar to how Vin+ is read.
  • Calculating the Gain = Vout/Vin+
  • Calculating the value for ReC = 1000/(Gain – 1)   [Note: the known resistor is the 1K feedback resistor in the EC Gain loop].
  • Calculating the value for EC in mS = 1/rEC*1000
In the run below, the resistor simulating the EC probe was 200Ω (approximately – the resistor comes from a pack of inexpensive breadboard ready resistors):

Enter number readings:
3
Reading Vin+
239
242
241
Vin+: 240
Reading ECv
1518
1521
1520
| Count: 3** Vin+ ** Average: 240.67| Std deviation: 1.25** ECv ** Average: 1519.67| Std deviation: 1.25
** Gain: 6.33| ReC: 187.65|EC: 5.33mS

I tried several runs with different number of readings up to 1,000.  All gave similar results.  The results for 200Ω is “good enough.”

In this run, I used a 2.2KΩ resistor (again, approximately):

Enter number readings:
3
Reading Vin+
239
246
242
Vin+: 242
Reading ECv
357
359
357
| Count: 3** Vin+ ** Average: 242.33| Std deviation: 2.87** ECv ** Average: 357.67| Std deviation: 0.94
** Gain: 1.48| ReC: 2104.35|EC: 0.48mS

Closing Thoughts

I am comfortable enough with the results to update the Ladybug Shield layout’s EC circuit. I plan to send updated Gerbers to OshPark so that I can reach my goal of using the shield to help me grow the lettuce starts that are starting to collect in my house. I’m very happy with the prototype process that I used. I was able to wire together the same parts to prototype on a breadboard that I use on the Shield. The SMT to DIP PCB boards have been a terrific aid in doing so. I now have parts that I can reuse on future breadboard circuits.

That’s it for now. Please find many things to smile about!

Follow

Get every new post delivered to your Inbox.