Home » MH ET LIVE ESP32 MINI KIT and DS18b20 shield examples

MH ET LIVE ESP32 MINI KIT and DS18b20 shield examples

by shedboy71

The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements and has an alarm function with nonvolatile user-programmable upper and lower trigger points. The DS18B20 communicates over a 1-Wire bus that by definition requires only one data line (and ground) for communication with a central microprocessor. In addition, the DS18B20 can derive power directly from the data line (“parasite power”), eliminating the need for an external power supply.

Each DS18B20 has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-Wire bus. Thus, it is simple to use one microprocessor to control many DS18B20s distributed over a large area. Applications that can benefit from this feature include HVAC environmental controls, temperature monitoring systems inside buildings, equipment, or machinery, and process monitoring and control systems.

DS18B20: Block Diagram

Key Features

  • Unique 1-Wire® Interface Requires Only One Port Pin for Communication
  • Reduce Component Count with Integrated Temperature Sensor and EEPROM
    • Measures Temperatures from -55°C to +125°C (-67°F to +257°F)
    • ±0.5°C Accuracy from -10°C to +85°C
    • Programmable Resolution from 9 Bits to 12 Bits
    • No External Components Required
  • Parasitic Power Mode Requires Only 2 Pins for Operation (DQ and GND)
  • Simplifies Distributed Temperature-Sensing Applications with Multidrop Capability
    • Each Device Has a Unique 64-Bit Serial Code Stored in On-Board ROM
  • Flexible User-Definable Nonvolatile (NV) Alarm Settings with Alarm Search Command Identifies Devices with Temperatures Outside Programmed Limits
  • Available in 8-Pin SO (150 mils), 8-Pin µSOP, and 3-Pin TO-92 Packages

Here is what the DS18b20 shield looks like.
DS18B20 Shield for Wemos D1 mini DS18B20

Looking at the shield I could see that it used D2 as the input

 

Code

We already had an example but it used D4, this shield uses IO21 so it was easy to change this and test

[cpp]
#include <OneWire.h>

// OneWire DS18S20, DS18B20, DS1822 Temperature Example

OneWire ds(21); // on pin

void setup(void)
{
Serial.begin(9600);
}

void loop(void)
{
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;

if ( !ds.search(addr))
{
ds.reset_search();
delay(250);
return;
}

if (OneWire::crc8(addr, 7) != addr[7])
{
Serial.println(“CRC is not valid!”);
return;
}

// the first ROM byte indicates which chip
switch (addr[0])
{
case 0x10:
type_s = 1;
break;
case 0x28:
type_s = 0;
break;
case 0x22:
type_s = 0;
break;
default:
Serial.println(“Device is not a DS18x20 family device.”);
return;
}

ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000);
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

for ( i = 0; i < 9; i++)
{
data[i] = ds.read();
}

// Convert the data to actual temperature
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10)
{
raw = (raw & 0xFFF0) + 12 – data[6];
}
}
else
{
byte cfg = (data[4] & 0x60);
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

}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
Serial.print(” Temperature = “);
Serial.print(celsius);
Serial.print(” Celsius, “);
Serial.print(fahrenheit);
Serial.println(” Fahrenheit”);
}
[/cpp]

 

Testing

Open the serial monitor

Temperature = 22.69 Celsius, 72.84 Fahrenheit
Temperature = 22.69 Celsius, 72.84 Fahrenheit
Temperature = 22.87 Celsius, 73.18 Fahrenheit
Temperature = 24.44 Celsius, 75.99 Fahrenheit
Temperature = 25.50 Celsius, 77.90 Fahrenheit
Temperature = 26.19 Celsius, 79.14 Fahrenheit
Temperature = 26.69 Celsius, 80.04 Fahrenheit
Temperature = 27.12 Celsius, 80.82 Fahrenheit
Temperature = 27.44 Celsius, 81.39 Fahrenheit
Temperature = 27.69 Celsius, 81.84 Fahrenheit
Temperature = 27.94 Celsius, 82.29 Fahrenheit
Temperature = 28.06 Celsius, 82.51 Fahrenheit
Temperature = 28.25 Celsius, 82.85 Fahrenheit
Temperature = 28.37 Celsius, 83.07 Fahrenheit
Temperature = 28.44 Celsius, 83.19 Fahrenheit
Temperature = 27.94 Celsius, 82.29 Fahrenhei

 

Links

This comes in at only $2.20

Shield for WeMos D1 mini V2 DS18B20 Single-bus digital temperature and humidity sensor module sensor

You may also like

Leave a Comment

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.