Running a servo on ATTiny 85

I’ve been looking to get a servo running on the ATTiny 85. I’ve just got my hands on some of the diminutive chips. Here’s how to get a servo moving with the older Software Servo library in Arduino and the AVR Pocket Programmer.

First connect up the ATTiny to the programmer. You can use this guide.

Next get the Software Servo library from the Arduino site. Note: this is different from the servo library that comes with recent version of the Arduino IDE. To install the library, create a new folder under the Libraries folder in the Arduino program location. For me (Win 7) this is: C:\Program Files\arduino-0022\libraries\SoftwareServo. Drop the SoftwareServo.h and SoftwareServo.cpp file you downloaded into this directory. Instructions for other platforms can be found here.

For a sample sketch I just took the Servo -> Sweep example sketch and replaced the line where the servo variable is declared and changed the pin number. I also added a couple of calls to SoftwareServo::refresh() as recommended in the Software Servo documentation. Here’s the code:

#include <SoftwareServo.h> 

SoftwareServo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 

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

void setup() 
{ 
  myservo.attach(1);  // attaches the servo on pin 1 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 
    SoftwareServo::refresh();
  } 
  for(pos = 180; pos>=1; 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 
    SoftwareServo::refresh();
  } 
}

To get the code onto the chip there’s some useful software from MIT which adds support for the ATTiny 45 and ATTiny 85 to the Arduino environment. This gives you the option to select the ATTiny 85 from the Tools -> Board menu in Arduino. The support files can be found here:

http://hlt.media.mit.edu/?p=1229

I have the AVR Pocket Programmer, so I was able to upload straight to the 85 by selecting Tools -> Board -> AT Tiny 85 (w USB Tiny ISP). To upload the code, follow the same process as for any other sketch – complie the code (click the Verify button), then hit the Upload button.

Connect up the servo up to 5v and ground, then connected the control wire (the yellow/white lead coming from the servo) to pin 6 on the ATTiny 85 (PB1 – the same pin with the yellow MISO cable in the diagram on this page). Here’s a link to the ATTiny 85 datasheet if it helps.

If you need to reupload the sketch I find I need to remove the servo control wire from pin 6.

The movement seems a bit jittery, not sure whats wrong there.

3 thoughts on “Running a servo on ATTiny 85

  1. Hello. I read your post about ATtiny85 running a servo motor. I need drive a servo using a potentiometer like Knob.ico software inside Arduino 1.0.1’s examples. These use the #include that dont work with ATtiny85. Can You help me? Regards. Paulo Cherem from Rio de Janeiro – Brasil

  2. Hello. I read your post about ATtiny85 running a servo motor. I need drive a servo using a potentiometer like Knob.ico software inside Arduino 1.0.1’s examples. These use the #include Servo.h that dont work with ATtiny85. Can You help me? Regards. Paulo Cherem from Rio de Janeiro – Brasil

Leave a reply to Alessandro Riva Cancel reply