DFRobot_Examples_Programming_standard - jimaobian/DFRobotWiki GitHub Wiki

Example with SHT1x Library

Example with SHT1x Library

Source Code


/***************************************************
 Digital Temperature and Humidity sensor (With Stainless Steel Probe)
 <http://www.dfrobot.com/index.php?route=product/product&path=48&product_id=912>

 ***************************************************
 This example reads temperature and humidity from SHT1x Humidity and Temperature Sensor.

 Created 2014-8-28
 By Angelo qiao <[email protected]>
 Modified 2014-8-29
 By Angelo qiao [email protected]>

 GNU Lesser General Public License.
 See <http://www.gnu.org/licenses/> for details.
 All above must be included in any redistribution
 ****************************************************/

/***********Notice and Trouble shooting***************
 1.Connection and Diagram can be found here
 <http://www.dfrobot.com/wiki/index.php/Digital_Temperature_and_Humidity_sensor_(With_Stainless_Steel_Probe)_(SKU:SEN0148)#Connecting_diagram>
 2.This code is tested on Arduino Uno, Leonardo, Mega boards.
 3.SHT1x library is created by jonoxer.
 See <https://github.com/practicalarduino/SHT1x> for details.
 ****************************************************/

#include "SHT1x.h"

const int SHT1xDataPin = 10;
const int SHT1xClockPin = 11;

SHT1x sht1x(SHT1xDataPin, SHT1xClockPin);

void setup()
{
  Serial.begin(9600);
  Serial.println("Starting up...");
}

void loop()
{
  int temperatureC=sht1x.readTemperatureC(); //store temperature in Centigrade
  int temperatureF=sht1x.readTemperatureF(); //store temperature in Fahrenheit
  int humidity=sht1x.readHumidity(); //store humidity

  Serial.print("Temperature: ");
  Serial.print(temperatureC);
  Serial.print("C / ");
  Serial.print(temperatureF);
  Serial.print("F. Humidity: ");
  Serial.print(humidity);
  Serial.println("%");

  delay(2000);
}

Example of SHARP GP2Y0A41SK0F IR ranger sensor

Example of SHARP GP2Y0A41SK0F IR ranger sensor

Source Code


/***************************************************
      SHARP GP2Y0A41SK0F IR ranger sensor (4-30cm)
 <http://www.dfrobot.com/wiki/index.php/SHARP_GP2Y0A41SK0F_IR_ranger_sensor_(4-30cm)_SKU:SEN0143>

 ***************************************************
 This example reads distance value from SHARP GP2Y0A41SK0F

 Created 2014-8-28
 By Angelo qiao <[email protected]>
 Modified 2014-8-29
 By Angelo qiao [email protected]>

 GNU Lesser General Public License.
 See <http://www.gnu.org/licenses/> for details.
 All above must be included in any redistribution
 ****************************************************/

/***********Notice and Trouble shooting***************
 1.The distance of the IR ranger sensor is 4-30cm
 2.This code is tested on Arduino Uno, Leonardo, Mega boards.
 3.The DFPlainprotocol is included and you can see this page for details:
 <http://www.dfrobot.com/wiki>
 ****************************************************/

#include "Arduino.h"

//PIN definition
const int DistancePin = A1;

//Functions
float readDistance(); //read distance from SHARP GP2Y0A41SK0F

void setup()
{
  Serial.begin(9600);
  Serial.println("Start...");
}

void loop()
{
  float distance=readDistance(); //Store the distance value

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println("cm");

  delay(1000);
}

float readDistance()
{
  int sensorValue = analogRead(DistancePin);
  sensorValue = constrain(sensorValue, 80, 530);

  float distanceValue = 2076.0/(sensorValue-11.0);

  return distanceValue;
}

Example of CO2 Sensor

Example of CO2 Sensor

Example of CO2 Sensor

Source Code


/***************************************************
 CO2 Sensor (Arduino compatible)
 <http://www.dfrobot.com/index.php?route=product/product&product_id=1023&search=co2&description=true>

 ***************************************************
 This example read and caculate the CO2 percentage from MG-811 Gas Sensor

 Created 2014-8-28
 By Angelo qiao <[email protected]>
 Modified 2014-8-29
 By Angelo qiao [email protected]>

 GNU Lesser General Public License.
 See <http://www.gnu.org/licenses/> for details.
 All above must be included in any redistribution
 ****************************************************/

