Status... I just put the prototype bread board together from the schematic and did my first run (still no 0.1uf cap across pins 4 and 11 on the quad op amp yet). At 48" H20 (1.73 psi), I got 1.880 ±.004 (span is 1.55VDC), and at 0 psi I got 0.330 VDC±.001. The span is low and the zero reading is high and would like to make that better but it is not bad for now. The noise level of .004VDC is ok for my needs, but of course it would be better if it was not that high. I am trying to resolve 1" of water (0.032VDC) which seems almost doable with the first run. I am only running at 1ma now. I think you said if I turned the current up to 1.5 ma I could have problems with the 9VDC regulator. The R5 0 to 1Kohm (coming out of A3) is set at 0 ohms now. Next is the hard part, getting the micro to read the data and will report back after I get that part working. Right now I am very happy just to see a live output wiggle.
I hope you will find that the getting the micro to read data is not so hard!
You can safely turn up the excitation to 1.5 mA and see if it still works. Measure the voltage between Vss and the excitation input, and if that remains less 7 V, it is a safe bet. Another way to increase the gain is in the amplification stages, A1, A2 or A3. I don't know where the 0.33V offset is coming from. It could be offset in the pressure sensor, or a mismatch of resistor values in the differential amplifier. Are you using 1% resistors? It helps in differential amplifiers to use matched pairs.
Ok, thanks again for the help. I will turn up the current to 1.5 ma and check it out. I am using 5% resistors but can easily switch them out for better ones, those were just handy. The extra high offset is a function of my pressure source. If I have it just sitting on the table with nothing plumbed into the pressure port, the offset is about 0.120 Vdc which still seems a bit high.
I wired the Basic Stamp with the ADC according to the demo and got rid of the extra channels 1-7 in the code and just kept channel 0 because I am only reading one voltage. Then ran ran the program to see what happened. I was able to get an output to show up on the debug terminal that changes with pressure which was exciting. But the outputs did not match what I read with my volt meter. On the debug terminal, the ADC channel 0 displayed 0.766 and the Volts displayed as 628, however my volt meter read 0.122 V which had me stumped. Today I found the "Basic Analog and Digital Text, v1.4" in the Parallax store and ordered that up as something I thought is a "must have" for me. I then read the on line version of the text and found in the section titled" The ADC0831 Integrated Circuit - An 8-bit Analog to Digital Converter" that showed VDD and Vref both connected to VDD on the Stamp (I did not have the ADC VDD connected to the Stamp VDD). When I connected both the VDD and Vref of the ADC to the Stamp VDD as indicated in the text book, the debug terminal was much closer to my volt meter. The ADC Ch0 read between 93-97 and the Volts ranged between 0.113 and 0.118 VDC compared to my steady volt meter reading of 0.119VDC. When I pressurized the unit, the output tracked pretty close to the volt meter as well. I then added the 0.1uF cap across pins 4&11 of the LM324 Quad Op Amp and the voltages got about 1 to 2 mv closer to my digital volt meter.
It seemed like there is a conversion going on somewhere maybe within the constants section I don't understand yet. How does the syntax for Shiftin and Shiftout work in the demo? For example, in SHIFTIN Dpin, Cpin, Mode, [ Variable { \Bits } {, Variable { \Bits }…} ]. This syntax was taken from BASIC Stamp Syntax and Reference Manual Version 2.2 page 431. I have a hard time understanding how the Variable\Bits portion in both Shiftin and Shiftout work. I also don't really understand why the constants are defined as they are and why the 1.22 needs to be written as $0139. I did note that the software does not seem to like the decimal point. If that is the case, how do you convert 1.22 to $0139? Maybe the after doing the exercises in the Basic Analog and Digital kit I will understand this better, but any clarification would be appreciated while I wait for it to arrive in the mail. Here is the program as I ran it, the debug display is a little messy but that can be fixed.
The readings do seem to jump around about 0.3% FS on my solderless breadboard. Do you think the readings get more solid when I solder the components in place?
Seems like I need to get the 1% resistors in place, turn up the current to 1.5 ma, and clean up the assembly a bit. I am still waiting for the switches to arrive in the mail that will control the solenoid. While waiting, I will try to write some code that will try to write the code that reads the data into the microcontroller and then write it to the EEPROM, and then write some control logic with the switches. I was able to do some switch control with the ping sensor using a transistor that came with my What is a Microcontroller kit.
Recheck the wiring agnd and dgnd, and Vdd and Vref.
Regarding the number of bits transferred in a SHIFT.. command, the default is 8 bits, but the \13 in the following overrides the default and tells it to do 13 bits instead.
The SHIFTOUT command by default shifts out 8 bits. The value of offset is a CONstant=24 which is in binary 00011000. That is ORed with the channel, 3 bits, so the the value shifted out for channel 0 will be the same, 00011000 msb first. That is the default 8 bits. The number of bits and the command positions are determined by the timing diagram in the data sheet, reproduced here:
I think you could put a \5 on the end of that SHIFTOUT and it would still work, because only 5 of the bits are significant. The top "1" bit is the start bit. The SHIFTIN does 13 bits instead of 12 in order to bypass the "Null bit". There are several equally effective ways to write these shift commands, but sometimes it is a frustrating process to get it right when pioneering a new chip!
About the math. The HEX constant $0139 is the same as 313 decimal. Note that 313/256 = 1.223. That is the fraction you want to convert from ADC counts, 4096 at full scale, to millivolts, 5000 at full scale. The Stamp special operator */ does its magic like this: Say you have,
y = 628 */ 313.
The Stamp internally multiplies 628 * 313 into a large number and then divides that by 256, to give the result of 766. The constant does not have to be written in HEX, but some people find that helpful, because in $0139 the $01 is the integer part and $39 is the fractional part, kind of like 1.22*x = 1*x + 0.22*x.
Thank you for the detailed reply. It was very helpful. Regarding Agnd, I do not currently have it connected to anything. Should the ADC VDD and Vref be connected to the Stamp Vdd, and the ADC AGND and Dgnd be connected to the Stamp Vss? Right now I only have Dgnd connected to the Stamp Vss. When I connected the Agnd to the Stamp Vss, it seems pretty centered around my volt meter reading. The Agnd to stamp Vss seemed to help a lot. Today the Volt meter reads 117.6 mv and the debug shows a range of 116 to 119 mv with the Agnd now connected. Thanks!
That sounds like great progress. The AGND and DGND are separate so that a system designer can minimize the amount of digital switching noise that is coupled into the analog measurement circuits. You can read more about it in the MCP3208 data sheet. But yes, they do both have to be connected to Vss at some point, and the results prove it!
It is best in a differential ampifier to use closely matched resistors, 1% or even 0.1% or individually matched. Both inputs from the pressure bridge are going to be at voltages near 6 V, but what you want to measure is the difference between the inputs. If the difference stays the same, at say 40mV, and the absolute input happens to increase to 7V or drop to 5V, you don't want the output of the amplifier to change. No change means the "common mode gain" is zero and that is the ideal. If it does change, you can't distinguish that from a change due to pressure. In this MSI pressure sensor, there probably won't be so much common mode change, but there will probably be some due to temperature. The bottom line is to use resistors more closely matched than random 5% values.
Do I need a pull down resistor hooked up to the photoMOS relay (Panasonic AQY282EH) to close the circuit to my solenoid? I am using two relays to make the motor run both directions (I guess an ON-OFF-ON toggle switch might be better if such a thing was available). I have the wire going from Stamp P9 to pin 1 of the relay, then from the pin 2 of the relay to the Stamp Vss. This closes the relay completing the circuit to the solenoid that runs the motor. I am now able to read a mv output into the EEPROM and make a decision on which switch to select so the motor goes forward or backward, or not at all if I am not off more than the tolerance allowed from the value stored in the EEPROM. The motor runs fine except that it will run normal (forward, back or not at all), then it will not go in one direction unless I reset the program. Sort of like one of the switches are stuck since it is not being pulled down.
The subroutine goes like this:
Move Motor:
READ 20, Word EEPROMmVSet
IF mVoltsOutput>EEPROMmVSet+300 THEN
HIGH 9 ' Closes relay to move motor forward
ELSEIF mVoltsOutput<EEPROMmVSet-300 THEN
HIGH 15 ' Closes relay to move motor backward
ELSE
LOW 9 ' mV output is in spec, don't move motor
LOW 15
ENDIF
RETURN
It seems to be related to the rate I change pressure. If I change it fast, it does not behave properly, if I change the pressure slowly, it seems to respond fine.
Other than that, it seems to be working pretty darn good now. Again, thanks for all of your help!
You should really have a current limiting resistor in series with the relay input, like the diagram in post #21. Something like 100Ω to 330Ω. You could put an visible LED in series too, from the stamp pin, to one side of the 100Ω, to the relay (+) input, and from the relay (-) input to Vss. The LED serves as an indicator and might help with troubleshooting the program issue.
The subroutine looks okay at first glance. Maybe there is something else going on elsewhere in the program. The rate of change shouldn't make much difference. DEBUG the ADC and threshold values.
That was a good idea. I put an LED in series with a 220 ohm resistor to pin 1 of the switches. I start off a zero pressure, and the green LED glows, then through the tolerance band where the motor should not run, and no LEDs glow, then the yellow LED glows, they when I suddenly release the pressure, the unit motor stops and both LEDs glow. I can slowly increase the pressure from zero and usuallyI can get the motor to start operating again normally. I can not duplicate this with the toggle mechanical switch that comes with the motor. The pressure outputs in the debug screen seem to be fine. It is almost like the step pressure change is too much for the two switches working together, but that does not seem too logical. Do you know if they make a toggle switch like these so both contacts can not close at one time?
Oh, I may have figured it out. I think I have to turn off each switch that fails my if statement or if the pressure changes the next time through the subroutine it goes right past it leaving the switch on, passes the next if statement and turns on that switch. Now both switches are on and the motor is stuck in the off condition. The middle section of the tolerance band turns off both switches which is why I think the pressure needs to move slowly now so it goes through the subroutine in the in spec condition so both switches are turned off. It also explains why I can increase pressure slowly and sometimes start it up again, and why when the motor is stuck off that the pressures seem to track on the debug terminal correctly. Kind of a rambling explanation, but I think you may understand.
I think I understand. It sounds like you got it and the important thing is that it works. It should.
In answer to your hardware question, I think they do make the form of photomos relay that is like a toggle switch, center off, but you would have to seek it out. With two of the relays you can get the same effect by hooking the input LEDs back to back, and hook them up between two Stamp pins, instead of between each Stamp pin and Vss separately. Per the diagram attached. Make one pin high and the other low to go forward, and vice versa for backward. Leaving the two pins in the same state or as inputs will turn both motors off.
Tracy, one more problem I seem to be having is that the response time of the pressure sensor seems to be very fast and is thus picking up the pressure pulses from the air pump. My readings are very noisy from the ADC when the pump is running but the readings from my DVM are very solid (my DVM is picking up the readings just prior to going into the ADC). When the pump supplies pressure to the sensor, the ADC readings jump around about ±400 mv and the DVM jumps around about ±2 mV. When I disconnect the pressure input hose to the pressure sensor, all readings are solid. I verified light was not impacting the ADC readings and verified it was not electrical noise from the pump by putting the pump on a completely separate battery with no common wiring with the sensor. When I blow into the pressure port all readings are solid as read from the ADC and the DVM. When I stuff part of a cotton qtip in the hose, it dampens it about half of what it was before.
Is there a filter I can put on the pressure sensor output that would neatly dampen out the ADC readings? Or do you have any other suggestions that can smooth out the output?
There are a couple of places you could put capacitors in order to smooth out the readings. and there is also the possibility of software smoothing. Does a time constant on the order of about 1 second sound right?
Try a capacitor across the output of the pressure sensor, between pins 4 and 10. Experiment. It may take an electrolytic or tantalum capacitor to see noticeable smoothing, in which case observe the (+) and pin (-) connections to the sensor and to the capacitor. Alternative locations for a capacitor are in parallel with R2 (the 100kΩ feedback resistor on op-amp A3), or from the ADC input to ground. For the latter you could increase the value of R5 up to 10kΩ. Again, experiment.
As a second step with software smoothing you can do something like this:
reading VAR Word
smoothed VAR Word
gosub next_Reading ' this gets the reading from the ADC
smoothed = reading ' initialize the smoothing
DO
gosub next_Reading
smoothed = (smoothed + reading) / 2
DEBUG DEC reading, TAB, DEC smoothed, CR
LOOP
Yes, that helped a lot. I tried the caps across the sensor output pins 4 & 10 and it quieted down a lot but it took about 20 seconds to stabilize on the final reading so the pressure response was very slow. I used a combination of 3300 uf and 1000 uf caps in combination resulting in 767 uf total. Something between 200 uf and 500 uf would be better but no capacitor stores are open today.
Then I tried the ADC input (pin 1, Ch 0) to ground and the pressure response was almost instant using a 1000uf capacitor. However, the capacitor increased the offset voltage from 328 mv without the cap, to 777 mv with the cap. (Remember, my offset is initially high because the air pump is on, in line, but just blowing in the air so there is some natural back pressure associated, when disconnected the offset drops to about 100 mv). Also, the span (FS - offset) decreased from 2692 mv without the cap to 2279 mv with the cap. I am ok with that change if not hard on the electronics since I can compensate for the output, but it was unexpected.
I also tried the electrolytic 1000 uf cap between the LM324 pin 9 (IN3-) and pin 8 (OUT 3) with the neg cap electrode on pin 9 and did not see a change.
If the observation found using the 1000 uf cap between the ADC input pin (Ch 0) to ground is not a problem from a circuit stand point, I can use it with the change in offset and span.
I also tried some different smoothing routines and found that the variation is reduced from ±13 mv to ±6 mv by smoothing the data.
My prototype boards are looking like a bowl of noodles now but it works very nice. Thanks for all of your help!
Comments
You can safely turn up the excitation to 1.5 mA and see if it still works. Measure the voltage between Vss and the excitation input, and if that remains less 7 V, it is a safe bet. Another way to increase the gain is in the amplification stages, A1, A2 or A3. I don't know where the 0.33V offset is coming from. It could be offset in the pressure sensor, or a mismatch of resistor values in the differential amplifier. Are you using 1% resistors? It helps in differential amplifiers to use matched pairs.
I wired the Basic Stamp with the ADC according to the demo and got rid of the extra channels 1-7 in the code and just kept channel 0 because I am only reading one voltage. Then ran ran the program to see what happened. I was able to get an output to show up on the debug terminal that changes with pressure which was exciting. But the outputs did not match what I read with my volt meter. On the debug terminal, the ADC channel 0 displayed 0.766 and the Volts displayed as 628, however my volt meter read 0.122 V which had me stumped. Today I found the "Basic Analog and Digital Text, v1.4" in the Parallax store and ordered that up as something I thought is a "must have" for me. I then read the on line version of the text and found in the section titled" The ADC0831 Integrated Circuit - An 8-bit Analog to Digital Converter" that showed VDD and Vref both connected to VDD on the Stamp (I did not have the ADC VDD connected to the Stamp VDD). When I connected both the VDD and Vref of the ADC to the Stamp VDD as indicated in the text book, the debug terminal was much closer to my volt meter. The ADC Ch0 read between 93-97 and the Volts ranged between 0.113 and 0.118 VDC compared to my steady volt meter reading of 0.119VDC. When I pressurized the unit, the output tracked pretty close to the volt meter as well. I then added the 0.1uF cap across pins 4&11 of the LM324 Quad Op Amp and the voltages got about 1 to 2 mv closer to my digital volt meter.
It seemed like there is a conversion going on somewhere maybe within the constants section I don't understand yet. How does the syntax for Shiftin and Shiftout work in the demo? For example, in SHIFTIN Dpin, Cpin, Mode, [ Variable { \Bits } {, Variable { \Bits }…} ]. This syntax was taken from BASIC Stamp Syntax and Reference Manual Version 2.2 page 431. I have a hard time understanding how the Variable\Bits portion in both Shiftin and Shiftout work. I also don't really understand why the constants are defined as they are and why the 1.22 needs to be written as $0139. I did note that the software does not seem to like the decimal point. If that is the case, how do you convert 1.22 to $0139? Maybe the after doing the exercises in the Basic Analog and Digital kit I will understand this better, but any clarification would be appreciated while I wait for it to arrive in the mail. Here is the program as I ran it, the debug display is a little messy but that can be fixed.
The readings do seem to jump around about 0.3% FS on my solderless breadboard. Do you think the readings get more solid when I solder the components in place?
Seems like I need to get the 1% resistors in place, turn up the current to 1.5 ma, and clean up the assembly a bit. I am still waiting for the switches to arrive in the mail that will control the solenoid. While waiting, I will try to write some code that will try to write the code that reads the data into the microcontroller and then write it to the EEPROM, and then write some control logic with the switches. I was able to do some switch control with the ping sensor using a transistor that came with my What is a Microcontroller kit.
This is good fun.
Regarding the number of bits transferred in a SHIFT.. command, the default is 8 bits, but the \13 in the following overrides the default and tells it to do 13 bits instead.
SHIFTOUT DataIn, Clock, MSBFIRST, [ Offset | channel ]
SHIFTIN DataOut, Clock, MSBPOST, [results\13]
The SHIFTOUT command by default shifts out 8 bits. The value of offset is a CONstant=24 which is in binary 00011000. That is ORed with the channel, 3 bits, so the the value shifted out for channel 0 will be the same, 00011000 msb first. That is the default 8 bits. The number of bits and the command positions are determined by the timing diagram in the data sheet, reproduced here:
I think you could put a \5 on the end of that SHIFTOUT and it would still work, because only 5 of the bits are significant. The top "1" bit is the start bit. The SHIFTIN does 13 bits instead of 12 in order to bypass the "Null bit". There are several equally effective ways to write these shift commands, but sometimes it is a frustrating process to get it right when pioneering a new chip!
About the math. The HEX constant $0139 is the same as 313 decimal. Note that 313/256 = 1.223. That is the fraction you want to convert from ADC counts, 4096 at full scale, to millivolts, 5000 at full scale. The Stamp special operator */ does its magic like this: Say you have,
y = 628 */ 313.
The Stamp internally multiplies 628 * 313 into a large number and then divides that by 256, to give the result of 766. The constant does not have to be written in HEX, but some people find that helpful, because in $0139 the $01 is the integer part and $39 is the fractional part, kind of like 1.22*x = 1*x + 0.22*x.
It is best in a differential ampifier to use closely matched resistors, 1% or even 0.1% or individually matched. Both inputs from the pressure bridge are going to be at voltages near 6 V, but what you want to measure is the difference between the inputs. If the difference stays the same, at say 40mV, and the absolute input happens to increase to 7V or drop to 5V, you don't want the output of the amplifier to change. No change means the "common mode gain" is zero and that is the ideal. If it does change, you can't distinguish that from a change due to pressure. In this MSI pressure sensor, there probably won't be so much common mode change, but there will probably be some due to temperature. The bottom line is to use resistors more closely matched than random 5% values.
The subroutine goes like this:
Move Motor:
READ 20, Word EEPROMmVSet
IF mVoltsOutput>EEPROMmVSet+300 THEN
HIGH 9 ' Closes relay to move motor forward
ELSEIF mVoltsOutput<EEPROMmVSet-300 THEN
HIGH 15 ' Closes relay to move motor backward
ELSE
LOW 9 ' mV output is in spec, don't move motor
LOW 15
ENDIF
RETURN
It seems to be related to the rate I change pressure. If I change it fast, it does not behave properly, if I change the pressure slowly, it seems to respond fine.
Other than that, it seems to be working pretty darn good now. Again, thanks for all of your help!
The subroutine looks okay at first glance. Maybe there is something else going on elsewhere in the program. The rate of change shouldn't make much difference. DEBUG the ADC and threshold values.
In answer to your hardware question, I think they do make the form of photomos relay that is like a toggle switch, center off, but you would have to seek it out. With two of the relays you can get the same effect by hooking the input LEDs back to back, and hook them up between two Stamp pins, instead of between each Stamp pin and Vss separately. Per the diagram attached. Make one pin high and the other low to go forward, and vice versa for backward. Leaving the two pins in the same state or as inputs will turn both motors off.
Is there a filter I can put on the pressure sensor output that would neatly dampen out the ADC readings? Or do you have any other suggestions that can smooth out the output?
Try a capacitor across the output of the pressure sensor, between pins 4 and 10. Experiment. It may take an electrolytic or tantalum capacitor to see noticeable smoothing, in which case observe the (+) and pin (-) connections to the sensor and to the capacitor. Alternative locations for a capacitor are in parallel with R2 (the 100kΩ feedback resistor on op-amp A3), or from the ADC input to ground. For the latter you could increase the value of R5 up to 10kΩ. Again, experiment.
As a second step with software smoothing you can do something like this:
Then I tried the ADC input (pin 1, Ch 0) to ground and the pressure response was almost instant using a 1000uf capacitor. However, the capacitor increased the offset voltage from 328 mv without the cap, to 777 mv with the cap. (Remember, my offset is initially high because the air pump is on, in line, but just blowing in the air so there is some natural back pressure associated, when disconnected the offset drops to about 100 mv). Also, the span (FS - offset) decreased from 2692 mv without the cap to 2279 mv with the cap. I am ok with that change if not hard on the electronics since I can compensate for the output, but it was unexpected.
I also tried the electrolytic 1000 uf cap between the LM324 pin 9 (IN3-) and pin 8 (OUT 3) with the neg cap electrode on pin 9 and did not see a change.
If the observation found using the 1000 uf cap between the ADC input pin (Ch 0) to ground is not a problem from a circuit stand point, I can use it with the change in offset and span.
I also tried some different smoothing routines and found that the variation is reduced from ±13 mv to ±6 mv by smoothing the data.
My prototype boards are looking like a bowl of noodles now but it works very nice. Thanks for all of your help!