Using Temperature sensor with servo motor (20kg powerHD)

Hi, I am building a greenhouse window opener for my small greenhouse. I have got the project working but the servo operates too fast. I have tried to combine a increment angle code but can’t get the temp code and the able code to work together. can anyone help. Thank you.

This is the code I am using:

#include <Servo.h> //servo library

float tempC; //temperature sensor LM35
int tempPin = 0; //analog data output

int ledPin1 = 9; //servo on indicator LED
int ledPin2 = 8; //servo off indicator LED

Servo servo1; // create servo object to control a servo

int pos = 0; // variable to store the servo position

void setup() {
Serial.begin (9600);
servo1.attach(3); // attaches the servo on arduino digital pin 3 to the servo object

pinMode (ledPin1, OUTPUT); //pin output mode for servo on
pinMode (ledPin2, OUTPUT); //pin output mode for servo off

void loop() {

tempC = analogRead (tempPin); //read the output of temperature sensor LM35
tempC = (5.0tempC100.0)/1024.0; //calculation to convert voltage to temperature reading
//tempC = (tempC/79.0)*5.0;
Serial.print ("Temperature "); //print the word temperature on the serial monitor
Serial.println (tempC); //print the temperature reading on the serial monitor
delay (3000); //delay 30 secondsbetween readings

if (tempC >25) //if temperature greater than
pos = 0; pos = 20; pos = 40; //position of the servo motor arm
delay (500);

digitalWrite (ledPin1, HIGH); //turn on the green LED to indicate the servo motor is in use
servo1.write (pos); //move the servo arm to the position indicated above

else //else statement
pos = 0; //position of the servo motor arm
digitalWrite (ledPin1, LOW); //turn off the green LED
servo1.write (pos); //move the servo arm to the position indicated above
//delay (100);
//digitalWrite (tranSwitch, LOW); //turn off the switch to break the circuit

if (tempC <25) //if temperature less than
digitalWrite (ledPin2, HIGH); //turn on white LED to indicate to indicate servo motor is not in use
digitalWrite (ledPin2, LOW); //turn off the white LED

I have got the project working but the servo operates too fast.

Study the Servo Sweep example that comes with the Arduino IDE.


there is a library called controlled Servo which offers limiting the speed of the servos.

some additional ideas just because they came to my mind
If your high-power-servo can do the job when the turning-speed is slowed down that's OK
another mechanical solution is to use a stepper-motor in combination with a trapezoidal lead screw
or a simple geared DC-motor with a rope drum
of course these solutions would need other parts to buy

best regards Stefan
any newbee can apply the most professional habit from the first line of code they write on their own:
add only ONE thing at a time. Test/debug that ONE thing until that ONE thing works reliable - repeat.
The sad thing is: only the REAL professionals write code this way.

Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

Just for interest why do you set pos to 0 then to 20 then to 40 without using the 0 and 20 values for anything?

Try using VarSpeedServo.h instead of the standard Servo.h library. It has a speed parameter on the write() command.