Introduction: Program an ATtiny44/45/84/85 With Arduino
This tutorial gives provides the steps to set up, program, and test an ATtiny44/45/84/85 with an Arduino. It is also provides some explanations as to the inner workings of AVR chips in general.
Step 1: AVR and ISP Explained
Many tutorials can show you how to do something. I would like to start off by giving a basic explanation as to why you are doing what you are doing.
This process is showing how to use an Arduino Uno as an In-System Programmer or ISP. An ISP allows AVR microcontrollers to be programmed and reprogrammed without having to remove them from the circuit. Programming any AVR microcontroller six wires are needed. Three of these wires are referred to as the Serial Peripheral Interface (SPI) and are the Master In - Slave Out (MISO), Master Out - Slave In (MOSI), and Serial ClocK (SCK). The "Master" is the ISP or the device you are using to program the AVR chip. The "Slave" is the AVR chip being programmed. The other three wires are for the 5V power supply (VCC), Ground (GND), and Reset (RESET).
The images above illustrate which pins on the ATtiny correlate to which function when programming them. The SCK pin is where the Master provides the clock information for communication. Every pulse of the SCK pin sends one bit of data over both the MOSI and MISO pins (this is essentially the ATtiny and Arduino communicating back and forth). The GND pins of both the Arduino and AVR should be connected to help the chips establish the same reference voltage. The RESET pin is the channel to which the Arduino is able to erase the contents on the AVR chip and enable serial programming. The VCC pin is connected to the Arduino simply to remove the need for batteries or external power supplies.
When you come to set up your Arduino to act as an ISP (you will see how to do this later in the tutorial) you are basically informing the Arduino how to format the code and over which pins it should send the data. You can buy 6 pin serial ports to program the AVR chips but if you already have an Arduino than this is a convenient method for you.
Step 2: Parts
ATtiny45 or ATtiny85
Insulated Jumper Cables
10 µF Capacitor
Resistor (Anywhere from 220 - 330 Ohms)
Step 3: Add Support for ATtiny
First you want to open File > Preferences.
Then, at the bottom of the pop up menu where you see "Additional Boards Manager URLs" you want to copy and paste "https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json" (without the quotation marks).
Now click OK.
Navigate to Tools > Board > Board Manager.
Scroll down to the bottom and you should find "attiny". Click the install button and the words "Installed" should appear when the task is complete.
To confirm that you have added support for the boards, navigate to Tools > Board and you should see ATtiny at the bottom of the list.
Step 4: Setup Arduino As ISP
Navigate to File > Examples > ArduinoISP. Open the sketch and upload it to your Arduino.
Step 5: Connect Components
First you will want to connect the 10 μF Capacitor to the Ground and Reset Pins on the Arduino. Make sure the ground side of the capacitor (Typically the side with a silver stripe) goes into the Ground pin on the board. This will help act as a filter and stop the Arduino from resetting itself when programming the ATtiny.
Next place the ATtiny on the breadboard. The gap in the center is made for different Integrated Circuits to be able to span it. Make sure the small dot on the ATtiny is in the top left hand corner as this is how we will be referencing it in the tutorial.
Now we will hook up the jumper wires. Because there can be confusion due to the physical pins being numbered different than the digital output pins, I will be referring to them based on their property (VCC, MISO, GND, etc.) but the graphic above will help remind you which is which.
- Arduino Pin 13 ---> SCK
- Arduino Pin 12 ---> MISO
- Arduino Pin 11 ---> MOSI
- Arduino Pin 10 ---> RESET
- Arduino 5V ---> VCC
- Arduino Ground ---> GND
Step 6: Burn Bootloader (Optional)
This is an additional step that is required if you plan to use the SoftwareSerial Library (though I do this to all of my chips so I don't have to worry about it in the future). The ATtiny runs at 1 MHz by default. You will need to change this to be 8 MHz so that it can communicate with the Arduino at the same frequency.
To do this, first select "Arduino as ISP" under Tools > Programmer.
Select ATtiny from Tools > Board.
Select the specific ATtiny you are using (44/45/84/85) from Tools > Processor.
Next select "8 MHz (Internal)" from Tools > Clock. It is important that you select Internal. Do NOT select "external" as your ATtiny will not work unless you attach an external clock to it (The ATtiny will keep these settings until you explicitly change them).
Finally, select "Burn Bootloader" under tools and wait for it to complete.
Step 7: Upload Code
Now you can start uploading code.
A good first program to try out your new chip is the Blink program found under examples. You will want to change the pin value to something between 0 - 4. The image above shows you which digital pins correspond to which physical pins. The digital pins are what you will be inputting into Arduino. For this example I made it digital pin zero.
Make sure you have the correct Board, Clock, and Processor selected under the Tools Menu for what you are using. Then hit upload.
Not all functions on Arduino will carry over to the smaller ATtiny chips. Sparkfun provides a good cheat sheet for some basic functions and is attached above.
Step 8: Test ATtiny
To test the ATtiny, attach your resistor and LED in series to the pin you specified in your program. In this case it was digital pin 0 and physical pin 5. Make sure to ground both the ATtiny and your LED. In this case I used the Arduino for both Ground and 5V out of convenience. Once you connect the 5V from the Arduino to the microcontroller it should start blinking (You could also attach this microcontroller to another power supply and add a 5V regulator).
How do you know what resistor you need? Well it is a safe bet that most LEDs will function with a resistor between 220 and 330 ohms (with a higher resistance providing a dimmer light). If you wish to calculate the resistor yourself you would need to use the equation V = IR where V is the difference in voltage between what is supplied and what is used, I is the current drawn by the LED, and R is the resistance of the resistor you would need to make a safe circuit. An example calculation is shown below where 5 volts is provided by the Arduino, 3 V is used by the LED, and 12 mA are drawn by the LED:
V = IR
R = V/I
R = (5 - 3) / (12 / 1000)
R = 166.7 Ohms
It is safe of course to use a resistor that is higher than what was calculated. The value that is calculated is the minimum that is necessary.
I removed the wires used to program the ATtiny from the above pictures to make it easier to see but it is not necessary to test your chip.
Step 9: ATtiny44/84
To program an ATtiny44 or ATtiny84, follow all the same steps but make sure you hookup the MOSI, MISO, GND, SCK, VCC, and RESET with reference to the image above.
Step 10: References
Bamaan made it!