Programming an ATtiny using an Arduino Uno

      No Comments on Programming an ATtiny using an Arduino Uno

Progamming an ATtiny micro controller can be done using an Arduino.

Step 1: Prepare Arduino IDE

Make sure your Arduino IDE is supporting ATtiny micro controllers.
Look at this post for more details about how to configure your IDE to do that.

Step 2: Prepare your Arduino Uno

To use your Arduino Uno as a programmer, prostate you have to make sure it is programmed with the proper firmware.
– Lauch your Arduino IDE
– Connect your arduino Uno to your computer using a USB cable.
– Load the following code example: File | Examples | 11.ArduinoISP| ArduinoISP
– Compile and upload that code to your Arduino Uno using the Sketch | Upload menu option.

Step 3: Wire Arduino Uno and ATtiny

Place your ATtiny on a breadboard like this:

20151121_152616_resized

Pay attention to your ATtiny chip. There should be a small dot indicating pin 0 on it.

My example is with an ATtiny 85 which is having the same pin layout as an Attiny13. Simply Google to find the datasheet of your specific ATtiny if you use another model. It will contain the pins layout.
Capture d’écran 2015-11-16 à 21.01.20

Do the following connections between your Arduino Uno and your ATtiny:
Arduino Pin 5V to ATtiny VCC pin
Arduino Pin GND to ATtiny GND pin
Arduino Pin 10 to ATtiny RESET pin
Arduino Pin 11 to ATtiny MOSI pin
Arduino Pin 12 to ATtiny MISO pin
Arduino Pin 13 to ATtiny SCK pin

ArduinoAsISP-breadboard

20151121_180322_resized

You also need to place a 10 uF capacitor between the Reset and GND pins on your Arduino Uno.
Of course, diagnosis the capacitor longer leg goes on the Reset pin and the shorter leg in the Gnd pin.

We will add a LED, more about to pin PB0 of the controller, because it will be used in our example program.
In my case, I used a red LED and a 220 Ohms resistor in series.

Step 4: Prepare program that will be uploaded to ATtiny

– Create a new program by choosing the following menu option: File | New
– Add the following import statements at the beginning of the file:

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

– Change the setup() function so it looks like this:

void setup() {
DDRB = 0b00000001;  // PB0 pin is an output pin
}

The DDRB register is used to configure which pins (PB0..PB6) are used as outputs. By default, all pins are input.
Some ATtiny have other ports than port B.
If the 2nd letter of pin names is a D, like in PD2, then that pin belongs to port D.
And, you would have to modify the DDRD register to set PD2 as an output pin.

Each bits of a DDR register represented one of the pins on the micro controller.
If we take DDRB on an ATtiny 85, then the bits are ordered that way:

PB5 | PB4 | PB3 | PB2 | PB1 | PB0
That why 0b0000001 means to turn on only the last bit, which is for PB0.

– modify the code of the loop() function so it looks like this:

void loop() {
PORTB |= (1 << PB0);  // Turn on LED
_delay_ms(1000);
PORTB ^= (1 << PB0);  // Turn off LED
_delay_ms(1000);
}

Same thing here. If the pin you would like to use as output is PD2, then your would have to do:

PORTD |= (1 << PD2);
...

As you may have guessed, this code does the same thing as the Blink code example (found under File | Examples | Basics | Blink) !

Step 5: Upload program to ATtiny

menu options

- Make sure you go in Tools | Board and select the right micro controller (ATtiny) and the right speed. Most chips I have seen are configured to run at 1 MHz by default.
- Select the right port (COM3 or something similar on Windows)
- Make sure you tell the IDE that you which to use an Arduino Uno as the programmer: Tool | Programmer | Arduino as ISP
- Compile and upload the code using the following menu option: Sketch | Upload

Your LED should now be blinking once per second.

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments Protected by WP-SpamShield Spam Plugin