Lesson 23 Digital Temperature Sensor

Share for us
Share on facebook
Share on twitter
Share on pinterest
Share on whatsapp

Introduction

Temperature Sensor DS18B20 is a commonly used digital temperature sensor featured with small size, low-cost hardware, strong anti-interference capability and high precision. The digital temperature sensor is easy to wire and can be applied a various occasions after packaging. Different from conventional AD collection temperature sensors, it uses a 1-wire bus and can directly output temperature data.

Components

– 1 * SunFounder Uno board

– 1 * USB data cable

– 1 * DS18B20 Temperature Sensor module

– 1 * I2C LCD1602

– 1 * 3-Pin anti-reverse cable

– 1 * 4-Pin anti-reverse cable

– 1 * Dupont wire (F to F)

Experimental Principle

With a unique single-wire interface, DS18B20 requires only one pin for a two-way communication with a microprocessor. It supports multi-point networking to measure multi-point temperatures. Eight DS18B20s can be connected at most, because too many of them will consume too much of the power supply and cause low voltage thus instability of signal transmission. The schematic diagram is as follows:

Experimental Procedures

Step 1: Build the circuit               

Step 2: Program (Please refer to the example code in LEARN -> Get Tutorial on our website)

Note: Here you need to add a library. Refer to the description in Lesson 1 previously in the manual.

Step 3: Compile

Step 4: Upload the sketch to SunFounder Uno board

Now, you can see the value of current temperature displayed on the LCD.

 Code

/****************************************************
name:Digital Temperature Sensor-ds18b20
function:you can see the value of current temperature displayed on the LCD.
****************************************************/
//Email:support@sunfounder.com
//website:www.sunfounder.com/****************************************/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27,16,2);// set the LCD address to 0x27 for a 16 chars and 2 line display#define ONE_WIRE_BUS 7 //ds18b20 module attach to pin7
/* Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) */
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);void setup(void)
{  // start serial port
  Serial.begin(9600);
  sensors.begin(); // initialize the bus
  lcd.init(); //initialize the lcd
  lcd.backlight(); //turn on the backlight
}
void loop(void)
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  //Serial.print(“Requesting temperatures…”);
  sensors.requestTemperatures(); // Send the command to get temperatures
  lcd.setCursor(0, 0);
  lcd.print(“TemC: “); //print “Tem: ” on lcd1602
  lcd.print(sensors.getTempCByIndex(0));//print the temperature on lcd1602
  //Serial.print(“Tem: “);
  //Serial.print(sensors.getTempCByIndex(0));
  //Serial.println(” C”);
  lcd.print(char(223));//print the unit” ℃ “
  lcd.print(“C”);
  lcd.setCursor(0, 1);
  lcd.print(“TemF: “);
  lcd.print(1.8*sensors.getTempCByIndex(0) + 32.0);//print the temperature on lcd1602
  lcd.print(char(223));//print the unit” ℉ “
  lcd.print(” F”);
  //Serial.print(“Tem: “);
  //Serial.print(1.8*sensors.getTempCByIndex(0) + 32.0);
  //Serial.println(” F”);
  //Serial.println(“”);
  //Serial.print(“Temperature for the device 1 (index 0) is: “);
  //Serial.println(sensors.getTempCByIndex(0)); //print the temperature on serial monitor
}