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 with Arduino. 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||×||(Optional) 9V Power Adapter for Arduino|
|1||×||(Optional) Screw Terminal Block Shield 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)
- DATA 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.
Connecting a DS18B20 temperature sensor to an Arduino requires a pull-up resistor, which can be a hassle. However, some manufacturers simplify the process by offering a wiring adapter that has a built-in pull-up resistor and a screw terminal block, making it much easier.
- Wiring diagram with breadboard
- Wiring diagram with adapter (recommended)
- Real wiring diagram with adapter
We suggest purchasing a DS18B20 sensor that comes with a wiring adapter for easy connection. The adapter has a built-in resistor, eliminating the need for a separate one in the wiring.
How To Program For DS18B20 Temperature Sensor
- Include the library:
- Declare OneWire and DallasTemperature object corresponding to the pin connected to sensor's DATA 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.