ESP32 - Temperature Sensor
There are many type of temperature sensors can works with ESP32 such as LM35, TH02, HDC1000 or HTS221... In this tutorial, we are going to learn how to use waterproof DS18B20 temperature sensor. This sensor is inexpensive, easy to use and look neat.
|1||×||ESP-WROOM-32 Dev Module|
|1||×||Micro USB Cable|
|1||×||Temperature Sensor DS18B20|
|1||×||4.7 kΩ resistor|
About One Wire Temperature Sensor - DS18B20
DS18B20 temperature sensor has three pins:
- GND pin needs to be connected to GND (0V)
- VCC pin needs to be connected to VCC (5V or 3.3V)
- DQ pin is 1-Wire Data Bus should be connected to a digital pin on ESP32.
The sensor usually has two forms: TO-92 package (looks like a transistor) and waterproof probe. We use the waterproof probe form in this tutorial.
How To Program For DS18B20 Temperature Sensor
- Include the library:
- Declare OneWire and DallasTemperature object corresponding to the pin connected to sensor's DQ pin
- Initialize the sensor:
- Send the command to get temperatures:
- Read temperature in Celsius:
- (Optional) Convert Celsius to Fahrenheit:
- If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
- Connect the ESP32 board to your PC via a micro USB cable
- Open Arduino IDE, select the right ESP32 board and COM port
- On Arduino IDE, Go to Tools Manage Libraries
- Search “OneWire”, then find the OneWire library by Paul Stoffregen
- Click Install button to install OneWire library.
- Search “Dallas”, then find the DallasTemperature library by Miles Burton.
- Click Install button to install DallasTemperature library.
- Copy the above code and paste it to Arduino IDE
- Click Upload button on Arduino IDE to compile and upload code to ESP32 board
- Put the sensor on hot and cold water, or grasp the sensor by your hand
- See the result on Serial Monitor. It looks like the below:.
We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.
Temperature Sensor on Commercial Products
※ NOTE THAT:
Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.