$USD
  • EUR€
  • £GBP
  • $USD
PROJECTS ArduinoGravity

Three-in-One Sensor and LM35, DS18B20 - Project Tutorial

DFRobot Jul 28 2017 857

Recently, the weather is so hot and you could fry eggs on the sidewalk. It is a good job that there are three sensors around me when I want to do an indoor temperature test. Below are sensors in need.


*You can click links in the end page for details.

Gravity: Analog LM35 Temperature Sensor for Arduino

LM35 is widely used as a semiconductor temperature sensor. It has good linearity and high sensitivity and its applications are easy to use. The output voltage is proportional to the temperature.

Connection Diagram

Room temperature read in an air conditioner is 27°C and in the LM35, it is 27.34°C (an average value, reading value is floating from 27°C to 28.2°C). Deviation exists because of large indoor environment. The air movement may influence temperature.

There is no LED display in the Arduino programs of DFRobot community but I add it to read temperature more visually. Even though we can open serial port monitor, revise baud rate to 9600 to receive environmental temperature, it is too rigid.
*Sample code is in the end page: LM35_CW.ino

Gravity: DS18B20 Temperature Sensor (Arduino Compatible)
DS18B20 is the most common temperature sensor in the market, small, precise, and convenient to connect. After package, it is applicable to different situations. You can change exterior according to situations, such as plastic films, boilers, engine rooms, clean rooms, and even magazines.

Connection Diagram


Room temperature read in an air conditioner is 27°C and in the DS18B20, it is 27.56°C (an average value, reading value is floating from 27.4°C to 28.9°C). The air movement may influence temperature.

Sample code to read temperature is in the end page: ds18b20.ino. It can display temperature value in the LED screen.

Three-in-One Sensor

Read introductions of two sensors, are you interested in playing sensors in this summer? Guessed that simple temperature measurement is not cool enough, I would love to share a three-in-one sensor that can monitor PM2.5, PM10, and formaldehyde in the air, temperature, and humidity at the same time. It chanced that my friend sent me a wooden room designing, then I proofed and made an environmental monitor house. If you are interested in this project, you should have a try!
When the environmental house is accomplished, you can use it to detect your living environment. If you want test results and announced data are the same, you need to test different notes. After all, test results are different in different places.

Connection Diagram


Assembly drawing
Wooden house and other accessories are as below:

Assembly Process
Step1.


Step2.


Step3.


Step4.


Step5.


Hardware Connection
Step1.


Step2.


Effects
After assembling, I am going to do test when the current is switched on. Below are the contrast test results of two different mobile phones.


Now you can test your living environment and get real data.
In the particularly hot days, we can measure temperature and humidity with three-in-one sensor. If you have just decorated your house, you can also test formaldehyde level. It is also an ideal tool to test environment around us.
*Sample code is in the end page: AirQualityMonitor.ino
Master boards of above three sensors are all compatible with DFRuino UNO R3 of Arduino UNO R3. Moreover, they replace 8U2 with ATmega16U2, realize faster conversion rate and larger memory space, let UNO R3 do not need any drive program in Linux or Mac. If you are interested in sensors mentioned, you can click the links below to know details.

Caution:
1. When uses DERduino UNO R3, it need Arduino1.0 to drive folder. We have tested that R3 applies to earlier version of IDE. However, if this is the first time for a PC to run R3, Arduino1.0 is needed to drive folders. If you are interested in differences of new IDE, you can check Arduino1.0 reports in Arduino official web.
2. To connect there-in-one sensor to DERduino UNO R3, you should download programs tp UNO master board firstly and assemble. You can use analog serial port to add more sensors for more convenient debugging, but you should wire it by yourself, it is not applicable to plug into DERduino UNO R3. However, plug is available for master board of DFRobot Leonardo & Xbee R3 serial port that is different from D0, D1.

Sensors links
Gravity: Analog LM35 Temperature Sensor for Arduino
Gravity: DS18B20 Temperature Sensor (Arduino Compatible)
Three-in-One Sensor

Power supply

7.4V Lipo 2500mAh Battery (Arduino Power Jack)
Wall Adapter Power Supply 7.5VDC 1A (American Standard)

