ATtiny85 Servo Control: Programming Tutorial

Published By

Standard or micro hobby servos are an essential part of many robotics projects, but controlling them takes more finesse than a simple on/off switch or relay. These servos take in a pulse-width modulation signal and respond with the correct physical rotation.

Per my research and experimentation, a pulse width of between 600μs to 2400μs produces a 180° rotation. The start of each pulse is spaced 20ms or 20,000μs from the beginning of the last, though the exact timing here appears to be flexible. Arduino boards can achieve this level of control, but what about the small-but-mighty ATtiny85? Yes, you can even use them to control a servo. Here's how to do it.

See related product

ATTINY85-20PU

Microchip Technology Microcontrollers - MCUs View

ATtiny85 Programming: Delay Microseconds

Years ago, ATtiny85 control under the Arduino ecosystem was a bit touch-and-go. To emulate servo control as simply as possible, I wrote a routine using the delay() command to "manually" produce the appropriate servo pulses out of the ATtiny85. This code does work, but because pulses vary in 1ms—or 1000μs—steps, the resolution is extremely limited. 

The better course is to use delayMicroseconds(), which will allow you to specify the pulse width much more precisely.

Servo_IMAGE 2

For this article, I'm running my ATtiny85 at the default 1MHz and programming it in PlatformIO under VSCode. You'll need to set up your configuration, as shown in the image above, adding the "board_build.f_cpu = 1000000L" to let it know it's running at one million hertz. The Arduino IDE should work as well if you take out the line: "#include <Arduino.h>."

As for why I didn't use delayMicroseconds() in my original experimentation, it appears that there was some complication with using delayMicroseconds with the ATtiny85 years ago. Fortunately, this issue appears to have been solved.

Use a Library: Sweep

Now that the ATtiny85 has benefitted from several years of development, controlling a servo is relatively easy.The only difference is that since the ATtiny85 doesn't have a pin 9, I modified the code to use pin 0 instead and attached the control wire accordingly.

Conveniently, the programmer I was using (the same one I used in the ATtiny85 Arduino tutorial) has several female pins to which I could connect. I also already had a custom servo connector with a capacitor ready to go. A dedicated power supply may have been more ideal, but this setup worked well, albeit with a bit of jerkiness. Thinking this jerking might have something to do with the resolution, I had some luck increasing the step time and rotation amount per this code.

ATtiny85 with a Potentiometer

For real-time interaction, Arduino provides an example that twists the servo using a rotating potentiometer. A modified version of this code is available for the ATtiny85, which includes pin 0 as the servo control pin and pin A1 as the potentiometer connection. Pin 0, which used to control the servos in these examples, is actually physical pin 5, which is in the corner across from the chip's dot. To visualize the layout, consult this diagram.

To use the servo with a potentiometer, hook the servo up, adding a potentiometer with the variable leg attached to pin IDE pin 2. Once you load in the modified code, you'll be able to twist your potentiometer and have the servo follow along with its angle.

Whatever your approach, you can definitely control a servo with the ATtiny85. Perhaps using this little chip can save money and space on your next project.

Related news articles

Latest News

Sorry, your filter selection returned no results.

We've updated our privacy policy. Please take a moment to review these changes. By clicking I Agree to Arrow Electronics Terms Of Use  and have read and understand the Privacy Policy and Cookie Policy.

Our website places cookies on your device to improve your experience and to improve our site. Read more about the cookies we use and how to disable them here. Cookies and tracking technologies may be used for marketing purposes.
By clicking “Accept”, you are consenting to placement of cookies on your device and to our use of tracking technologies. Click “Read More” below for more information and instructions on how to disable cookies and tracking technologies. While acceptance of cookies and tracking technologies is voluntary, disabling them may result in the website not working properly, and certain advertisements may be less relevant to you.
We respect your privacy. Read our privacy policy here