/***********Notice and Trouble shooting***************
 1.The percentage of the CO2(carbon dioxide) is only for reference.
 You need to calibrate it in industrial implement.
 2.This code is tested on Arduino Uno, Leonardo, Mega boards.
 3.Through exponential curve fitting based on the data from datasheet,
 we get the function,which calculates the percentage of the CO2 from voltage.
 ****************************************************/

#include "Arduino.h"

//PIN definition
const float PercentageCO2Pin = A0;

//Functions
float readPercentageCO2(); //read percentage of the CO2 from MG811

void setup()
{
  Serial.begin(9600);
  Serial.println("Starting...");
}


void loop()
{
  float percentageCO2 = readPercentageCO2();

  Serial.print("CO2: ");
  Serial.print(percentageCO2);
  Serial.println("PPM");

  delay(1000);
}

//Functions implement
//Co2=2.718281828459045*e^(-0.0558861525*sensorVoltage)
float readPercentageCO2()
{
  const float VotageGain = 8.5;

  int sensorValue = analogRead(CO2Pin);
  float sensorVoltage = sensorValue/1024.0*5.0;
  sensorVoltage = sensorVoltage*1000.0/VotageGain;
//  Serial.println(sensorVoltage);
  float percentageCO2Value = 25784989641.0468*pow(2.718281828459045, -0.0558861525*sensorVoltage);
  return percentageCO2Value;
}

Example of CO2 Sensor with smooth algorithm

Example with SHT1x Library example with sht1x library

Source Code


/***************************************************
 CO2 Sensor (Arduino compatible)
 <http://www.dfrobot.com/index.php?route=product/product&product_id=1023&search=co2&description=true>

 ***************************************************
 This example read and caculate the CO2 percentage from MG-811 Gas Sensor

 Created 2014-8-28
 By Angelo qiao <[email protected]>
 Modified 2014-8-29
 By Angelo qiao [email protected]>

 GNU Lesser General Public License.
 See <http://www.gnu.org/licenses/> for details.
 All above must be included in any redistribution
 ****************************************************/

/***********Notice and Trouble shooting***************
 1.The percentage of the CO2(carbon dioxide) is only for reference.
 You need to calibrate it in industrial implement.
 2.This code is tested on Arduino Uno, Leonardo, Mega boards.
 3.Through exponential curve fitting based on the data from datasheet,
 we get the function,which calculates the percentage of the CO2 from voltage.
 4.The range of the MG811 is 400-10000ppm
 ****************************************************/


#include "Arduino.h"

//PIN definition
const float PercentageCO2Pin = A0;

//Functions
float readPercentageCO2(); //read percentage of the CO2 from MG811

void setup()
{
  Serial.begin(9600);
  Serial.println("Starting...");
}

void loop()
{
  float percentageCO2 = readPercentageCO2();

  Serial.print("CO2: ");
  if (percentageCO2 == 400.0) {
    Serial.print("<=400");
  }
  else if (percentageCO2 == 10000.0){
    Serial.print(">=10000");
  }
  else{
    Serial.print(percentageCO2);
  }
  Serial.println("PPM");

  delay(1000);
}

//Functions implement
//Co2=2.718281828459045*e^(-0.0558861525*sensorVoltage)
float readPercentageCO2()
{
#define ANALOG_SAMPLING_TIMES  (50)   //sampling time in one reading process
#define ANALOG_SAMPLING_INTERVEL (5)  //sampling interval of the two samples

  const float VoltageGain = 8.5;       //Voltage Gain of the circus

  long sensorSumValue = 0;

  for (int samplingIndex=0; samplingIndex<ANALOG_SAMPLING_TIMES; samplingIndex++) {
    sensorSumValue += analogRead(PercentageCO2Pin);
    delay(ANALOG_SAMPLING_INTERVEL);
  }

  float sensorVoltage = sensorSumValue*5.0/(1024.0*ANALOG_SAMPLING_TIMES);

  sensorVoltage = sensorVoltage*1000.0/VoltageGain;
//  Serial.println(sensorVoltage);
  float percentageCO2Value = 25784989641.0468*pow(2.718281828459045, -0.0558861525*sensorVoltage);
  percentageCO2Value = constrain(percentageCO2Value, 400, 10000);
  return percentageCO2Value;
}