Archive

Posts Tagged ‘attiny’

Using an Arduino to Program an ATtiny

October 15, 2012 2 comments

The ATtiny is in most ways a smaller version of the ATmega – the brain of an Arduino – and I will be attempting to program it using the Arduino for ISP. If you want to learn what ISP is you can briefly look at the wiki article or this finely detailed article which deals with programming ATMEL microcontrollers. But basically, ISP lets you program your chip when its already been placed into your circuit, which is why its known as In-System Programming (ISP).

Components

To guide me through this process I will be using this tutorial by MIT Media Labs(which is somewhat outdated) and this more easily digestible guide on Instructables. As described on both website, I will be needing:

  • Arduino Uno or Duemilanove (with an ATmega328)
  • ATtiny85 (but you can also use an ATtiny45, ATtiny44, or ATtiny84)
  • 10 uF Capacitor
  • a breadboard
  • jumper wires
  • an LED
  • a 220 Ohm resistor

Setup

First, we’ll need to install ATtiny support (also known as “cores”) into the Arduino IDE. I’m going to assume you already have the latest version of Arduino IDE (1.0.1 as of this posting), if not, you should go update it.

EDIT: While the above linked cores will work just fine with this tutorial, a much better version can be found here. The install process is much the same as below, simply copy and overwrite any files into the hardware folder.

  • Locate your main Arduino folder
  • In it should be a sub-folder called “hardware”
  • Copy the ATtiny folder from inside the .zip to the hardware folder. You should end up with a folder structure something like Documents > Arduino > hardware > ATtiny that contains the file boards.txt and another folder called variants.

  • Restart the Arduino development environment.
  • If done correctly, you should see multiple ATtiny entries in the Tools > Board menu.

Connections

Next, we need to connect the Arduino to the ATtiny. It may be a good idea to make sure that the Arduino isn’t running anything. You can do this by uploading the “BareMinimum” sketch found under File > Examples > Basics. Then, connect the ATtiny to the Arduino in this way:

Here’s how my setup looked:

(NOTE: In the picture I reversed the GND and 5V on the Arduino. Don’t do that.)

The pin connections are:

  • ATtiny Pin 2 to Arduino Pin 13 (or SCK of another programmer)
  • ATtiny Pin 1 to Arduino Pin 12 (or MISO of another programmer)
  • ATtiny Pin 0 to Arduino Pin 11 (or MOSI of another programmer)
  • ATtiny Reset Pin to Arduino Pin 10 (or RESET of another programmer)

NOTE: Dont confuse the output pins (0 – 4) above with the actual IC pins (1 – 8), refer to the Fritzing diagram above and the diagram below to connect everything correctly. You can use the dot in the corner of the ATtiny to orient it properly. Double check everything before powering up unless you want to chance frying your ATtiny.

Arduino ISP

Configuring the Arduino to act as a serial programmer to program other chips is simple;

  • Make sure the board being used is still the Arduino (you can see what board is being uploaded to at the bottom right of the Arduino IDE)
  • Open the “ArduinoISP” sketch from the File > Examples menu.
  • Upload the sketch to your Arduino.
  • The Arduino should now be ready to work as an ISP.

NOTE: At this point the capacitor should NOT be used to connect ground and reset as it may prevent the sketch from being uploaded successfully.

Configuring the ATtiny to run at 8 MHz

Before we program the ATtiny with our first sketch, its a good idea to configure it to run at 8 MHz to it so that we can have our sketches run at a normal speed and use the SoftwareSerial library. You will only need to do this once for each microcontroller.

  • Place the 10uF capacitor between ground and the Arduino reset pin. Be sure to connect the negative of the capacitor to ground.
  • Select which board you are programming by going to Tools > Board. In my case I will be programming a ATtiny85 and running it at 8 MHz.
  • Check to make sure the Arduino is serving as ISP by going to Tools > Programmer, and selecting “Arduino as ISP”.
  • Finally, run the “Burn Bootloader” command from the Tools menu.

Programming the ATtiny

Now that we’ve gotten through all the steps of setting everything up, programming the ATtiny is easy! I’ll be uploading the Blink sketch in this example.

  • Make sure that the ATtiny is still the board being uploaded to. (Check the bottom right corner of the Arduino IDE)
  • Open the basic blink sketch.
  • Remember to change the LED pin number from 13 to 0! (Check the ATtiny pin diagram above to see which pins you can use.)
  • Finally, upload it as you would any other sketch.

It should give the following error twice:

avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny85

Simply ignore the error message and you should be good to go.

Testing

If all went well, you should be able to get an LED to blink by connecting it from ATtiny “Pin 0” (pin 5 of the IC) to ground with a resistor in series. Here’s how my ATtiny85 looks running the sketch:

Note how I don’t need to have it connected to the Arduino any more! All I’m doing is giving the ATtiny a 5 volt supply via a voltage regulator. But really, the ATtiny85-10PU can run just fine with 1.8 – 5.5 volts (depending on the frequency you run it at). I should also note that the maximum current rating per I/O pin is 40 mA, so don’t plan on powering anything too big with this IC.

Final Note:

If you’re looking for somewhere to buy ATtinys check out Mouser. Here is a through hole ATtiny85-20PU for $1.29, but you can also get the ATtiny85-20SU* for $0.83, which is in a very solderable SOIC package if you are interested in saving some money and getting into surface mount soldering. I would go so far as to suggest buying at least 5 of them, because once you start using them you will think of all kinds of tiny projects that you could put them in.

*(20PU means that it runs at  maximum 20MHz, 2.7 – 5.5 volts. PU/SU is for package type.)

Bonus:

To make a dedicated ATtiny prototyping platform for the Arduino you can use a protoshield such as this and wire it up as shown here:

The space in the upper part of the mini-breadboard can be used for testing small circuits and debugging, though I would suggest using only Pin 3 and Pin 4 (left of the ATtiny in the image above) for this, as Pins 0 – 2 are used for programming. The bottom portion is used for programming status indicators, GREEN being all is well, YELLOW is programming in progress, and RED is error. Here is how it looks in real life:

Still alive!

October 6, 2012 Leave a comment

Hey there! Just checking in to make sure that I haven’t abandoned this blog/journal/whatever on my progress into the world of electronics. I have been making great strides indeed, it’s just that documenting that work is quite laborious.

Anyway, there are numerous things that I’m working on simultaneously. I will be getting a fresh new batch of components and tools in the next few weeks (already got them! 🙂 ) so I am looking forward to getting started on actual projects, instead of just tinkering around (although that has been greatly entertaining).

Some of the things that I will be working on are:

[Done!] A clock with a 7-segment led display whose time accuracy will be controlled by the real time clock (also known as a RTC) DS1307.

I already have the RTC DS1337 that I previously purchased, however it is quite different from what I expected. It doesn’t seem to have an easy battery backup like the DS1307, and there really isn’t much hacker community support for it in the way of libraries and tutorials.

Now I could buy a premade module for about $10-15 which would just “plug and play”, but the module is only made up a few passive components – besides the RTC -, and shouldnt be hard at all to make myself. (It wasn’t.)

[Done!] Controlling a 7-segment LED display with a display driver. More specifically, the MAX7219 display driver. I figure it would be a lot easier to get this thing up and running rathar then deal with the hastle of using multiple shift-registers and a whole lot of resistors just to control a 4 or so digit LED display. (And it was, honestly, this thing is like a god send for controlling 7-segment and matrix LED display.)

[Done!] Programming an ATtiny, to start with, and later an ATmega. This is so that I can get away from being dependent on the Arduino being hooked up to my projects all the time, and would reduce the size of my projects as all I would need is the ATmega/tiny, a crystal, a couple of capacitors and something to regulate voltage.

[Done!] Modifying a servo to rotate continuously. This is absolutely necessary for my main project, and there are more then a couple of guides/tutorials online that detail how to do it. (It was ridiculously easy to do this, took no more than 5 minutes start to finish.)

Update (6/Oct/2012): So I started writing this a while back, maybe a month or so, and have accomplished a lot of the things that I had planned. As such, I updated this post and am submitting it now. The detailed posts accompanying each project should be up soon – but knowing my post history I wouldn’t hold my breath waiting for them.