Hello everyone, I'm trying to run this very simple code, just a servomotor controlled with a potentiometer:
#include <Servo.h> // add servo library
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
Everything worked perfectly powered with the USB B.
But then i tried using a 5v power supply through the barrel connector.
At that point the board worked but after a few seconds the servomotor stopped responding to the potentiometer and made little sounds as if it wanted to move but it was stucked.
I then changed the power supply again, using a 12v (always through the barrel). It did the same exact thing as with the 5V, but then i got 3 board leds on (instead of the 2 that were on up to this point) (see pic) and now it's impossible to upload anything.
I'm using a very cheap board, is it possible that i broke it?
Do you know the meaning of the third led turned on?
And even before this point, how is it possible that the servo wasn't responding correctly with the different power method?
Thank you a lot in advance if you have any input for me!
Carlo
That is where you are going wrong. You need to supply the barrel connection with a minimum of 7V up to 12V.
This is because power from that goes through a diode (loose 0.7V) and then into a regulator. That needs more than it outs out.
Also the current you can draw from the 5V pin when powering it this way is limited to about 100 mA due to the poor thermal dissipation of the regulator.
Yep that's why i changed to 12v hoping that that would be the solution, but the problem was the same (and even worse since now it looks like it can't upload anything).
Regarding the current that is possible to draw, the servomotor should go from 20mA to 100mA (i read with also peaks of 300mA), but it should be a problem that it is asking more than Arduino can provide right? At the beginning with the usb B it was working perfectly
Is that with the power from the USB and nothing connected to the barrel?
If so you could have broken it. Even powering from the USB is too much current for the 5V pin when driving one servo, let alone more.
If you draw too much current from an Arduino (or any other electronic device) the voltage from that device drops. If the voltage drops to the servo this affects the electronics inside the servo and so it readjusts itself to compensate. So it twitches.
Also there is not a one to one relationship between the pulse signal you give a servo and its position. Each has its own tolerances. Noise (very small changes in voltages) on the system can leave the servo not exactly in the right position and the servo makes a chattering noise as it hunts for the correct position. This is normal and is how servos work. So at some angles you might get twitching and at others nothing.
I don't understand why It was working perfectly using the USB B supply and not with the barrel. In both cases the servo was drawing directly from the board
When things are wrong they don't always go "puff" immediately. Sometimes if it is just marginally wrong they can work for a time and then fail.
The time between being put in a stressed position, and when it fails, increases the less it is abused. In effect the lifetime or time to failure is increased. This is what results in so many bad tutorials on the internet. Some one gets something "working" writes an Instructable (other crap sites are available) and goes onto the next one. They never get to see it fail. Also they think that if a device functions then everything is fine. They never check the waveform on a design for signs of trouble, hence lots of designs for I2C circuits with no pull up resistors.
Professional engineers spend more time studying the design and the component's data sheets to make sure no rating on each device is being exceeded.
Even then a product needs testing to make sure it is going to last the design time. This is not just one sample but typically could be 50 to 100 samples. This is done at high temperature because for every 10˚C hotter you run a product the lifetime typically halves. Then when ever a device fails it is examined to find out what component failed. Then they concentrate on that part.
The reason here is, the USB power supply can supply more current than the poor little regulator on the Arduino, and when the USB is connected the USB power bypasses that regulator. So with USB plugged in, you're running your servo with USB 5V, not Arduino-regulator 5V.
That, by the way, can be hard on your USB output, though they're generally pretty well protected internally, so it's hard to kill them. Depending on the servo, stall currents can exceed 1 Ampere, even for the little SG90 servos, so using an external 5V supply is a wise move.