Temperature controlled servo

Hey, I got my Arduino starter kit from Sparkfun two weeks ago and I think I understand how the code works for the environment. I came up with an idea where I will use my Arduino to control my blinds with information from a temperature sensor. I tried writing the code but instead of running a servo, I decided to work with LEDs to see if the project will work. It worked perfectly and I decided to go ahead with the servo. The servo is responsive but the only problem now is that the servo keeps on running after the delay and it keeps turning the motor inside the servo and not the servo gear itself. I realised it was an issue because it was in ‘void loop’ and that it will constantly tell the motor to turn to that position even though it has already in that position.

I need some help in regards with telling the Arduino to not turn the servo if it’s already in that position. Not only that, every time the servo moves, the temperature reading from the sensor shoots up. I’ll put up my code so you guys can have a look and correct me. Thanks and sorry for the long post.

#include <Servo.h>

int temppin = 0; // Set temperature sensor in pin 0
int blueled = 11; // Set led in pin 11
int yellowled = 10; // Set led in pin 10
int redled = 9; // Set led in pin 9
int thresholdcold = 30; // Threshold for cold temperature
int thresholdhot = 32; // Threshold for hot temperature
int thresholdnormal = 31; // Threshold for normal temperature
int servocold = 179; // Angle in which servo will go to
int servohot = 0; // Angle in which servo will go to
int servonormal = 89; // Angle in which servo will go to
Servo servo1;

void setup()
{
Serial.begin(9600); // Begin communicating with computer
pinMode(blueled, OUTPUT); // Set all led pins as output
pinMode(yellowled, OUTPUT);
pinMode(redled, OUTPUT);
servo1.attach(3); // Attaches servo to specified pin

}

void loop()
{
int reading = analogRead(temppin); // Read the temperature sensor
float voltage = reading * 5.0; // Convert the reading according to voltage size
voltage /= 1024.0;
Serial.print(voltage); // Print out the voltage
Serial.println(“volts”); // Print “volts” to indicate that value is in volts
float temperatureC = (voltage - 0.5) * 100; // Convert the voltage to degrees celcius
Serial.print(temperatureC); // Print out the temperature
Serial.println(“degrees C”); // Print out “degrees C” to indicate value is in degrees celcius

if(temperatureC < thresholdcold) // If temperature is above the threshold, activate sequence
{
digitalWrite(blueled, HIGH);
digitalWrite(redled, LOW);
digitalWrite(yellowled, LOW);
servo1.write(servocold);
}

else if(temperatureC > thresholdhot) // If temperature is above the threshold, activate sequence
{
digitalWrite(redled, HIGH);
digitalWrite(blueled, LOW);
digitalWrite(yellowled, LOW);
servo1.write(servohot);
}

else if(temperatureC >= thresholdnormal) // If temperature is above the threshold, activate sequence
{
digitalWrite(yellowled, HIGH);
digitalWrite(redled, LOW);
digitalWrite(blueled, LOW);
servo1.write(servonormal);
}

delay(1000);

}

I had a bit of a play with your code, all i have changed is that i have added an int called previousPosition, this is updated to whatever you set the servo to, every time you change it. the code now checks if the next value it wants to assign the servo is the same as the last, if it is, it doesn’t change it.

#include <Servo.h>

int temppin = 0; // Set temperature sensor in pin 0
int blueled = 11; // Set led in pin 11
int yellowled = 10; // Set led in pin 10
int redled = 9; // Set led in pin 9
int thresholdcold = 30; // Threshold for cold temperature
int thresholdhot = 32; // Threshold for hot temperature
int thresholdnormal = 31; // Threshold for normal temperature
int servocold = 179; // Angle in which servo will go to
int servohot = 0; // Angle in which servo will go to
int servonormal = 89;  // Angle in which servo will go to
int previousPosition = 0;
Servo servo1;


void setup()
{
  Serial.begin(9600); // Begin communicating with computer
  pinMode(blueled, OUTPUT); // Set all led pins as output
  pinMode(yellowled, OUTPUT);
  pinMode(redled, OUTPUT);
  servo1.attach(3); // Attaches servo to specified pin
 
}

void loop()
{
  int reading = analogRead(temppin); // Read the temperature sensor
  float voltage = reading * 5.0; // Convert the reading according to voltage size
  //voltage = 1024.0;
  Serial.print(voltage); // Print out the voltage
  Serial.println("volts"); // Print "volts" to indicate that value is in volts
  float temperatureC = (voltage - 0.5) * 100; // Convert the voltage to degrees celcius
  Serial.print(temperatureC); // Print out the temperature
  Serial.println("degrees C"); // Print out "degrees C" to indicate value is in degrees celcius
 
 
  if(temperatureC < thresholdcold) // If temperature is above the threshold, activate sequence
  {
    digitalWrite(blueled, HIGH);
    digitalWrite(redled, LOW);
    digitalWrite(yellowled, LOW);
	if (servocold != previousPosition){
	    servo1.write(servocold);
		previousPosition = servocold;
		}
  }
 
  else if(temperatureC > thresholdhot) // If temperature is above the threshold, activate sequence
  {
    digitalWrite(redled, HIGH);
    digitalWrite(blueled, LOW);
    digitalWrite(yellowled, LOW);
		if (servohot != previousPosition){
	    servo1.write(servohot);
		previousPosition = servohot;
		}
  }
 
  else if(temperatureC >= thresholdnormal) // If temperature is above the threshold, activate sequence
  {
    digitalWrite(yellowled, HIGH);
    digitalWrite(redled, LOW);
    digitalWrite(blueled, LOW);
	if (servonormal != previousPosition){
	    servo1.write(servonormal);
		previousPosition = servonormal;
		}
  }
 
  delay(1000);
 
}

Thanks for the reply. I tried out the code and it works perfectly fine. The servo stopped spinning and that was what I was aiming for. Thanks heaps. :D

No problems, If you feel generous, theres a little green karma plus you can click :D

1 Like

hello navin.. can i know what type of temperature sensor that you use? thank you