So here you have this long-awaited post.
In this article we will see how to program some of the Atmel ATtiny family, particularly those highlighted in the title, using Arduino libraries and Arduino Uno as ISP. Obviously, everything can be done using a dedicated programmer, but here we will not talk about this subject because the differences are minimal.
Let’s start with a premise: on the ATtiny we will not upload the bootloader, but we will proceed to load our sketch directly into the memory of the microcontroller. So this is a first difference with the ATmega328. There are perhaps some ATtiny bootlaoders, but it doesn’t make sense to load them for several reasons. The first is the small memory of this microcontroller (ATtiny85 has 8Kb compared to 32Kb of the Atmega328). Secondly, most of the ATtiny do not have serial hardware, so it would make not sense to install a bootloader if it doesn’t help to do uploads via serial port.
Let us now see the advantages of the ATtiny85 compared to Atmega328:
1 – lower power consumption (and therefore suitable for projects with battery)
2 – smaller size (the ATmega328 has 28 pins while the ATtiny85 has only 8 pins)
3 – lower cost
Obviously with fewer pins we have a limited number of I/O compared to big brothers.
As regards the programming is very similar to that seen in the previous post and the one before that.
Let’s see the pinout of ATtiny85 that we will use as starting example in this article:
From Arduino point of view:
+-/\-+ Ain0 (D 5) PB5 1| |8 VCC Ain3 (D 3) PB3 2| |7 PB2 (D 2) INT0 Ain1 Ain2 (D 4) PB4 3| |6 PB1 (D 1) pwm1 GND 4| |5 PB0 (D 0) pwm0 +----+
So if I want to use the PIN6 as a digital output I will use pinMode (1, output) and if I want to read the analog input from PIN3 I will use analogRead (A2).
Of course, as in Arduino, we can use analog inputs as digital outputs.
The serial hardware does not exist as we said, but we can use the appropriate PIN2 (PB3) as unidirectional software serial. Practically using the standard Arduino command Serial.begin (9600), on PIN2 we have a TX signal that can connect to RX pin of an USB-SERIAL converter, in order to communicate from the ATtiny to the PC, so it’s like having a “read-only serial”.
But let’s get to the point: compared to the programming of the Atmega328, the programming of the ATtiny requires some changes and the adding of some libraries to the IDE.
I recommend using the 0023 version of the IDE, which can be downloaded from the website of Arduino (with other versions it was unsuccessful), and create a separate special folder to be used only ATtiny programming.
So if we already have Arduino (for example in the c:\Arduino folder), we will create a new folder called for example c:\Arduino-Tiny.
After this we have to download the core arduino-tiny-0022-0009.zip, from here, then we have to download the library PinChangeInterrupt-0001.zip from here and finally the library TinyTuner-0003.zip found here. The first file needs to be unzipped in hardware folder (eg: c:\Arduino-Tiny\hardware). It will ask you to overwrite some files. Answer yes without problems. The other two libraries need to be unpacked under the libraries folder (eg c:\Arduino-Tiny\libraries). At this point it should be alright. You can either upload your examples using the following scheme:
Basically, it’s like the ATmega328. The pin 10 need to be connected to RESET, and pins 11, 12, 13 to pin MOSI, MISO, SCK (ie in this case 5, 6, 7), and of course the power supply.
Remember as always to insert the 22uF capacitor between GND and RESET on Arduino UNO otherwise the board will reset and the upload will not work giving you an sync error!!!
At this point, opening our IDE, and an example sketch, we can select Tools -> Boards -> ATtiny85 @ 1Mhz, then File -> Upload to I / O Board.
It will give you an error of type “PAGEL,” but you can safely ignore it. You will then see the message “Done Uploading” that will warn you that everything was successful.
As you can see it is very simple.
The ATtiny by default have fuse set in a way that the microcontroller uses its internal oscillator at 1MHz. And this is the best option for most projects, but if you need more speed, you can program the ATtiny in such a way that it uses its internal oscillator at 8MHz or by using an external oscillator at 16Mhz (see previous posts) . If we want change our ATtiny speed, for example at 8MHz, we need to set the fuse, doing an upload of a empty bootloader, containing only the settings that interest us. To do this, simply select from Tools -> Boards the setting of interest (eg ATtiny85 @ 8MHz), and then Tools -> Burn Bootloader —> w/Arduino as ISP.
At this point you will probably get the following error:
avr_read(): error reading address 0x0000 read operation not supported for memory "lock" avrdude: failed to read all of lock memory, rc=-2
This error is due to the fact that the is missing the line that concerns the operation memory lock in file c:\Arduino-Tiny\hardware\tools\avr\etc\avrdude.conf the line that concerns the operation memory lock.
So we are going to edit this file and add the following line:
read = "0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0", "0 0 0 0 0 0 0 0 o o o o o o o o";
Under ATtiny85 section, below the line memory “lock” that should look like so after the change:
# ATtiny85 has Signature Bytes: 0x1E 0x93 0x08. memory "signature" size = 3; read = "0 0 1 1 0 0 0 0 0 0 0 x x x x x", "x x x x x x a1 a0 o o o o o o o o"; ; memory "lock" size = 1; write = "1 0 1 0 1 1 0 0 1 1 1 x x x x x", "x x x x x x x x 1 1 i i i i i i"; read = "0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0", "0 0 0 0 0 0 0 0 o o o o o o o o"; min_write_delay = 9000; max_write_delay = 9000; ;
Now running the bootloader operation should go.
If we want to we can fix also the problem of the message “PAGEL” by adding the following lines:
pagel = 0xB3; bs2 = 0xB4;
Below the line chip_erase_delay = 4500 in the Attiny85 section. This should look like so after the changes:
#------------------------------------------------------------ # ATtiny85 #------------------------------------------------------------ part id = "t85"; desc = "ATtiny85"; has_debugwire = yes; flash_instr = 0xB4, 0x02, 0x12; eeprom_instr = 0xBB, 0xFF, 0xBB, 0xEE, 0xBB, 0xCC, 0xB2, 0x0D, 0xBC, 0x02, 0xB4, 0x02, 0xBA, 0x0D, 0xBB, 0xBC, 0x99, 0xE1, 0xBB, 0xAC; ## no STK500 devcode in XML file, use the ATtiny45 one stk500_devcode = 0x14; ## avr910_devcode = ?; ## Try the AT90S2313 devcode: avr910_devcode = 0x20; signature = 0x1e 0x93 0x0b; reset = io; chip_erase_delay = 4500; pagel = 0xB3; bs2 = 0xB4; pgm_enable = "1 0 1 0 1 1 0 0 0 1 0 1 0 0 1 1", "x x x x x x x x x x x x x x x x";
In this way, the error is corrected but only on Attiny85. To solve the problem for other microcontrollers, here I have given you a starting point. If you do a search on google with the error appearing during the bootloader upload you will find the solution.
There may be an additional error (I can not remember the message) when you upload very large sketchs.
In this case, I remember it was related to a AVR-gcc bug, and in order to resolve it, I needed to download the WinAVR tools, install them, and copy the contents of the folder c:\WinAvrtools in c:\Arduino-Tiny\hardware\tools replacing all files (except previously edited file avrdude.conf).
This last part is a bit more complicated, but you will see that if you will follow everything it will not be difficult.
At the next post then!