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++)
{
pulseOut(s,pos);
delay(20);
}
}

void pulseOut( byte pin, byte p){
digitalWrite(pin,HIGH);
delayMicroseconds(300+p*(2500/180));
digitalWrite(pin,LOW);
//Serial.println(p*(1000/180));
}

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.
EDIT 2019 the program was developed using the Smeezekitty core. Nowadays the Microcore is more popular. The Microcore however, does not accept variables in the “delayMicroseconds(300+p*(2500/180));” statement. Therefore when using the Micocore, one needs to put a constant value in that statement, a value that gives an acceptable speed.

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.

3 thoughts on “Servo on Attiny13”

  1. It does not compile on Arduino 1.6 and 1.8 using MicroCore. Which arduino version do you use and with which core for Attiny13?

  2. That was ong ago, it was smeezekitty. There are some significant differences between the two
    The problem is with the delayMicroseconds(300+p*(2500/180)); statement.
    Microcore does no accept variables in delayMicroseconds.
    Best to put a constant value there that gives a satisfactory result

Leave a reply to E Cancel reply

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