Arduino - Temperature Sensor
There are many type of temperature sensors can works with Arduino 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||×||Arduino UNO or Genuino UNO|
|1||×||USB 2.0 cable type A/B|
|1||×||Temperature Sensor DS18B20|
|1||×||4.7 kΩ resistor|
|1||×||(Optional) 9V Power Adapter for Arduino|
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. It should be connected to a digital pin on Arduino.
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.
- With breadboard
- Without breadboard
- Real wiring
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:
- Connect Arduino to PC via USB cable
- Open Arduino IDE, select the right board and 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 open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino
- Put the sensor on hot and cold water, or grasp the sensor by your hand
- See the result on Serial Monitor.
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.