in Arduino

Using an Atmel ATTiny2313 to blink a LED on the cheap.

After using Arduino for a while, I decided to give actual microcontrollers a try. Arduino makes it really easy for you to have everything on a single package, with a really simple and well documented API. But, if you want to save money or want to do things from scratch, a bare microcontroller is the way to go.

After doing some research, I chose to use the ATTiny2313, because it is inexpensive, it has an internal oscillator and has enough IO pins and features to do some cool stuff. But first, I went and implemented the “hello world” of microcontrollers on it: blink a LED.

Bill of Materials

In order to start, you are going to need some stuff:

  • The datasheet for the ATTiny2313
  • An AVR development environment. On the Mac (my current platform) the best option seems to be AVR MacPack. It puts all the necessary tools into one easy to use installer package.
  • An AVR Programmer. I chose the USBTinyISP from adafruit. It seems to be the most inexpensive and with the best features. You will have to put it together yourself, but that’s half the fun of this little project right?
  • An AVR Target board. This is where the actual AVR programmer will be connecting and burning the code into the chip. There are some prebuilt options available, but in the spirit of keeping things cheap, it is better to just build your own. There is an excellent article on Evilmadscientist on creating a minimalistic target board for several Atmel chips, including the ATTiny2313.
  • The actual hardware:
    • A ATTiny2313
    • a 20-pin socket
    • Male pin headers for making the connections with the programmer
    • A prototyping board
    • Soldering materials, basic soldering skill, big soldering will
    • LED
    • Resistor

Software

After installing the AVR Macpack, referenced on the Bill of Materials, from a terminal, type the following:
avr-project Blinky
The command above will create a folder with a project skeleton called Blinky. It will generate several files for you and give you a good starting point for the project., if at some point you need to work with pdf files check out theĀ online soda pdf converter. The important parts of this project will be the following 2 files:
Blinky\firmware\Makefile
Blinky\firmware\main.c

The file main.c contains the source code for the microcontroller and the Makefile will contain instructions that will tell the the avr toolchain how to compile and burn the code into the attiny2313.
Let’s edit main.c so that it will look like this:

#include <avr/io.h>

int main(void)
{
   // set the 6th pin of port D as output (pin 11, see datasheet)
   DDRD = 1 << 6;       
   for(;;){
      // wait for 200ms
      _delay_ms(200);
      // toggle pin 11
      PORTD ^= 1 << 6;
   }
   return 0;  
}

Now let’s edit the Makefile and change the following lines:

DEVICE     = attiny2313
CLOCK      = 8000000
PROGRAMMER = -c usbtiny 
FUSES      = -U hfuse:w:0xdf:m -U lfuse:w:0xff:m