Master board UNO R3
DFRduino UNO R3 - Arduino Compatible

Expansion board
Gravity: IO Expansion Shield for Arduino V7.1

Display screen
Gravity: I2C 16x2 Arduino LCD with RGB Backlight Display

RGB light
WS2812 RGB LED Module

Sample codes in need:

ds18b20

#include <OneWire.h>

#include <Wire.h>

#include "DFRobot_RGBLCD.h"

int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2

DFRobot_RGBLCD lcd(16,2);  //16 characters and 2 lines of show

//Temperature chip i/o

OneWire ds(DS18S20_Pin);  // on digital pin 2


void setup(void) {

  Serial.begin(9600);

   lcd.init();

    lcd.setRGB(0, 0, 255);

}


void loop(void) {

  float temperature = getTemp();

  Serial.println(temperature);

  delay(1000);

   lcd.setCursor(0,0); 

    lcd.print("Tep: ");

    lcd.print(temperature);

    delay(1000);

  

  delay(100); //just here to slow down the output so it is easier to read

  

}



float getTemp(){

  //returns the temperature from one DS18S20 in DEG Celsius


  byte data[12];

  byte addr[8];


  if ( !ds.search(addr)) {

      //no more sensors on chain, reset search

      ds.reset_search();

      return -1000;

  }


  if ( OneWire::crc8( addr, 7) != addr[7]) {

      Serial.println("CRC is not valid!");

      return -1000;

  }


  if ( addr[0] != 0x10 && addr[0] != 0x28) {

      Serial.print("Device is not recognized");

      return -1000;

  }


  ds.reset();

  ds.select(addr);

  ds.write(0x44,1); // start conversion, with parasite power on at the end


  byte present = ds.reset();

  ds.select(addr);    

  ds.write(0xBE); // Read Scratchpad


  

  for (int i = 0; i < 9; i++) { // we need 9 bytes

    data[i] = ds.read();

  }

  

  ds.reset_search();

  

  byte MSB = data[1];

  byte LSB = data[0];


  float tempRead = ((MSB << 8) | LSB); //using two's compliment

  float TemperatureSum = tempRead / 16;

  

  return TemperatureSum;

}



LM35_CW


#include <Wire.h>

#include "DFRobot_RGBLCD.h"



DFRobot_RGBLCD lcd(16,2);  //16 characters and 2 lines of show

void setup()

{

    

  Serial.begin(9600);//Set Baud Rate to 9600 bps

   lcd.init();

    lcd.setRGB(0, 0, 255);

}


void loop()

    uint16_t val;

    double dat;

    val=analogRead(A0);//Connect LM35 on Analog 0

    dat = (double) val * (5/10.24); 

    Serial.print("Tep:"); //Display the temperature on Serial monitor

    Serial.print(dat);

    Serial.println("C");

    delay(500);

    //lcd.print("hello!");

//delay(1000);

//lcd.clear();

 lcd.setCursor(0,0); 

    lcd.print("Tep: ");

    lcd.print(dat);  

   delay(1000);

  // lcd.clear(); 

    

}





AirQualityMonitor

#include <Wire.h>

#include "DFRobot_RGBLCD.h"

#include <Adafruit_NeoPixel.h>



char col;

unsigned int PMSa = 0, FMHDSa = 0, TPSa = 0, HDSa = 0, PMSb = 0, FMHDSb = 0, TPSb = 0, HDSb = 0;

unsigned int PMS = 0, FMHDS = 0, TPS = 0, HDS = 0, CR1 = 0, CR2 = 0;

unsigned int lcd_a = 0, lcd_b = 0, lcd_c = 0;

unsigned char buffer_RTT[40] = {}; //串口接收数据

unsigned long last_lcd_time = 0, last_light_time = 0;

#define PIN 8       //灯IO

#define NUMBER 2     //共有2个灯

char tempStr[15];

DFRobot_RGBLCD lcd(16, 2);  //RGB address; 16 characters and 2 lines of show

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMBER, PIN, NEO_GRB + NEO_KHZ800);   //灯参数



void setup()

