I've got some issue with a circuit including a led, a button and 2 servos
I have 3 EMAX ES08D (4.8V - 6V) digital servos and 1 TowerPro SG92R servo.
When I try the towerpro with another servo. The towerpro works fine, the other one do nothing. When I try 2 EMAX servos, one is buzzing and the other one does nothing.
I checked each servo alone (towerpro and emax) with the sweep sketch. I even modified the sweep sketch to sweep 2 EMAX servos. That worked. But I can't succeed to make the LED + button + servos work. I tried using a external power supply for the servos, that didn't work either. Sometimes, the emax servos is buzzing, then stop and can heat a bit. The ground of the arduino is connected to the breadboard, so is the ground of the 6V power supply.
Do I have to buy analog servos?
I join pics of the circuit and the code. Maybe you can help me to find my mistakes.
I'm quite desperate as I know that should work but can't find the reason why it doesn't. I'll be really thankful for any help you could provide!
#include <Servo.h>
//servo 1
Servo myservo;
Servo myservo1;
int val; // variable for reading the pin status
int val2; // variable for reading the delayed/debounced status
int buttonState;
int pos = 0;
int pos1 = 180;
int servostatus = 0;
int switchPin =2; // Switch connected to digital pin 2
int ledPin = 5;
int ledPin2 = 18;
void setup() // run once, when the sketch starts
{
//servo 1
myservo.attach(9);
myservo1.attach(10);
pinMode(switchPin, INPUT);
pinMode(ledPin, OUTPUT);
buttonState = digitalRead(switchPin);
myservo.write(0);
myservo1.write(175);
pinMode(ledPin2, OUTPUT);
}
void loop() // run over and over again
//servo 1
{
val = digitalRead(switchPin); // read input value and store it in val
delay(10); // 10 milliseconds is a good amount of time
val2 = digitalRead(switchPin); // read the input again to check for bounces
if (val == val2) { // make sure we got 2 consistant readings!
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
if (servostatus == 0) { // is the light off?
servostatus = 1; // turn light on!
myservo.write(0);
myservo1.write(180);
delay(1000);
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
delay(00);
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
delay(00);
// fading
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
delay(30);
}
} else {
servostatus = 0; // turn light off!
digitalWrite(ledPin, LOW);
delay(15);
digitalWrite(ledPin2, LOW);
myservo.write(180);
myservo1.write(0);
}
}
}
buttonState = val; // save the new state in our variable
}
}
It is hard to tell how everything is connected.
Do you use pin 0 and pin 1 ? Those pins are used to upload a sketch and for the serial monitor.
You power the Arduino with the USB ? That is okay.
You power the servos with 4 AA batteries ? That is not okay. You need a good power supply, for example 5V 2A (5V 1A should also be okay).
If a servo starts moving, it could require a current between 0.5A and 1A. With two servos active at the same time, you need twice the current.
Ground of the arduino is on the blue rail of the breadboard.
The arduino is indeed powered by USB.
The servos are, as you said, powered by 4 AA batteries, so 6V for 5A. Servos can work up to 6V, and I've enough amp to run them both. I first tried to power the servos with the arduino as it works with the sweep sketch, but couldn't manage to make it work so I've been advised to power the servos with something else.
It's still a prototype, no load on the servo or anything.
I don't trust those batteries.
Do you assume they can provide 5A ? If only one of them is weak, the current could be a lot less than 1A.
Could you try a good power supply for the servos ?
Is the GND of the Arduino also connected to the ground of the servos (and the '-' of the batteries) ?
I checked the battery with a multimeter. 6V and a bit more than 5A.
On the blue rail I've the ground of the battery, the ground of the 2 servos, the ground of the arduino, the ground of the led and the ground of the button.
Sadly, I haven't a "good" power supply.
I don't understand why the only servo that works is the cheap one from arduino kit.
I could run the two emax servo from the external power supply with the SWEEP sketch, they ran normally back and forth between 0 and 180° (more or less). But if I try with the led and button, the emax refuse to work. Sometimes they're buzzing for a few seconds then stop (and may heat a bit).
If I only try the button and the led, that works like a charm. The led flickers then fade in. If I try the button, the led and the Tower Pro, also works like a charm. I push the button, the servo activates, the led flickers and fade in. I click a second time, the led shuts down and the servo activates in the opposite way.
So, the code is good (no doubt on that), the button works, the led works and the Tower Pro servo works. But, despite the emax servos work individually or two at a time with the sweep function, they don't work with this circuit. Can't find out why.
Servo test code that you can use to test several servos at once using the serial monitor.
//zoomkat 11-22-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added
String readString;
#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod; // create servo object to control a servo
void setup() {
Serial.begin(9600);
//myservoa.writeMicroseconds(1500); //set initial servo position if desired
myservoa.attach(6); //the pin for the servoa control
myservob.attach(7); //the pin for the servob control
myservoc.attach(8); //the pin for the servoc control
myservod.attach(9); //the pin for the servod control
Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}
void loop() {
//expect single strings like 700a, or 1500c, or 2000d,
//or like 30c, or 90a, or 180d,
//or combined like 30c,180b,70a,120d,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
if (readString.length() >1) {
Serial.println(readString); //prints string to serial port out
int n = readString.toInt(); //convert readString into a number
// auto select appropriate value, copied from someone elses code.
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.write(n);
if(readString.indexOf('b') >0) myservob.write(n);
if(readString.indexOf('c') >0) myservoc.write(n);
if(readString.indexOf('d') >0) myservod.write(n);
}
readString=""; //clears variable for new input
}
}
else {
readString += c; //makes the string readString
}
}
}
DevilFlash:
I tried the test code but it doesn't write anything except "multi-servo-delimit-test-dual-input-11-22-12".
I powered the 4 servos with the battery or the arduino. Also tried 2 servos, same thing.
Do I have to send numbers through the serial monitor?
Clue:
//zoomkat 11-22-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added
There is a 220 Ohm resistor from LED ground to blue rail. 3V white led for 6V power supply. Seems right to me.
@ Zoomkat: I checked again with your code. Tower Pro goes fine for any value I input but the Emax doesn't do anything for extreme value (1->4° and 170 -> 180°).
So I guess the problem was the course range of the emax servos. I just have to modify the code to avoid those extreme values.