Make dirt-cheap electronics with the ATTiny45 + Arduino Nano

Tweet about this on TwitterShare on FacebookShare on RedditShare on Google+Email this to someonePin on Pinterest

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.

gif_im_color_dither_64_gif_200x157_bffdc2

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 Smile with tongue out I’ve written this to be very basic and easy to follow. You can also checkout this guide.

Stuff you need

Steps

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).
image

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:
image

3) Open up the Arduino IDE from the start menu shortcut. Click Tools > Board and a whole bunch of ATTiny names should be listed.
image

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.
image
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.
image

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:
attiny_bb

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:
image

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:
image
(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:
attiny_led_bb

(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:
image
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 Smile
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.

gif_im_color_dither_64_gif_200x157_bffdc2

 

ATTiny 45/85 Pins

ATTiny-pin-layout

“A3” represents that it is an Analog Input pin, and you reference it by using “A3”.

Yay

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!

Tweet about this on TwitterShare on FacebookShare on RedditShare on Google+Email this to someonePin on Pinterest
  • arduino-guy

    Sweet article dude !

  • Jope

    You should always use a bypass capacitor between VCC and GND (0.1 µF), even if your little example circuit works without it.

    • RogueCode

      Thanks. Yes, I didn’t add it just to simplify things and because I was going to show that in the follow up post.

  • permababy

    Arduino Pro Mini clone is $2.88 on ebay. I use an old Arduino Uno with the ATMega chip removed as USB-to-serial interface:

    http://www.ebay.com/itm/200957063666

    http://arduino.cc/en/Guide/ArduinoMini

  • Jeff Murchison

    Great stuff.

    “Obviously doing this every time you want to write to a chip is a waste of time…”

    That’s why I built myself a shield for doing this, I got way too tired of wiring it up every single time.