using System; using System.Threading; using System.Collections; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware.Netduino; namespace Sensors { class MG_811 { //define the DC gain of amplifier private const double DC_GAIN = 8.5; //define how many samples you are going to take in normal operation private const int READ_SAMPLE_INTERVAL = 50; //define the time interval(in milisecond) between each samples in normal operation private const int READ_SAMPLE_TIMES = 5; //These two values differ from sensor to sensor. user should derermine this value. private const double ZERO_POINT_VOLTAGE = 0.220F; //define the output of the sensor in volts when the concentration of CO2 is 400PPM private const double REACTION_VOLTGAE = 0.020F; //define the voltage drop of the sensor when move the sensor from air into 1000ppm CO2 //two points are taken from the curve. //with these two points, a line is formed which is //"approximately equivalent" to the original curve. //data format:{ x, y, slope}; point1: (lg400, 0.324), point2: (lg4000, 0.280) //slope = ( reaction voltage ) / (log400 ¨Clog1000) private double[] CO2Curve = null; public MG_811(OutputPort BOOL_PIN) { //Set Values for CO2Curve CO2Curve = new double[3] { 2.602, ZERO_POINT_VOLTAGE, (REACTION_VOLTGAE / (2.602 - 3)) }; //Turn ON PullUp Resistors BOOL_PIN.Write(true); } /// <summary> /// Returns the reading from the MG-811 CO2 Sensor. /// </summary> /// <param name="MG_PIN">This is the AnalogInput that the sensor's output pin is connected to</param> /// <returns>PPM CO2</returns> /// <remarks>Will return -1 if the reading is below 400 PPM</remarks> public double GetCO2Reading(AnalogInput MG_PIN) { int percentage; double volts; volts = MGRead(MG_PIN); percentage = (int)MGGetPercentage(volts); return percentage; } private double MGRead(AnalogInput MG_PIN) { int i; double v=0; for (i=0;i<READ_SAMPLE_TIMES;i++) { v += MG_PIN.Read(); Thread.Sleep(READ_SAMPLE_INTERVAL); } v = (v/READ_SAMPLE_TIMES) *5/1024; return v; } double MGGetPercentage(double volts) { if ((volts/DC_GAIN)>=ZERO_POINT_VOLTAGE) { return -1; } else { return System.Math.Pow(10,((volts/DC_GAIN) - CO2Curve[1])/CO2Curve[2]+CO2Curve[0]); } } } }
MG811 CO2 Sensor and Netduino Plus 2
#1
Posted 20 December 2012 - 04:47 PM
#2
Posted 20 December 2012 - 06:53 PM
#3
Posted 20 December 2012 - 06:58 PM
Is there a way i could make this compatible with the netduino? Maybe a resistor in line somewhere? Can you tell if it is actually 5v from the specs on the website?if its really 5v it can be dangerous to analog pins, they are not 5v tolerant when used as analog input (it wount harm the pin itself, but the adc)
#4
Posted 20 December 2012 - 07:09 PM
Hi, I just ported over some code for a CO2 Sensor found here and haven't been able to test it yet, but wanted to show everyone on here and see if anyone sees any problems with it. I'm not sure how this will work as it looks like the analog output voltage is 0-5.. If anyone has any suggestions, please let me know.
Joshua,
I haven't looked to much at the code, but it is good to see someone porting the Arduino Code over to C# and sharing it.
As for the 5vdc output that is too high for the Netduino ADC (max. 3.3vdc), but there are a couple of options.
- Seeing the module output is coming from an Op Amp you can setup a simple voltage divider (1.6kOhm & 3.3kOhm in series)
- It look like the Op Amp resistor R4 (7.5kOhm) is a surface mount device, you may be able to remove and replace with a 4.5kOhm resistor so that you get 3.3vdc at 10000 ppm CO2.
Chuck
#5
Posted 20 December 2012 - 07:40 PM
Thanks Chuck. could I replace it with one of these?Joshua,
I haven't looked to much at the code, but it is good to see someone porting the Arduino Code over to C# and sharing it.
As for the 5vdc output that is too high for the Netduino ADC (max. 3.3vdc), but there are a couple of options.
- Seeing the module output is coming from an Op Amp you can setup a simple voltage divider (1.6kOhm & 3.3kOhm in series)
- It look like the Op Amp resistor R4 (7.5kOhm) is a surface mount device, you may be able to remove and replace with a 4.5kOhm resistor so that you get 3.3vdc at 10000 ppm CO2.
Chuck
I'm a developer, not an electrical engineer, so I'm new to this.. however i do have a reqork station at my dispose. Is the smd above the correct package size?
#6
Posted 20 December 2012 - 08:13 PM
I am not sure I would need to see one of the boards to tell, the removal of the old resistor could be a little tricky. Someone like Arron (often in Chat room) may be able to provide some suggestions. The 805 resistor size is 2.0 mm × 1.25 mmThanks Chuck. could I replace it with one of these?
I'm a developer, not an electrical engineer, so I'm new to this.. however i do have a reqork station at my dispose. Is the smd above the correct package size?
#7
Posted 20 December 2012 - 08:41 PM
I've actually done this before.. it wasn't too bad. Would I be able to use a slightly smaller or larger value resister there.. I just looked at the price of those and id have to buy 100 of them at 3 something each just to get 1. I have This booklet of smds on hand and it has a 4.3kOhm and a 4.7kOhm in it. Would one of those work?I am not sure I would need to see one of the boards to tell, the removal of the old resistor could be a little tricky. Someone like Arron (often in Chat room) may be able to provide some suggestions. The 805 resistor size is 2.0 mm × 1.25 mm
#8
Posted 20 December 2012 - 09:58 PM
Joshua,I've actually done this before.. it wasn't too bad. Would I be able to use a slightly smaller or larger value resister there.. I just looked at the price of those and id have to buy 100 of them at 3 something each just to get 1. I have This booklet of smds on hand and it has a 4.3kOhm and a 4.7kOhm in it. Would one of those work?
Where are you located (country), You should be able to find someone to to sell you a couple of them.
Also check Amazon Resistor Kit or Ebay Resistor Kit.
As to the value I use the formula in the data sheet, You can change the value but may need to change R1 & R4 the value I gave you was keeping R1 the same. Just play with the values using the formula to find a pair that works for you.
You could use a Potentiometer and put 5 vdc on it and adjust the wiper to 3.3 vdc. I have done that with 10 turn pots before.
Chuck
#9
Posted 20 December 2012 - 10:08 PM
Joshua,
Where are you located (country), You should be able to find someone to to sell you a couple of them.
Also check Amazon Resistor Kit or Ebay Resistor Kit.
As to the value I use the formula in the data sheet, You can change the value but may need to change R1 & R4 the value I gave you was keeping R1 the same. Just play with the values using the formula to find a pair that works for you.
You could use a Potentiometer and put 5 vdc on it and adjust the wiper to 3.3 vdc. I have done that with 10 turn pots before.
Chuck
ok. I have one of those smd resistor kits This one to be exact. .. It just doesn't have 4.5k in it. What formula do i use for those resistors?
#10
Posted 20 December 2012 - 10:34 PM
Vout = Vin x (1+R4/R1)ok. I have one of those smd resistor kits This one to be exact. .. It just doesn't have 4.5k in it. What formula do i use for those resistors?
R4 = ((3.3/0.6)-1)x R1
Or like above you can take a 5kohm multi turn pot (the ones that I use are made to go in a printed circuit board and have a multi turn screw to adjust) and set the wiper to 3.3 kohm an the value should stay where you put it and use can adjust the output if needed.
Attached Files
#11
Posted 21 December 2012 - 01:47 AM
Vout = Vin x (1+R4/R1)
R4 = ((3.3/0.6)-1)x R1
Or like above you can take a 5kohm multi turn pot (the ones that I use are made to go in a printed circuit board and have a multi turn screw to adjust) and set the wiper to 3.3 kohm an the value should stay where you put it and use can adjust the output if needed.
oh, ok. I see.. so the .6 is the max output of the sensor which is the input to the amp circuit and the 3.3 is what you want the Vout max to be. Is there anywhere you would recomend where I can purchase one or two of those 5kOhm pots at a decent price? I'm in the US btw.
Thank You
Joshua
#12
Posted 21 December 2012 - 02:30 AM
oh, ok. I see.. so the .6 is the max output of the sensor which is the input to the amp circuit and the 3.3 is what you want the Vout max to be. Is there anywhere you would recomend where I can purchase one or two of those 5kOhm pots at a decent price? I'm in the US btw.
Thank You
Joshua
Depends on how fast you need them,
Digikey care them search for trim pot or trimmer. Minimum Quanity is only 1. Digikey trimpots
Radio Shack may have them or one that will work in a local store they show them online. 10K 15 turn trimmer
Ebay has them in the US 5K Trimpot or from Thialand (7 to 14 days) 25 turn 5K Trimpot
It does not have to be 5k, about anything from 1k to 10k should be good I think the impediance of the analog channel on the Netduino is fairly high (current draw suddent effect the voltage drop of the resistor / pot.
Hope this helps, If you have additional questions I will be gone out of country for the next 3 days, but someone should able to help. Have a Merry Christmas.
Chuck
#13
Posted 21 December 2012 - 02:47 PM
Depends on how fast you need them,
Digikey care them search for trim pot or trimmer. Minimum Quanity is only 1. Digikey trimpots
Radio Shack may have them or one that will work in a local store they show them online. 10K 15 turn trimmer
Ebay has them in the US 5K Trimpot or from Thialand (7 to 14 days) 25 turn 5K Trimpot
It does not have to be 5k, about anything from 1k to 10k should be good I think the impediance of the analog channel on the Netduino is fairly high (current draw suddent effect the voltage drop of the resistor / pot.
Hope this helps, If you have additional questions I will be gone out of country for the next 3 days, but someone should able to help. Have a Merry Christmas.
Chuck
Thanks. So, i'm either going to put this at R4 and adjust down to 4.5k or I am just going to replace R1 with a 1.6k SMD which i already have on hand. That would create a max Vout of 3.4125. Would that be too high for the netduino?
#14
Posted 15 January 2013 - 06:03 PM
Thanks. So, i'm either going to put this at R4 and adjust down to 4.5k or I am just going to replace R1 with a 1.6k SMD which i already have on hand. That would create a max Vout of 3.4125. Would that be too high for the netduino?
Hi Joshua,
Have you succeeded ?
Rocca
#15
Posted 15 January 2013 - 06:34 PM
Hi Joshua,
Have you succeeded ?
Rocca
I actually didn't end up using this because I needed more pins than I had on the Netduino so I utilized an Arduino and one of the Netduino's serial ports to get readings from multiple devices.
#16
Posted 16 January 2013 - 02:20 AM
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users