Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

Joshua

Member Since 12 Dec 2012
Offline Last Active Feb 02 2014 06:57 PM
-----

Topics I've Started

MG811 CO2 Sensor and Netduino Plus 2

20 December 2012 - 04:47 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.

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]);
            }
        }
    }
}

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.