Arduino - Temperature Sensor - Servo Motor
This tutorial presents how to use an Arduino and a DS18B20 temperature sensor to control a servo motor. Specifically, the Arduino is programmed to continually measure the temperature using the DS18B20 sensor. The servo motor is then adjusted according to the following rules:
- If the temperature exceeds a predetermined threshold, the Arduino will rotate the servo motor to 90 degrees.
- If the temperature falls below the threshold, the Arduino will rotate the servo motor back to 0 degrees.
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 Servo Motor and Temperature sensor
If you do not know about servo motor 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 Servo Motor
Quick Steps
- Do the wiring between Arduino, temperature sensor and servo motor 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 servo motor
- See the result on Serial Monitor, It looks like below:
Arduino Code - Temperature Sensor Triggers Servo Motor with Tolerance
During operation, the temperature may fluctuate above or below the threshold, causing rapid and constant adjustments to the servo motor. This can result in vibration instead of smooth rotation to the desired angle. To mitigate this issue, a tolerance can be incorporated into the threshold. The following code illustrates how to implement this solution:
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 servo motor angle is adjusted to 0°.
- If the temperature is above 20.5°C, the servo motor angle is adjusted to 90°.
- If the temperature is between 19.5°C and 20.5°C, no adjustment is made to the servo motor angle.