Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Lesson 18: Temperature Sensor Module (DS18B20)
In this lesson, you will learn how to read temperature data from a DS18B20 temperature sensor module using an ESP32 Development Board. We’ll use the DallasTemperature library to interface with the sensor and display temperature readings in both Celsius and Fahrenheit units on the Serial Monitor.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Universal Maker Sensor Kit |
94 |
You can also buy them separately from the links below.
Component Introduction |
Purchase Link |
|---|---|
ESP32 & Development Board (ESP32 Board) |
|
- |
|
Wiring
Code
Note
To install the library, use the Arduino Library Manager and search for “DallasTemperature” and install it.
Code Analysis
Library inclusion
The inclusion of the OneWire and DallasTemperature libraries allows communication with the DS18B20 sensor.
Note
To install the library, use the Arduino Library Manager and search for “DallasTemperature” and install it.
#include <OneWire.h> #include <DallasTemperature.h>
Defining the sensor data pin
The DS18B20 is connected to digital pin 25 of the Arduino.
#define ONE_WIRE_BUS 25Initializing the sensor
The OneWire instance and DallasTemperature object are created and initialized.
OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire);
Setup function
The
setup()function initializes the sensor and sets up serial communication.void setup(void) { sensors.begin(); // Start up the library Serial.begin(9600); }
Main loop
In the
loop()function, the program requests temperature readings and prints them in both Celsius and Fahrenheit.void loop(void) { sensors.requestTemperatures(); Serial.print("Temperature: "); Serial.print(sensors.getTempCByIndex(0)); Serial.print("℃ | "); Serial.print((sensors.getTempCByIndex(0) * 9.0) / 5.0 + 32.0); Serial.println("℉"); delay(500); }