Getting Started with the Atmel ATtiny84 Microcontroller



Like many folks, I got introduced to microcontrollers through the Arduino platform. The ease of setup, simple language syntax and the availability of a huge number libraries makes Arduino a very attractive choice to prototype your hardware projects. But after you get a grip on your project, sometimes it’s worth asking if you really need the power and the prototyping ergonomics of an Arduino, especially if all you are doing is reading a few sensors and turning on a few LEDs.


Atmel has a line of microcontrollers called tinyAVR which are little microcontrollers which can do a lot of the work that an Arduino does in a much more compact form. Take a look at the wikipedia page that compares the capabilities of the tinyAVRs.


In this post, I’ll be setting up to program an Atmel ATtiny84, a chip that I will be using for most of my ATtiny projects. I picked this 14-pin chip rather than the very popular 8-pin ATtiny85 because the former has a few more I/O pins which could come in handy for many projects. Here is a pin comparison between these 2 chips.


[From Atmel datasheets, for illustrative purpose.]


 


I will be programming the ATtiny85 in C, using the free AVR-GCC and AVRDUDE tools. Windows users please read the Adafruit guide to set up the AVR tools. Mac users (like me, for instance) can use the free CrossPack suite for AVR programming.


Next, you need a programmer. I recommend the Sparkfun Pocket AVR programmer, which I use.


The setup is really simple – just connect the VCC, MISO, MOSI, SCK, RESET, and GND pins from the chip to the same pins on the programmer. The ugly looking small PCB you see below is my homemade version of the Sparkfun AVR programming adapter – not a requirement, it just makes it convenient to connect the 6-pin plug easily to a breadboard.


Note that you need not connect the VCC pin to the programmer, if you are powering the circuit yourself. There is a small switch on the programmer in case you decide to go that route.


Here is my programming setup:


 


Once you program the chip, you don’t need the programmer, and here you can see the chip working directly from the 3V battery:


 


Here is the source code – very simple:


 

//
// A simple program for the ATtiny84 that blinks an LED.
//
// electronut.in
//

#include <avr/io.h>
#include <util/delay.h>

#define F_CPU 8000000

int main (void)
{
// set PB1 to be output - connect your LED to pin 3
DDRB = (1 << PB1);

// loop
while (1) {

// set PB1 high
PORTB = (1 << PB1);

_delay_ms(200);

// set PB1 low
PORTB &= ~(1 << PB1);

_delay_ms(200);
}

return 1;
}

Here is the Makefile:


 

# Name: Makefile
#
# A simple program for the ATtiny84 that blinks an LED.
#
# electronut.in

DEVICE = attiny84
CLOCK = 8000000
PROGRAMMER = -c usbtiny
OBJECTS = main.o

# for ATTiny85 - unset CKDIV8
FUSES = -U lfuse:w:0xe2:m -U hfuse:w:0xdf:m -U efuse:w:0xff:m

# Tune the lines below only if you know what you are doing:

AVRDUDE = avrdude $(PROGRAMMER) -p $(DEVICE)
COMPILE = avr-gcc -Wall -Os -DF_CPU=$(CLOCK) -mmcu=$(DEVICE)

# symbolic targets:
all: main.hex

.c.o:
$(COMPILE) -c $< -o $@

.S.o:
$(COMPILE) -x assembler-with-cpp -c $< -o $@
# "-x assembler-with-cpp" should not be necessary since this is the default
# file type for the .S (with capital S) extension. However, upper case
# characters are not always preserved on Windows. To ensure WinAVR
# compatibility define the file type manually.

.c.s:
$(COMPILE) -S $< -o $@

flash: all
$(AVRDUDE) -U flash:w:main.hex:i

fuse:
$(AVRDUDE) $(FUSES)

# Xcode uses the Makefile targets "", "clean" and "install"
install: flash fuse

# if you use a bootloader, change the command below appropriately:
load: all
bootloadHID main.hex

clean:
rm -f main.hex main.elf $(OBJECTS)

# file targets:
main.elf: $(OBJECTS)
$(COMPILE) -o main.elf $(OBJECTS)

main.hex: main.elf
rm -f main.hex
avr-objcopy -j .text -j .data -O ihex main.elf main.hex
avr-size --format=avr --mcu=$(DEVICE) main.elf
# If you have an EEPROM section, you must also create a hex file for the
# EEPROM and add it to the "flash" target.

# Targets for code debugging and analysis:
disasm: main.elf
avr-objdump -d main.elf

cpp:
$(COMPILE) -E main.c

To upload the program to your chip, connect the programmer to the USB port of your computer, and run ‘make install’ in a shell in the relevant project directory. If all goes well, you will see a happy blinking LED.


So now you know how to program an ATtiny84. But to really understand the chip, you have to read the Atmel datasheet (just search for ‘ATtiny84 datasheet’ on the net) – there is no way around it. It’s complex and can seem incomprehensible in the beginning, but do persist, and in the end, I assure you that it will be much more rewarding than blindly using libraries that someone else wrote.


Good luck to you in starting out with the tinyAVRs, and watch electronut.in for upcoming projects that use these chips.