I make a lot of little gadgets using the Netduino because it is such a good prototyping tool. The SDK and dev tools (Visual Studio) are very mature and usually just work. But mainly, being able to code in C# (including intellisense) is just awesome (the Arduino IDE is rather basic, and I’m not a fan of Wiring). The problem is that these boards start at $35 so it’s not feasible to put them into every little project – plus for most things the Netduino is far more powerful than what you need.
That’s where the ATTiny chips come in. They are a range of tiny little chips that use barely any power, and can control whatever you want them to. Plus, you can burn Arduino onto them to make the coding really easy.
Obviously there is no easy way to plug these into your PC to write code to them, so this post will show how to use an Arduino Nano to write to them. There is lots of information scattered around the net about this, but most don’t use the Nano, or they require other components besides the ATTiny and Nano – plus I always seem to forget the wiring so I want to have an easy to find reference I’ve written this to be very basic and easy to follow. You can also checkout this guide.
Stuff you need
- Arduino Nano. I use this because they are so cheap. I keep one lying around specifically for writing to ATTiny’s. Grab one for around $7 on eBay (or cheaper if you’re willing to solder).
- An ATTiny chip. I use the 45 below, but each has their own pros/cons. The 45 costs just over $1 or R14 in South Africa.
- Wires and a breadboard.
- An LED.
- A 220 Ohm resistor.
1) Head over to the Arduino site to download and install the IDE: http://www.arduino.cc/en/Main/Software (I’m using Arduino 1.0.5).
2) Download this file, which contains the stuff that allows the Arduino IDE to interact with ATTiny’s. Extract the attiny folder (found in the master folder) to a new folder called hardware in your Arduino sketchbook directory (by default in My Documents/Arduino). It should have this hierarchy:
4) For now, from the Board menu select Arduino Nano w/ ATmega328 and plug your Nano in via the USB cable. To make sure it’s working properly, click File > Examples > 01.Basics > Blink. To deploy it to the board press the Upload button which is a right arrow.
After a few seconds the Nano’s white LED will start blinking, which means it was successful.
5) Now click File > Examples > Arduino ISP, and then press Upload. Notice that while it is uploading the Nano’s colored LED’s are flickering (that’s sending and receiving). When that is done the Nano now knows how to write stuff to the ATTiny.
6) Push the Nano into a breadboard lining up the center gap of the breadboard along the center of the Nano. Do the same with the ATTiny. Take note of the orientation (the ATTiny has a little dot on the top to help). Wire up the Nano to the ATTiny pins carefully to match this:
7) Now that it is all connected, we need to get the ATTiny to actually run Arduino. From the Tools > Board menu select the ATTiny that you are using. Notice that for each of them there are 3 speed options:
Because we are keeping things simple, we are not using any external components. That means that we can only use the internal clock, so are limited to either 1MHz or 8MHz. The choice between 1MHz and 8MHz is yours – it depends how fast you need your code to run, and theoretically 1MHz will draw less power. I usually use 8MHz. If you choose 20MHz by mistake you will need to connect a crystal to be able to set it back.
8) We need to tell the Arduino IDE to use the Nano to write to the ATTiny, so click Tools > Programmer > Arduino as ISP. Then press Tools > Burn Bootloader which will get Arduino onto the chip. There are a few things that can go wrong here – the most common one I’ve seen is “resp=0x15”. I fixed that by just writing the Arduino ISP sketch to the Nano again (repeat step 4 and 5, then come back to 7).
If the bootloader is successfully burned then the output window will look like this:
(Ignore those two errors).
9) At this stage your little $1 chip is an Arduino! To test that, let’s connect an LED and load the Blink example onto it. First connect up an LED like in the image below. The whole circuit is the same as before besides the resistor and LED:
(The long leg goes into the ATTiny, and the short leg goes to a 220 Ohm resistor that goes to GND).
10) Click File > Examples > 01.Basics > Blink. When the code opens up we need to change the first line of code from int led = 13; to int led = 4; – this is because we are using pin 4 on the ATTiny (see a little later in this post for the pin layout). To get the code onto the chip, it’s slightly different to running stuff on the Nano. Press File > Upload Using Programmer. Give it a few seconds and then the output window should look like this:
Something to note here is the sketch size. One of the limitations with the ATTiny’s is how much space there is for your code. In this instance, the blink code took up about 20% of the max. The ATTiny 85 has more space than the 45. If you need to run large complicated code then the exact method above can be used on a $3 ATMega328 – which is the chip used in a full-sized Arduino.
11) With the blink sketch deployed you should see the LED happily flashing away
You can now safely remove the ATTiny and put it into its own breadboard. To power it just connect the – of a battery to the bottom left pin, and the + to the top right. It will work anywhere between 2.7V and 5.5V.
ATTiny 45/85 Pins
“A3” represents that it is an Analog Input pin, and you reference it by using “A3”.
That’s all there is to it! The flashing LED is a really basic example, so I’ll update this when I’ve made a blog post on creating a real-world item.
Obviously doing this every time you want to write to a chip is a waste of time, so I keep an Arduino Nano permanently stuck in a breadboard with the right wiring so I can just push in an ATTiny chip whenever I want.
If you’re new to Arduino coding head on over to these tutorials.
Let me know if you have any problems!