Simple ATtiny Servo Control


ATtiny, arduino, and a micro servo

This method may not be pretty, but for crude servo control with the ATtiny chip, this method should work.  I programmed it using an Arduino Uno as shown here.

Hobby servos work by taking the length of time a pulse of electricity is sent to the “signal” input.  If it is 1.5 milliseconds (ms), it moves to the middle of it’s travel, 2 ms to the right, and 1 ms to the left.  This excellent paper illustrates this, as well as some standard connector colors, which have helped me with some of my projects.

For my “tiny useless machine,” I needed a way to control a servo using the ATtiny chip.  Accuracy didn’t have to be great, so what I did was used the delay command to make the pulse either 1 millisecond (delay 1) or 2 milliseconds (delay 2).  The code below will show what I mean, and should bring a servo to the +90 degree position.  Substitute 1 in for the delay, and it reverses to -90.

servo control

Servo control diagram, from Wikipedia

for (int i = 0; i < 8; i++){
digitalWrite(servo1, HIGH);
digitalWrite(servo1, LOW);

One way I could see improving the accuracy of this method would be to tell the Arduino IDE that a chip with a 20 Mhz clock speed was actually running at 1 Mhz.  Given my previous experience using a 1 Mhz chip at a supposed 8 Mhz, I believe this would make the timers run 20 times faster than you say, so a “40 ms” delay would actually be 2 ms.  This would allow you better resolution, as a 30 ms signal would actually be 1.5 ms, able to center your servo.  I have  not tried this yet, but in theory it should work.

On the other hand, if you’d like a really good, if slightly more involved method, the “Cunning Turtle” team has a servo library that seems to control a servo with the ATtiny45/85 quite well.  Again, I haven’t tried this method, so you’re on your own here.  As for the useless machine application mentioned earlier, here’s a video of it in action:

It should be noted that these techniques are really quite general.  Although they have worked for my ATtiny chip, any microcontroller, or timing circuit for that matter, could use this concept to control a servo.


Trackbacks and Pingbacks: