Recently I worked on a project where I wanted to control a servo using an Atmel ATtiny85. I checked online for ready made code that did this but I couldn’t find anything satisfactory. Not wanting to switch to a larger microcontroller I decided to write my own servo library for the ATtiny85.
I call it Servo8Bit. It supports up to 5 servos, runs on the ATtiny85 or ATtiny45 and uses only one 8 bit counter. It can generate a servo control pulse from 512 to 2560 microseconds with 256 steps of resolution. And, most importantly, it is very easy to use.
I enjoy using ATtiny microprocessors in my projects, particular the ATtiny85. It is very small with only 8 pins, and it costs about two dollars each. All the code I found for it controlled the servo with about 12 steps of resolution. The problem is that the ATtiny85 only has 8 bit timers. There is excellent servo control code for most other ATtiny microcontrollers and they all make use of 16 bit counters. I could have used another ATtiny, one that has a 16 bit timer, but all of those have more pins than the ATtiny85.
This library is modeled after then Arduino servo library and has an identical interface. Here is a short example program that shows how this library is used.
The definition of the delay() function is omitted, which is needed to for this code to compile. The example file included with this library contains all the code. Here is a video that shows this example code in action:
These are the functions that are available in this library. They behave identical to the functions of the Arduino servo library with no surprises.
When a servo is attached to a pin a PWM servo control signal will start being generated on that pin. By default it will command the servo to move to its middle position. You can command the servo to move by calling the write() function and passing in a number between 0 and 180. This number represents rotational position in degrees. For example, depending on how you’re looking at the servo, 0 would mean go all the way left, 180 would mean go all the way right and 90 would mean go to the center. If you are using a continuous servo then this number represents speed and direction, where 0 is full speed forward, 180 is full speed backwards and 90 is stop.
Not all servos go to the same position for each pulse length. You might notice this if you use a continuous servo and instead of stopping it rotates slowly in one direction when you tell it to go to position 90. In this case you might want to change the pulse lengths used by the driver. You can do this by calling the attach function with 3 arguments, like so: myServo.attach(pin, min, max), where pin is the pin on portB to be pulsed, min is the minimal pulse length to generate at 0 degrees and max is the maximum pulse length to generate at 180 degrees. The default is 544 and 2400 microseconds. You can also use the function writeMicroseconds() to manually set the pulse length.
To completely stop pulsing the servo call the detach() function. The pin will not be pulsed again until the attach() function is called.
Limitions of current version
* The driver can easily be tweaked to support more servos, at the cost of extended time between pulses. See the notes in file ‘Servo8Bit.h’.
Source Code, make-file, example - download
Inside a huge PCB factory: https://www.youtube.com/watch?v=_XCznQFV-Mw
News on theme: