arduino · Electronics · raspberry pi

Servo Triggers – Quickest, Fastest, Easiest way to make your servos dance

13118-01a

What’s a Servo?

Servos are a fundamental building block of all mad-scientist laboratories.  These little guys will magically move to a position of your choosing, and stay there.

As stated by Servo City:

As long as the coded signal exists on the input line, the servo will maintain the angular position of the shaft. … A very common use of servos is in Radio Controlled models like cars, airplanes, robots, and puppets. They are also used in powerful heavy-duty sail boats.  Servos are rated for Speed and Torque.

Here is a couple of pictures of servos in the wild:

How do they work?

Servos are a compact package of control circuitry, motor, gearing, and, importantly, a Potentiometer.  Potentiometers are hidden in all kinds of things.  That game controller you use to play X-Box has at least six of them in it!  (Try to guess where when you know what they do!)

Potentiometer is a little guy that, based on its position, has a particular resistance.  Resistance, measured in Ohms, is a measure of how hard an electron will have to work to make it through a material. More on that… on this page.  The important bit is that a Potentiometer tells the Servo board circuitry which position its drive shaft(or output spline) is in.

220px-potentiometer

Potentiometer

Since the servo can “know” its current position, it can use that information to determine how to get to the position you might ask it to achieve.

Normal Servo Operation

Typically, when a hobbyist wishes to use servos,  they’ll get an Arduino clone, and add code like this:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

Notice the:

myservo.write(pos);

That “pos” is the angle the author would like the servo to move to.

Or in the case of a raspberry pi:

# Servo Control
import time
import wiringpi

# use 'GPIO naming'
wiringpi.wiringPiSetupGpio()

# set #18 to be a PWM output
wiringpi.pinMode(18, wiringpi.GPIO.PWM_OUTPUT)

# set the PWM mode to milliseconds stype
wiringpi.pwmSetMode(wiringpi.GPIO.PWM_MODE_MS)

# divide down clock
wiringpi.pwmSetClock(192)
wiringpi.pwmSetRange(2000)

delay_period = 0.01

while True:
 for pulse in range(50, 250, 1):
   wiringpi.pwmWrite(18, pulse)
   time.sleep(delay_period)
   for pulse in range(250, 50, -1):
     wiringpi.pwmWrite(18, pulse)
     time.sleep(delay_period)

This is fine, but that’s a pain.  When time is short, and the idea is hot, you don’t want to take the time to fire up the IDE, and muss with the servo until it gets to the positions you are after…

Enter the Servo Trigger!

Servo Trigger Usage

The servo trigger makes it very easy to determine, store, and reuse a servo’s position.  There are three potentiometers on the board.  The first, is the position you’d like to servo to be in when the pin labeled “IN” is hot.  The second is the position you’d like the servo to be in when its not “hot” and the last is how fast you’d like the servo to try to get there!

13118-05

Now, with one pin, and the code you already know and love for turning it on, and off, you can control a servo’s position.  Additionally, this method of setting a servo’s position, allows you to do it while the servo is running, allowing you to tune it in-location.

They aren’t cheap – but check them out at the manufacturer’s website – so your ideas wont get delayed:

Sparkfun.com – Servo Trigger

Now go use it!

I’ve used these for:

  • magic boxes that only open when you stroke the right place, or do the right thing on a webpage.
  • resetting an Annunciator.
  • Waving a flag
  • Blinking an eyeball
  • Revealing a secret
  • Exposing a picture

The possibilities are truly endless.  Go play.

Advertisement

One thought on “Servo Triggers – Quickest, Fastest, Easiest way to make your servos dance

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.