Hi guys,
I've a problem on a project with two servo motors and a temperature sensor.
I'm trying to move two servo (and light up two LED) on different angles depending on different temperature.
When the servo are not attached the sensor works well even with the LED but when I attached the two servo motors, and the sensor detects the higher temperature request it starts to show wrong temperatures till 35-40 degrees. I've also difficult to move the two servo on different angles (one opposite to the other).
I hope some one can help me!
That's the code Thank you!
#include <Servo.h>
int temppin = A0; // Set temperature sensor in pin 0
int redled1 = 12; // Set led in pin 10
int redled2 = 13; // Set led in pin 9
int thresholdcold = 22; // Threshold for cold temperature
int thresholdhot = 28; // Threshold for hot temperature
int thresholdnormal = 24; // Threshold for normal temperature
int servocold = 179; // Angle in which servo will go to
int servohot = 1; // Angle in which servo will go to
int servonormal = 89; // Angle in which servo will go to
int previousPosition = 0;
Servo servo1;
Servo servo2;
void setup()
{
Serial.begin(9600); // Begin communicating with computer
pinMode(redled1, OUTPUT);
pinMode(redled2, OUTPUT);
servo1.attach(2); // Attaches servo to specified pin
servo2.attach(3); // Attaches servo to specified pin
}
void loop()
{
int reading = analogRead(temppin); // Read the temperature sensor
float voltage = (reading/1024.0) * 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(redled1, LOW);
digitalWrite(redled2, LOW);
if (servocold != previousPosition){
servo1.write(servocold);
servo2.write(servocold);
previousPosition = servocold;
}
}
else if(temperatureC > thresholdhot) // If temperature is above the threshold, activate sequence
{
digitalWrite(redled1, HIGH);
digitalWrite(redled2, HIGH);
if (servohot != previousPosition){
servo1.write(servohot);
servo2.write(servohot);
previousPosition = servohot;
}
}
else if(temperatureC >= thresholdnormal) // If temperature is above the threshold, activate sequence
{
digitalWrite(redled1, HIGH);
digitalWrite(redled2, LOW);
if (servonormal != previousPosition){
servo1.write(servonormal);
servo2.write(servonormal);
previousPosition = servonormal;
}
}
delay(1000);
}
Hi guys,
I've a problem on a project with two servo motors and a temperature sensor.
I'm trying to move two servo (and light up two LED) on different angles depending on different temperature.
When the servo are not attached the sensor works well even with the LED but when I attached the two servo motors, and the sensor detects the higher temperature request it starts to show wrong temperatures till 35-40 degrees. I've also difficult to move the two servo on different angles (one opposite to the other).
I hope some one can help me!
That's the code
#include <Servo.h>
int temppin = A0; // Set temperature sensor in pin 0
int redled1 = 12; // Set led in pin 10
int redled2 = 13; // Set led in pin 9
int thresholdcold = 22; // Threshold for cold temperature
int thresholdhot = 28; // Threshold for hot temperature
int thresholdnormal = 24; // Threshold for normal temperature
int servocold = 179; // Angle in which servo will go to
int servohot = 1; // Angle in which servo will go to
int servonormal = 89; // Angle in which servo will go to
int previousPosition = 0;
Servo servo1;
Servo servo2;
void setup()
{
Serial.begin(9600); // Begin communicating with computer
pinMode(redled1, OUTPUT);
pinMode(redled2, OUTPUT);
servo1.attach(2); // Attaches servo to specified pin
servo2.attach(3); // Attaches servo to specified pin
}
void loop()
{
int reading = analogRead(temppin); // Read the temperature sensor
float voltage = (reading/1024.0) * 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(redled1, LOW);
digitalWrite(redled2, LOW);
if (servocold != previousPosition){
servo1.write(servocold);
servo2.write(servocold);
previousPosition = servocold;
}
}
else if(temperatureC > thresholdhot) // If temperature is above the threshold, activate sequence
{
digitalWrite(redled1, HIGH);
digitalWrite(redled2, HIGH);
if (servohot != previousPosition){
servo1.write(servohot);
servo2.write(servohot);
previousPosition = servohot;
}
}
else if(temperatureC >= thresholdnormal) // If temperature is above the threshold, activate sequence
{
digitalWrite(redled1, HIGH);
digitalWrite(redled2, LOW);
if (servonormal != previousPosition){
servo1.write(servonormal);
servo2.write(servonormal);
previousPosition = servonormal;
}
}
delay(1000);
}
If you are drawing their power from the Arduino board (a) you will probably damage the board and (b) they will probably affect the voltage which may be the cause of the temperature problem.
The servo are powered directly form Arduino that is connected by Usb, how do you suggest to power the Servo?
Is it good connecting Arduino with an 9v battery or is better using an external 6v battery directly on the breadboard or to the vin and ground pin? I'm using to servo that work with 4.8v.
The servo are powered directly form Arduino that is connected by Usb, how do you suggest to power the Servo?
Is it good connecting Arduino with an 9v battery or is better using an external 6v battery directly on the breadboard or to the vin and ground pin? I'm using to servo that work with 4.8v.
I power servos from a 4-AA cell pack and Arduino from USB, usually. My servos work from 4.8 to 6V. 9V batteries (transistor batteries we used to call them) will not provide enough current for servos, but will power Arduino through Vin or the power jack. although the 9V batteries won't last a long time.
Servos can use a lot more power than an Arduino board or USB connection can produce.
Use a separate power supply (4.5v to 6v) connected to the power and ground of the servo. Connect the servo signal to the Arduino pin AND the servo ground to the Arduino ground pin.
Yeah the problem was exactly as you described, I could't get any battery today but I solve the problem as well using a 100uf capacitor between power and ground. Thank you!
Do you have also any suggestion on how to move the servos on opposite direction, i cannot get how to write the code to make them work in this way!
Thank you! I could't get any battery today but I solve the problem as well using a 100uf capacitor between power and ground. Tomorrow I would try make them work with AA batteries as well.
But i still have problems, I cannot get how to write the code to make them turn in opposite direction, Do you have any suggestion about?
Thank you!
I don't know what you mean by "opposite directions".
Perhaps it is like this - servo.write(45) moves it to (say) the left-hand 45 degree position but you really want it in the right-hand 45 degree position - which really requires servo.write(135). I think you get that effect with servo.write(180 - myPosition).
Sorry I didn't get I was sending two times the post.
By the way I solve the problem, i post the code for anyone that could need it.
Thank you for your help!
int temppin = A0; // Set temperature sensor in pin 0
int redled1 = 12; // Set led in pin 10
int redled2 = 13; // Set led in pin 9
int thresholdcold = 21; // Threshold for cold temperature
int thresholdhot = 23; // Threshold for hot temperature
int thresholdnormal = 22; // Threshold for normal temperature
int servocold = 89; // Angle in which servo will go to
int servo1hot = 29;
int servo2hot = 169;// Angle in which servo will go to
int servonormal = 129; // Angle in which servo will go to
int previousPosition = 0;
Servo servo1;
Servo servo2;
void setup()
{
Serial.begin(9600); // Begin communicating with computer
pinMode(redled1, OUTPUT);
pinMode(redled2, OUTPUT);
servo1.attach(3); // Attaches servo to specified pin
servo2.attach(5); // Attaches servo to specified pin
}
void loop()
{
int reading = analogRead(temppin); // Read the temperature sensor
float voltage = (reading/1024.0) * 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(redled1, LOW);
digitalWrite(redled2, LOW);
if (servocold != previousPosition){
servo1.write(servocold);
servo2.write(servocold);
previousPosition = servocold;
}
}
else if(temperatureC > thresholdhot) // If temperature is above the threshold, activate sequence
{
digitalWrite(redled1, HIGH);
digitalWrite(redled2, HIGH);
if (servo1hot != previousPosition){
servo1.write(servo1hot);
servo2.write(servo2hot);
previousPosition = servo1hot;
}
}
else if(temperatureC >= thresholdnormal) // If temperature is above the threshold, activate sequence
{
digitalWrite(redled1, HIGH);
digitalWrite(redled2, LOW);
if (servonormal != previousPosition){
servo1.write(servonormal);
servo2.write(servocold);
previousPosition = servonormal;
}
}
delay(1000);
}