Fads to Obsessions and Beyond...




Free domain for life, exceptional technical support, website transfer

Learning ATtiny Microcontrollers

The table below lists specific items, components and or parts in relation to use or interfacing with various ATtiny microcontrollers. The remainder of this page gives general background information about using and programming ATtiny microcontrollers.

Click column title to change sort order
ATtiny Part Number Component/ Project Description Data Sheet
ATtiny85 Blink LED Demonstrate programming the ATtiny85 to blink an LED ATtiny85
ATtiny85 Buttons Demonstrate button interfacing with ATtiny85 and software debouncing methods ATtiny85
ATtiny85 Serial Comms (USB-TTL) Demonstrate serial communications with ATtiny85 with USB-TTL converters/PC Com ports ATtiny85
PCF8574 PCF8574A 8-bit port expander Demonstrate interfacing ATtiny85/Digispark with I2C using the PCF8574A 8-bit port expansion chip. PCF8574
HCSR04 HC-SR04 Ultrasonic Sensor Demonstrate interfacing ATtiny85/Digispark with the HC-SR04 Ultrasonic Sensor, distance readings in cm via RS232-USB to PC. HCSR04

Programming the ATtiny (Arduino IDE)

There are a variety of methods for progamming ATtiny microcontrollers, but I use the Arduino IDE and Uno as an ISP programmer (since I have a Uno on hand and therefore don't need to outlay money for a dedicated hardware programmer).

Also, since Digispark clones (an ATtiny85 on a small breakout board with PCB traces enabling inserting into a USB connector) are actually cheaper to purchase than stand-alone ATtiny85 chips, "direct" programming from the Arduino IDE via USB and a bootloader is also used. The USB/bootloader is convenient (don't need to have the Uno/ISP programmer connected) but this takes up ~2K of code space, of which the ATtiny85 only has ~8K to begin with.



Programming ATtiny85/Digispark via Arduino IDE

The Digispark has a bootloader installed that allows the breakout board to act as a USB device which then can be programmed using the Arduino IDE. If the bootloader has been erased/corrupted and or you are using a bare ATtiny85, instructions for uploading the bootloader are in a following section.

Once the Arduino IDE/Digispark files have been installed, programming a Digispark board (or ATtiny85 with installed bootloader) simply involves selecting "Digispark (Default - 16.5mhz)" via the Ardinuo IDE Tools|Board menu, entering the appropriate sketch in the editor window and then clicking the upload button.

The bottom status box on the Arduino IDE will display the results of the compiling of the sketch, and if successful will ask to plug in the Digispark/ATtiny85 (which you have 60 seconds to comply). After insetring the Digispark to the USB connector, the upload progress will be displayed in the Arduino IDE status box, and upon successful completion (after a 5 second delay) the uploaded code on the Digispark/ATtiny85 will start running.

Upon subsequent insertions of the Digispark into a USB powered socket (or power-up via other suitable source) the previously uploaded firmware will run after a 5 second delay (during which the bootloader is accessing if you are trying to reprogram the chip).

Programming ATtiny85/Digispark via Arduino as ISP

Atmel microcontrollers such as the ATtiny85 do not need a "high voltage" (generally 12V) to force the target microcontroller into programming mode. The ATtiny85 can be programmed using "normal" logic level voltages (i.e. 5V) which is called In System Programming (ISP). ISP is effectively an SPI interface with the microcontroller RESET pin used as SPI Slave Select, which can be implemented on any microcontroller that supports SPI. The Arduino community has created a sketch called ArduinoISP, which allows an Arduino board such as the Uno to behave as an in-system programmer.

Program Uno as ISP

Using the Arduino IDE select the ArduinoISP sketch using the menu selection Files|Examples|11.ArduinoISP|ArduinoISP

With the Tools|Boards menu select "Arduino/Guniuno UNO" as the board

Plug the Uno into the USB port (check in Tools|Port that the correct port is selected for the Uno)

Click the "upload" icon and the sketch will be uploaded to the Uno, which can then be used as an ISP programmer

Using Uno/ISP to program ATtiny85/Digispark

These steps are for programming the ATtiny85/Digispark with an Uno programmed to act as an ISP. This is not loading the bootloader (this will actually over write the bootloader if present), this is just loading IN GENERAL compiled code into the ATtiny85/Digispark with the Uno.

Connect the Uno to the ATtiny85/Digispark as per the following diagram.

ISP programmer

The pin connections are as follows:

Enter/select the sketch in the Arduino IDE (eg FILE|examples|01 Basics|Blink)

Using the Arduino IDE in the Tools menu, select the Board = "Attiny 25/45/85", Processor = "Attiny85", Clock = "Internal 16Mhz".

If these selections are not available in the Arduino IDE, you will need to download and install using the Arduino Boards Manager (see following section).

Using the Arduino IDE menu TOOLS|Programmer|Arduino as ISP (note the “as ISP” not “ArduinoISP”).

Plug the Uno into the USB (and then check that TOOLS|PORT is connected to the Uno port).

Click “upload” and then the sketch will be uploaded to the ATtiny85/Digispark via the Uno.

Installing ATtiny boards into Arduino IDE

Using the Arduino IDE open File|Preferences to open the dialog window

Click the "Additional Boards Manager URLs" icon, and then add to the following URL to the list https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json

Select the Tools|Boards|Boards Manager and then allow the boards list to load, and then search/filter search for ATtiny, and then Install ATtiny by David A. Mellis",?

Click "close" and the Arduino IDE now will allow selection of ATtiny boards for programming via ISP.

Loading Micronucleus bootloader into ATtiny85

Ensure that the UNO (programmed as an ISP) and connected to the ATtiny85 as per instructions above

Have a copy of the file “t85_default.hex” in the following location on the hard drive C:\Program Files\Arduino (or other as desired, but change the directory paths as appropriate in the avrdude command line). The “t85_default.hex” is the compiled Micronucleus bootloader and is available from the digistump site.

Open a cmd.exe (PC windows - > start | run) and then enter the following command line, which is all one line when entered into the cmd.exe window (changing the paths as necessary for your actual file locations)


    "C:\Program Files\Arduino\hardware\tools\avr/bin/avrdude" 
    -CC:"\Program Files\Arduino\hardware\tools\avr/etc/avrdude.conf" -v -pattiny85 -cstk500v1 
    -PCOM3 -b19200 -Uflash:w:"C:\Program Files\Arduino/t85_default.hex":i 
    -U lfuse:w:0xe1:m -U hfuse:w:0xdd:m -U efuse:w:0xfe:m
    

Plug in the UNO to the USB on the PC and then press ENTER when in the cmd.exe window after the command line (previous step) has been entered

The progress of the upload will display in the command window with the final “avrdude done. Thank You.” showing completion. Micronucleus bootloader should now be loaded on the ATtiny85/Digispark.

Now when plugging the ATtiny85/Digispark into the USB port on the PC, should be recognized as a device (if digistump drivers previously installed).

Now can use Arduino IDE to load sketches into ATtiny85/Digispark using the bootloader/USB connection.

References

Comments/Questions

No comments yet.

Add Comment/Question

Only Logged-In Members can add comments

"If we could sell our experiences for what they cost us, we'd all be millionaires".

Please donate any amount to help with hosting this web site.

If you subscribe (only $2/annum) you can view the site without advertisements and get emails abouts updates etc.