{

  Serial.begin(9600);

  lcd.init();//初始化LCD1602 RGB屏

  strip.begin();    //启动RGB灯

  lcd.setRGB(0, 255, 0); //设置屏的初始背光颜色

  lcd.setCursor(0, 0 ); //设置从(0,0)开始显示

  lcd.print("T:");//显示T:

  lcd.setCursor(9, 0 );

  lcd.print("H:");

  lcd.setCursor(0, 1 );

  lcd.print("F:");

  lcd.setCursor(9, 1 );

  lcd.print("P:");

}


void loop()

{

  lcd_a = random(256);//读取随机值

  delayMicroseconds(10);//延迟,意在使得三个随机值没有去同一个(指随机值变化过再取,也有可能相同)

  lcd_b = random(256);

  delayMicroseconds(10);

  lcd_c = random(256);



  if (millis() - last_lcd_time > 30000) //每3秒变化一次显示屏背光(可能会出现与上一次相同)

  {

    lcd.setRGB(lcd_a, lcd_b, lcd_c); //设置背光颜色

    last_lcd_time = millis();//取系统时间

  }


  if (millis() - last_light_time > 500) //RGB灯亮的颜色,此处设置好了颜色,没有随机

  {

    strip.setPixelColor(0, strip.Color( 255, 227, 132) ); //0,为第一个RGB灯;255为R,227为G,132为B,可进行调整

    strip.setPixelColor(1, strip.Color( 255, 227, 132) );

    strip.show();//RGB使能控制,无这个函数,RGB灯不改变状态

    last_light_time = millis();//取系统时间

  }


  while (Serial.available() > 0) //检测是否有串口数据

  {

    for (int i = 0; i < 40; i++) //读取串口数据

    {

      col = Serial.read();

      buffer_RTT[i] = (char)col;

      delay(2);

    }


    Serial.flush();


    //lcd.clear();

    CR1 = (buffer_RTT[38] << 8) + buffer_RTT[39];

    CR2 = 0;

    for (int i = 0; i < 38; i++)

      CR2 += buffer_RTT[i];

    if (CR1 == CR2)               //校验

    {

      PMSa = buffer_RTT[12];      //读取PM2.5高八位数据

      PMSb = buffer_RTT[13];      //读取PM2.5低八位数据

      PMS = (PMSa << 8) + PMSb;   //PM2.5数据

      FMHDSa = buffer_RTT[28];    //读取甲醛高八位数据

      FMHDSb = buffer_RTT[29];    //读取甲醛低八位数据

      FMHDS = (FMHDSa << 8) + FMHDSb; //甲醛数据

      TPSa = buffer_RTT[30];      //读取温度高八位数据

      TPSb = buffer_RTT[31];      //读取温度低八位数据

      TPS = (TPSa << 8) + TPSb;   //温度数据

      HDSa = buffer_RTT[32];      //读取湿度高八位数据

      HDSb = buffer_RTT[33];      //读取湿度低八位数据

      HDS = (HDSa << 8) + HDSb;   //湿度数据

    }

    else

    {

      PMS = 0;

      FMHDS = 0;

      TPS = 0;

      HDS = 0;

    }

  }

  lcd.setCursor(2, 0 );

  sprintf(tempStr,"%d%d.%d",TPS/100,(TPS/10)%10,TPS%10); //显示温度 

  lcd.print(tempStr); 

  lcd.write(0xdf);              //显示°

  lcd.print('C');               //显示C

  lcd.setCursor(11, 0 );

  sprintf(tempStr,"%d%d.%d",HDS/100,(HDS/10)%10,HDS%10);   //显示湿度

  lcd.print(tempStr); 

  lcd.print('%');               //显示%

  lcd.setCursor(2, 1 );         //

  lcd.print((float)FMHDS/1000);        // 显示甲醛

  lcd.print((int)FMHDS%10);     // 显示甲醛最后一位小数,单位  mg/m³ 毫克每立方米

  lcd.setCursor(11, 1 );        //        

  sprintf(tempStr,"%d%d%d",PMS/100,(PMS/10)%10,PMS%10);  //显示PM2.5个、十、百位单位ug/m³ 微克每立方米

  lcd.print(tempStr);   

}


REVIEW