Re-Programming the LilyTiny / LilyTwinkle
Introduction
The LilyTiny (and LilyTwinkle) are both great, low-cost, sew-able microcontrollers for eTextile projects. For most projects that only need to interface to a small number of LEDs or sensors, the LilyTiny is a great option.
The LilyTiny has 6 petals. Two are reserved for power(+) and ground(-). The other four are general purpose I/O pins (GPIOs). The LilyTiny is pre-loaded with a sample sketch which shows a variety of LED patterns on each of the pins:
- "breathing" pattern (pin 0)
- heartbeat pattern (pin 1)
- simple on-off blink (pin 2)
- random fade (pin 3)
This is a great place to start, but the only way to re-program one of these boards is to use an AVR Programmer and an ISP Pogo Pin Adapter to connect to the 6 exposed pins on the bottom of the LilyTiny board.
This is okay if you haven't already sewn your board into your project. If you have, we can still re-program your board. This tutorial will show you exactly how to accomplish this.
Required Materials
To follow along with this tutorial, you will need the following materials. You may not need everything though depending on what you have. Add it to your cart, read through the guide, and adjust the cart as necessary.
- 1x 8-Pin SOIC IC Test Clip
- 8x Male to Female Jumper Wires
- 1x Tiny AVR ISP Programmer
- 1x LilyTiny (or LilyTwinkle) to reprogram. The Protosnap LilyTwinkle Kit is a great option for beginners!
- 1x USB Extension Cable (Optional, but recommended)
There are a couple of ways to re-program your board. We will focus on one of the easiest methods here -- using the Tiny AVR Programming stick.
ATTiny Board Add-On Files
Before we start, we need to setup the Arduino programming environment to handle the ATTiny hardware. The ATTiny is not part of the "default" board files that comes with Arduino IDE v1.x. You will need to "install" these board files to your Arduino environment in order to re-program your LilyTiny.
Manually Installing the ATtiny hardware files
If you prefer to manually install the files, we will first need to install the ATtiny hardware board files. Download the zipped files from the GitHub repository to manually install the files.
Create a folder under your Arduino sketchbook called "hardware."
Locate your Arduino sketchbook folder (you can find its location in the preferences dialog in the Arduino software) - this is typically under Documents > Arduino
Create a new sub-folder called “hardware” in the sketchbook folder, if it doesn’t exist already.
Open (unzip) the attiny-master.zip file and copy the “attiny” folder (not the attiny-master folder) from the unzipped attiny-master.zip file to your new “hardware” folder.
You should end up with folder structure like Documents > Arduino > hardware > attiny that contains the boards.txt file and another folder called variants.h.
Restart the Arduino IDE.
You should see ATtiny entries in the Tools > Board menu.
Tiny AVR Programmer
Drivers
Follow the hook-up guide for the Tiny AVR programmer. For Windows / PC users, there are a few driver files that you'll need. For Mac / OS X users, the Tiny AVR Programmer should be plug-and-play ready.
Tiny AVR Programmer Hookup Guide
October 28, 2013
Hardware Hookup
While the photos show us using the 4-wire ribbon cables and the straight pin break-away headers to connect the SOIC clip to the Tiny AVR Programmer, we have found that the straight pin break-away headers (or any square pins) do not sit into the Tiny AVR Programmer very well. Instead, we recommend using the 12" Male-to-Female jumper wires to connect the SOIC clip to the Tiny AVR Programmer.
Make sure that the left side pins on the programmer are wired to the left side of the clip and the right side pins are wired to the right side of the clip. When you clip this onto the LilyTiny, make sure that the chip is right-side up. There are a few distinguishing marks to identify which way is up. On the programmer, there is a notch where an 8-pin chip would go. This should be up. And, on the LilyTiny, the Lilypad script "L" should be on the bottom.
When you are finished connecting the Tiny AVR Programmer to the ATtiny85, it should look similar to the image below.
You are now ready to re-program the LilyTiny or LilyTwinkle! The last step is to insert the Tiny AVR Programmer to your computer's USB port to begin programming.
Test Code - "Hello World!"
As with nearly every introductory Arduino project, we test our system with a "blink" program -- the equivalent to "Hello World!" in most other programming environments. First, we need to make sure the configuration is set properly in the Arduino IDE.
Step 0 - Open up the Arduino IDE
Open the Arduino IDE if you have not already.
Step 1 - Setting the Board Type
The LilyTiny has an ATtiny85 microcontroller on it. Change the board type in the Arduino IDE to correspond with this. The ATtiny85 can be set with either a 1 MHz internal clock or an 8 MHz internal clock. Be sure to select 8 MHz. Select: Tools --> Board --> ATtiny85 (internal 8 MHz clock)
Step 2 - Setting the Programmer
Because we are using the Tiny AVR as our programmer, we need the change the default programmer. This settings is also under the Tools > Programmer > USBtinyISP.
Step 3 - Upload Code
Copy the code below and paste this into your Arduino window.
language:c
int blinkPin = 0;
void setup()
{
pinMode(blinkPin, OUTPUT);
}
void loop()
{
digitalWrite(blinkPin, HIGH);
delay(500);
digitalWrite(blinkPin, LOW);
delay(500);
}
Click the upload button. You may see a few warning messages such as:
language:bash
avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny85
You can ignore this one. If everything is working, you should be able to see a blinking LED on GPIO 0 (pin 5 of the ATtiny). This can be on the Tiny AVR Programmer, the ProtoSnap LilyTwinkle Kit, or an LED that is attached to the ATtiny 85.
Notes on Programming the ATtiny85
The ATtiny85 isn’t your everyday Arduino microcontroller. It packs a lot of punch for its small size.
The following Arduino commands should be supported:
- pinMode()
- digitalWrite()
- digitalRead()
- analogRead()
- analogWrite()
- shiftOut()
- pulseIn()
- millis()
- micros()
- delay()
- delayMicroseconds()
- SoftwareSerial
All 5 pins are general purpose digital I/Os (GPIO). This means that they can be used with both digitalWrite() and digitalRead().
Pins 0 and 1 support PWM output (analogWrite).
Pins 2, 3, & 4 are tied to A/D on the chip (analogRead).
While the ATtiny85 supports most of the things you need, there are some things it can’t do.
No Hardware Serial (UART)
The ATtiny85 does not have a built in hardware UART. If you try to compile any Arduino code with Serial.begin()
or Serial.print()
you’ll get an error. However, there is a work around for this -- using Software Serial. This tutorial shows an example of how to do this:
Resources and Going Further
The TinyAVR Programmer is a real slick way to re-program the ATTiny45 or ATTiny85 chips. However, it does require that you install a few additional drivers. You can use this programmer directly with the Pomona 5250 SOIC clip. This programmer already has a debug LED on pin 0. We have a full hook-up guide for the Tiny AVR Programmer here.
Tiny AVR Programmer Hookup Guide
October 28, 2013
For more information regarding the ATtiny85, LinyTiny/Twinkle or more AVR programming tutorials, check out the following links:
- High-Low Tech Tutorial -- An overview of programming the ATtiny85 using an Arduino Uno as a programmer.
- Quick Reference Guide for ATTiny85
- GitHub LilyTiny / LilyTwinkle Repo
- Default Firmware
- ATtiny85 Board Definitions -- The attiny folder should live within a hardware folder in your Arduino sketchbook.
- Tiny AVR Drivers
- 32-bit USBTinyISP Driver -- Windows driver for 32-bit systems
- 64-bit USBTinyISP Driver -- Windows driver for 64-bit systems
Need some inspiration for your next project? Check out some of these related tutorials:
- H2OhNo! -- The H2OhNo! water alarm and development board uses an ATtiny85 to sense the presence of water. This tutorial goes deep into getting the ATtiny85 into a very low power mode.
- Shift Registers -- If you're feeling restrained by the ATtiny's lack of pins, you may be able to use a shift register to expand on that I/O count.
- Using the Arduino Pro Mini 3.3V -- If you're looking for small, but need more pins and functionality check out the Arduino Pro Mini.
- Installing an Arduino Bootloader -- You can use the Tiny AVR Programmer to program all sorts of AVRs, including those on most Arduino-compatible boards. If you ever find yourself needing to reprogram your Arduino bootloader, the Tiny AVR Programmer should be all you need.
Or check out these tutorials using the Attiny.