The DS18B20 is a temperature sensor that can be used in various simple projects. This part uses the 1 wire (I2C) bus and you can connect multiple sensors up to your Arduino.
The part is also relatively low cost and only requires an additional 4k7 pull up resistor. In the example below we shall make a basic example that reads the temperature and outputs via serial and can be verified using the serial monitor in the Arduino IDE.
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.
Lets look at the parts list
Parts Required
Here are the parts I used
Name | Link | |
ESP32 | ||
DS18B20 |
|
|
Connecting cables |
Layout
As always be careful not to get the connections incorrect, you can refer to the pinout for the device below to help . The DS18B20 can be powered by between 3.0V and 5.5V so you can simply connect its GND pin to 0V and the VDD pin to +5V from the Wemos Mini
Here is the connection diagram showing how to connect your wemos to the resistor and sensor.
Schematic
Code
This needs the onewire library installed
#include <OneWire.h> // OneWire DS18S20, DS18B20, DS1822 Temperature Example OneWire ds(A13); // on pin D4 (a 4.7K resistor is necessary) 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; } //Serial.println(); // 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"); }
Output
Open the serial monitor and you should see something like this
Temperature = 20.62 Celsius, 69.12 Fahrenheit
Temperature = 20.81 Celsius, 69.46 Fahrenheit
Temperature = 22.12 Celsius, 71.82 Fahrenheit
Temperature = 23.12 Celsius, 73.62 Fahrenheit
Temperature = 23.87 Celsius, 74.97 Fahrenheit
Temperature = 24.44 Celsius, 75.99 Fahrenheit
Temperature = 24.81 Celsius, 76.66 Fahrenheit
Temperature = 25.19 Celsius, 77.34 Fahrenheit
Temperature = 25.44 Celsius, 77.79 Fahrenheit
Temperature = 25.75 Celsius, 78.35 Fahrenheit
Temperature = 25.94 Celsius, 78.69 Fahrenheit