Arduino - Temperature Sensor - Relay
This tutorial presents how to use an Arduino and a DS18B20 temperature sensor to control a relay. Specifically, the Arduino is programmed to continually measure the temperature using the DS18B20 sensor. The relay is then adjusted according to the following rules:
- If the temperature exceeds a predetermined threshold, the Arduino will turn the relay ON.
- If the temperature falls below the threshold, the Arduino will turn the relay OFF.
The relay then can connect to fan, heating element, cooling element or other things.
Hardware Required
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some links direct to products from our own brand, DIYables.
Buy Note: Many DS18B20 sensors on the market are low-quality. We highly recommend buying the sensor from the DIYables brand using the link above. We tested it, and it worked well.
About Relay and Temperature sensor
If you do not know about relay and temperature sensor (pinout, how it works, how to program ...), learn about them in the following tutorials:
Wiring Diagram
- Wiring diagram with breadboard
This image is created using Fritzing. Click to enlarge image
- Wiring diagram with adapter (recommended)
This image is created using Fritzing. Click to enlarge image
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.
Arduino Code - Temperature Sensor Triggers Relay
Quick Steps
- Do the wiring between Arduino, temperature sensor and relay as above diagram
- Connect Arduino to PC via USB cable
- Open Arduino IDE, select the right board and port
- Navigate to the Libraries icon on the left bar of the Arduino IDE.
- Search “DallasTemperature”, then find the DallasTemperature library by Miles Burton.
- Click Install button to install DallasTemperature library.
- You will be asked to install the library dependency
- Click Install All button to install OneWire library.
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino
- Change temperature around the temperature sensor
- See the change of relay
- See the result on Serial Monitor, It looks like below:
Arduino Code - Temperature Sensor Triggers Relay with Tolerance
While in operation, the temperature may experience fluctuations above or below the predetermined threshold, leading to frequent and sudden state changes on the relay, which can cause unforeseen operational issues. To address this problem, it is possible to include a margin of tolerance in the threshold. The code below demonstrates how this solution can be implemented:
The code presented above specifies a threshold of 20°C and a tolerance of 0.5°C. The following conditions apply:
- If the temperature is below 19.5°C, the relay is turned off.
- If the temperature is above 20.5°C, the relay is turned on.
- If the temperature is between 19.5°C and 20.5°C, the relay's state is kept unchanged.