Servo on Attiny13

Wanted to use an Attiny13 to sweep a Servo. There is an 8 bits Servo library for the Attiny85 series, that will compile after  some changes (TIMSK->TIMSK0 and TIFR->TIFR0) but that will be over the memory limit of the Attiny13, some other programs I found that were playing directly with the timing registers didn’t really work and one even broke the gears on my servo, so I decided to just make  my own program, which is in fact much simpler than I thought once I started:

byte s=0;
void setup() {
pinMode(s, OUTPUT);


void loop() {
for (byte pos=0;pos<180;pos++)

void pulseOut( byte pin, byte p){

The line “delayMicroseconds(300+p*(2500/180));” needs a bit of explanation:
“p” is the position in degrees, so if “p” is ‘0’, the pulsewidht for 0 degrees is sent to the server. In this case that is 300 uS. if p=180, the value for a full sweep is sent to the servo. In this case that is 2800uS.The values of 300 and 2500 are experimental: I started out with 1000 and 1000 but then the servo turned only some 90 degrees. Some programs for Attiny85 mention 60 and 4000uS as the min and max values for the pulse width
As  the values are  experimental, it doesn’t really matter if the  timing of the delayMicroseconds is off a bit on the Attiny13 core, but they seem OK for a UNO as well.
Though only a few lines, it still takes 540 bytes on the Attiny13.
True, due to the use of ‘delay’ the processor will mainly be ‘waiting’. Ofcourse one can solve this by using millis, or by setting up a timer, but it is an Attiny13…. there really isnt so much memory space to cramp a lot of other programming in it anyway, so sweeping the servo is the core  function.


Leave a Reply

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

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

Google+ photo

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

Twitter picture

You are commenting using your Twitter 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.