Temperature sensors DS18B20. Part 12.

Temperature sensors DS18B20

We have connected almost all except the most important temperature sensors. The project uses digital sensors DS18B20. Digital - for us this means that they provide a fairly good accuracy and range of the connection. Wire length can be up to 100m. I used to connect the shielded wire 4 x 0.22mm2 For those who do not know how to solder have the option to buy a ready-sensor in a sleeve made of stainless steel and ready to connect. For those who are not looking for easy ways you can buy sensor in the TO-92 case and solder wires to it yourself. I can say that the ordering on aliexpress of 5 ready sensors usually comes out cheaper than buying a separate sensors and tinker with soldering. It all depends on your desire and time.

Stainless steel DS18b20

Stainless steel DS18b20

In fact, all the sensors can be put on a "common" wire, but for reliability, I used a separate cable for each sensor. The sensor connection is quite simple. It is necessary to join a + 5V, GND, and connect the output to the input of arduino. But the sensor will not work :) For proper operation we must also "short-circuit" the sensor output, via a resistor 4.7kOHm to + 5V.

Table of conformity between DS18b20 pins and Arduino:

T3, DS18B20 pins Arduino pins

Wiring diagram for a single sensor T3:

Arduino DS18B20 1 sensor

Connecting of one sensor DS18B20

All other sensors will be connected in the same way:

Arduino DS18B20 4 sensors

Connecting of four sensors DS18B20

The scheme may seem daunting, it is actually simple. All VCC contacts are connected to + 5V, all GND contacts are connected to GND. Outputs of sensors should be connected to the 8, 7, 6 and 5 contacts of Arduino, while between each output terminal and + 5V we have to put resistor 4.7K

Real view:

Arduino DS18B20 sensor, real view

Overall view of connection of one sensor DS18B20

Arduino DS18B20 sensor, real view

DS18B20 sensor connection without soldering - just for test!

Arduino DS18B20 sensor, real view

Connecting a resistor 4.7K without soldering

It's time to download the test sketch and check that the temperature sensor is correctly connected to arduino! First, it should find the sensor, then start to show current temperature on the LCD display.

// include the library code:
#include <LiquidCrystal.h>
#include <OneWire.h>

#define PWM_LED_PIN 3 
#define PIN_LCD_RS 39
#define PIN_LCD_EN 41
#define PIN_LCD_D4 43
#define PIN_LCD_D5 45
#define PIN_LCD_D6 47
#define PIN_LCD_D7 49
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd( PIN_LCD_RS, PIN_LCD_EN, PIN_LCD_D4, PIN_LCD_D5, PIN_LCD_D6, PIN_LCD_D7 );
OneWire  ds(5);

byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius;
void setup() {   
  analogWrite( PWM_LED_PIN, 255 );
  lcd.begin(16, 2); 
  lcd.print("House4u sol.proj");   
  lcd.setCursor(0, 1); 
  if ( !ds.search(addr)) 
    lcd.print("No more addr...");   
  if (OneWire::crc8(addr, 7) != addr[7]) 
      lcd.print("CRC error!");
      delay( 1000 );
  // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      lcd.print("Chip = DS18S20");  // or old DS1820
      type_s = 1;
    case 0x28:
      lcd.print("Chip = DS18B20");
      type_s = 0;
    case 0x22:
      lcd.print("Chip = DS1822");
      type_s = 0;
      lcd.print("Unknown device.");
  delay( 2000 );
  lcd.setCursor(0, 1); 
  lcd.print("                ");

void loop() 
  lcd.setCursor(0, 1);     
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  delay(1000);     // maybe 750ms is enough, maybe not
  present = ds.reset();
  ds.write(0xBE);         // Read Scratchpad
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();    
  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
  } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  celsius = (float)raw / 16.0;

If we are all connected correctly, you should see the temperature:

Result output of DS18B20

Result output

<< BackNext >>

Share with friends: