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:

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×DS18B20 Temperature Sensor (WITH Adapter)
1×DS18B20 Temperature Sensor (WITHOUT Adapter)
1×Servo Motor
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Recommended) Breadboard Shield For Arduino Uno
1×(Recommended) Enclosure For Arduino Uno

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

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
Arduino DS18B20 servo motor wiring diagram

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram with adapter (recommended)
Arduino temperature sensor servo motor wiring diagram

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

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-temperature-sensor-servo-motor */ #include <Servo.h> #include <OneWire.h> #include <DallasTemperature.h> const int SERVO_PIN = 5; // Arduino pin connected to Servo Motor's pin const int SENSOR_PIN = 2; // Arduino pin connected to DS18B20 sensor's DATA pin const float TEMPERATURE_THRESHOLD = 20; // °C Servo servo; // create servo object to control a servo OneWire oneWire(SENSOR_PIN); // setup a oneWire instance DallasTemperature sensor(&oneWire); // pass oneWire to DallasTemperature library float temperature; int angle; void setup() { Serial.begin(9600); // initialize serial servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(0); sensor.begin(); // initialize the sensor } void loop() { sensor.requestTemperatures(); // send the command to get temperatures temperature = sensor.getTempCByIndex(0); // read temperature in Celsius if (temperature > TEMPERATURE_THRESHOLD) angle = 90; // set angle to 90 degree else angle = 0; // set angle to 0 degree servo.write(angle); // rotate servo motor // print to serial Serial.print("Temperature: "); Serial.print(temperature); Serial.print("°C => servo angle: "); Serial.println(angle); }

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.
Arduino Dallas Temperature library
  • You will be asked to install the library dependency
  • Click Install All button to install OneWire library.
Arduino 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:
COM6
Send
Temperature: 19.2°C => servo angle: 0 Temperature: 19.5°C => servo angle: 0 Temperature: 19.6°C => servo angle: 0 Temperature: 19.9°C => servo angle: 0 Temperature: 20.2°C => servo angle: 90 Temperature: 20.7°C => servo angle: 90 Temperature: 21.3°C => servo angle: 90
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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:

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-temperature-sensor-servo-motor */ #include <Servo.h> #include <OneWire.h> #include <DallasTemperature.h> const int SERVO_PIN = 5; // Arduino pin connected to Servo Motor's pin const int SENSOR_PIN = 2; // Arduino pin connected to DS18B20 sensor's DATA pin const float TEMPERATURE_THRESHOLD = 20; // °C const float TOLERANCE = 0.5; // °C Servo servo; // create servo object to control a servo OneWire oneWire(SENSOR_PIN); // setup a oneWire instance DallasTemperature sensor(&oneWire); // pass oneWire to DallasTemperature library float temperature; int angle; void setup() { Serial.begin(9600); // initialize serial servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(0); sensor.begin(); // initialize the sensor } void loop() { sensor.requestTemperatures(); // send the command to get temperatures temperature = sensor.getTempCByIndex(0); // read temperature in Celsius if (temperature > (TEMPERATURE_THRESHOLD + TOLERANCE)) angle = 90; // set angle to 90 degree else if (temperature < (TEMPERATURE_THRESHOLD - TOLERANCE)) angle = 0; // set angle to 0 degree servo.write(angle); // rotate servo motor // print to serial Serial.print("Temperature: "); Serial.print(temperature); Serial.print("°C => servo angle: "); Serial.println(angle); }

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.

The Best Arduino Starter Kit

※ OUR MESSAGES