Marker Lamp 2015.11

An Arduino controlling a Servo

Making a servo move used to be an expensive endeavor, but now, with an Arduino containing at least 6 PWM (Pulse Width Modulation) pins, you can easily control 6 of them for less than $10!

To control a servo, you need to send it a digital pulse every 20 milliseconds (which turns out to be 50 Hz = 1/20 ms) and the width of the pulse determines how far the servo arm will move. As shown at Servocity, a 1 ms wide pulse will move the servo to the one end and a 2 ms wide pulse will move it to the other end. While, a 1.5 ms width pulse, would center the servo in its range. Now you might understand why a simple DPDT (Double Pole Double Throw) toggle switch can not control a servo like we can control a Tortoise machine. Toggle switches simply do not generate pulses of different widths! (And I know someone will challenge me on this one, and I will gladly post the video of anyone toggling a switch at 50 Hz "moving a servo precisely", on YouTube! ) Also read this page.

Servos themselves are also getting less and less expensive, $2.34 for this TG9e Micro Servo at Hobby King ( http://tinyurl.com/p9accx5 ).

One more thing before we 'Arduino' the servo, you need to know that a servo can use quite a bit of current to obtain and maintain its position, if something is pushing against it, so, to avoid resetting the Arduino board, it is advised that you provide a separate power source to power the servo. They typically need something between 4.8V and 6V, so a 5Vdc wall wart would do just fine. The USB 2.0 port can only provide 500 mA at most, and when your servo starts buzzing, you are exceeding this current limit. In our Division 3 clinic, we plugged a 9V battery into the Nano's Vin and GND pins.


Servo control pin connected to D3 (http://fritzing.org/ used)

Software:

The folks at Arduino.cc gave us a library to control a servo with, by simply including the Servo.h file. So quickly read though the next few lines of code and I'll explain it after wards:

#include <Servo.h> // include the Servo library
// define the PWM pin used to connect the servo to
// on the Uno and Nano, you can use 3,5,6,9,10 or 11

#define servo1PIN 3

Servo myServo1; // create the servo object

void
setup() {
myServo1.
attach( servo1PIN ); // attach the pin to the servo object
}
// setup

void loop() {
   myServo1.write( 90 ); // set the servo position to 90
   delay( 1000 ); // wait one second
   myServo1.write( 120 ); // set the servo position to 120
   delay( 1000 ); // wait one second
}
// loop

If the comments are not clear enough, we first include the Servo library, then define servo1PIN to pin 3. Create the myServo1 object and in the setup() function attach it to the servo1PIN. And then over and over in the loop() function, write 90 and 120 to it, while delaying a second after each.

You plug your specific Arduino board into the computer's USB port, start the Arduino software, paste the code shown above into it, and from the Tools menu, pick your specific board (Uno or Nano) and then select the COM port. Now select the "Upload" button (which will verify or compile the code). Give it a few seconds and start watching the servo motor move from 90 to 120 degrees. Only your imagination can guide you to the next step! Read an analog or digital pin to decide when you move the servo? Or how far? Adding a second servo with servo2PIN, myServo2.attach() and myServo2.write()?

Now you have successfully moved your first servo with an Arduino! You also did not have to worry one little bit (pun intended) about the width of the pulse in milliseconds, isn't that nice? Next time we will flash some lights while moving the servo...

The code above and another version to move the servo slower between the 2 points are attached at the bottom of this page.

Speed

ċ
markerLampServo1.ino
(1k)
Gert (TxAdmin) Muller,
Oct 14, 2015, 12:15 PM
ċ
markerLampServo1Slower.ino
(1k)
Gert (TxAdmin) Muller,
Oct 14, 2015, 12:15 PM
Comments