AVR ATtiny Microcontroller Programming Guide for macOS Catalina

The ATtiny series of microcontrollers are very useful for projects where space is very tight, or projects that need many copies of a board. They're less expensive than an Arduino board, and take up less physical space. Programming them is a little trickier, but if you know C or Arduino, you'll manage.

The ATtiny85 Microcontroller

The ATtiny85 is the most powerful of the small ATtiny series that fits into an 8-pin DIP housing, perfect for learning, prototyping, and quick builds. The instructions below also work for other ATtiny chips, and for their larger brothers, the ATmega series (including the ATmega 128 that the standard Arduino UNO board uses, for example), but you'll have to adjust the target device in the Makefile. ATtinys and ATmegas belong to the AVR family of microcontrollers made by Atmel, which is now owned by Microchip.

Programmer

You'll need an In-System Programmer (ISP) to upload code to the ATtiny. It connects to your Mac via USB, and to the ATtiny using a standard 6-pin ISP header (there's also a larger 10-pin variant). The USBtiny family of programmers are widely used, well supported by avrdude, and very affordable. I've tested the ones below, and they come with open-source schematics and good documentation, so you could even build your own if you like:

If you have more advanced needs, like high-voltage serial programming (necessary if you "bricked" your ATtiny by setting certain fuses the wrong way), or in-circuit serial debugging, then you will need a more high-end programmer. I would recommend the original Atmel-ICE programmer (around $100), but it's overkill for small DIY projects.

Many ISP programmers provide 5V to the target chip when connected. This is useful to program an isolated chip, but may not be what you want if your chip is soldered onto a board that pulls more power than the programmer can provide. Adafruit's USBtinyISP has a jumper that can be pulled off to disconnect the 5V target power. Sparkfun's Tiny AVR Programmer has a soldered jumper that can be opened with a soldering iron to do the same. In that case, power your target chip at 5V using another way. Be careful with 3.3 V systems. Adafruit and Sparkfun have tutorials how to handle level shifting when programming. In general, no microcontroller likes signal voltages on its pins that exceed its supply voltage!

Another common problem is that your board design pulls so much current at one of the ATtiny's programming pins that programming fails, e.g., if an ATtiny pin powers several LEDs directly. In that case, use a socket to make your ATtiny removable, or add jumpers (or transistors) to your board design to take the load off the ATtiny pin, at least during programming.

If your target board does not have a dedicated 6-pin ISP connector, connect the 6-pin ISP header to your ATtiny as follows, e.g., using a small breadboard and male–male jumper wires. Use red for VCC, black for GND, and different colors for the other cables to keep your sanity. Wrap some sticky tape around the jumper wires once they're connected to the ISP socket correctly, so you don't have to re-sort them each time. 

In the table below, the ISP pins are shown in the way their on-board header pins would be arranged looking down onto them from the top, and the ATtiny pins are arranged in the order as the 8-pin DIL chip would appear on the board looking down onto it from the top. ISP pin 1 is usually marked with a red cable in the ribbon wire. Check that you have ISP pin 2 at 5 V, and ISP pin 6 at GND using a multimeter before connecting your ATtiny to avoid frying your ATtiny with a wrong pin connection.

ISP ISP ATtiny ATtiny
1 2 6 (MISO) 8 (VCC)
3 4 7 (SCK) 5 (MOSI)
5 6 1 (RESET) 4 (GND)

Mac Software

The easiest way to get the latest versions of the open-source avr-gcc compiler, the avrdude hex code uploader, and related tools to program AVR microcontrollers, is to install the latest version of the Arduino IDE. It includes these tools and libraries because it uses them itself behind the scenes. To find their installation location, in Arduino Preferences, turn on verbose output for both compiling and uploading. Connect an Arduino board and upload a simple sketch, like the Blink sketch from the Examples:Basics: menu. See my Arduino In A Nutshell booklet if you need help setting up your Arduino environment. Then copy your console output from the Arduino IDE to a text editor and search for avr-gcc and avrdude. This gives you the path to the avr-gcc and arvdude executables, along with the parameters Arduino uses for compiling and uploading. In my case, the path is the one below. It may change in future versions of the Arduino IDE, although that's not very likely. Add the following two lines to your ~/.bash_profile file (haven't switched to zsh yet) to update your $PATH environment variable so the tools are found from the command line and the Makefile:

PATH="${PATH}:/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin"
export PATH

Create a folder with your AVR C code called main.c, and a Makefile containing the following:

all: hex
hex:
    avr-gcc -Os -DF_CPU=8000000 -mmcu=attiny85 -c main.c
    avr-gcc -DF_CPU=8000000 -mmcu=attiny85 -o main.elf main.o
    avr-objcopy -O ihex main.elf main.hex
flash:
    avrdude -C /Applications/Arduino.app/Contents/Java/hardware/tools/avr/etc/avrdude.conf -c usbtiny -p attiny85 -U flash:w:main.hex
clean:
    rm -f main.o main.elf main.hex

Then you can compile your code using make, and upload it to your ATtiny using make flash. Use make clean to remove all build files except for your source code.

If you get a warning about missing command line tools, install them using the command

xcode-select --install

As an example main.c file, here is some minimal code to blink an LED connected to pin 2 on the ATtiny85:

#include <avr/io.h>
#include <util/delay.h>
#define LED 1<<3         // Connect LED anode to Port B, bit 3
                         // (PB3, physical pin 2), and its cathode
// via a 330 Ohm resistor to GND (pin 4) int main(void) { DDRB = LED; // Set LED port to output, rest to input for(;;) { // Repeat forever: PORTB |= LED; // Turn on LED _delay_ms(250); // Wait 1s PORTB &= ~LED; // Turn off LED _delay_ms(250); // Wait 1s } }

CrossPack (previously called AVRMacPack) used to be a very useful package with ports of the open source tools (avrlibc library, avr-gcc compiler, avrdude programmer and others) to program the ATtiny and most other AVR microcontrollers using the command line under macOS, similar to the WinAVR package for Windows. However, it has not been updated since 2016.

Overview Presentation (from 2008, slightly out of date)

Learning AVR C

To learn the special commands that avr-gcc supports, you can use the avr-libc reference. A more readable approach, including sample projects with ATtiny and other AVR microcontrollers, can be found in these books:

Make: AVR Programming (2014) is a little more recent than the book below, but not free, although you may have access to an online version via your institution.

tinyAVR Microcontroller Projects for the Evil Genius (2011, now free) is a good intro to using the ATtiny with C. No special experimenter's board, no BASIC used, no lengthy introduction to C, instead lots of simple and fun hardware projects, well documented.

Attachments:
File Description File size Downloads Last modified
080529 i10 ATtiny Talk.key   6752 kB 205 2011-08-19 12:50
080529 i10 ATtiny Talk.pdf   807 kB 1748 2011-08-19 12:58

We use cookies on our website. Some of them are essential for the operation of the site, while others help us to improve this site and the user experience (tracking cookies). You can decide for yourself whether you want to allow cookies or not. Please note that if you reject them, you may not be able to use all the functionalities of the site.