Introduction: Use a $1 ATTiny to Drive Addressable RGB LEDs
Arduinos are wonderful and easy to work with, but there are some projects where you need a microcontroller, or want to add some interactivity but don't really want to dedicate a $20-30 board!
At a little over $1/each (less in bulk) the ATTiny85 is a great chip for the job: it has 6 I/O pins and is capable of 16Mhz with minimal external components, is enough to work with many sensors, even drive popular addressable LEDs like Adafruit's NeoPixels, WS2811 strips.
The setup process is a little technical, but not too scary I promise, and you can use the familiar Arduino programming environment.
What you'll need:
* An ATTiny (this tutorial uses the ATTiny85-20PU)
* a USB ISP AVR Programmer (My tutorial uses this one)
* a breadboard
* some jumpers or wires (color coded wires are really helpful)
* a computer with the latest Arduino software installed (if you don't already have it, download here)
* a 5V wall wart (Old cell phone power suppliers work wonderfully, double check them for a 5V output. If you need one, these are plentiful at thrift shops!)
* 5V Addressable LEDs (this tutorial assumes use of 3-wire WS2811/WS2812)
Very helpful:
* .10 uF capacitor
* 1 low value resistor (ex 47, 100, 220 Ohm)
* A plain 1-color LED for testing
If you're looking for addressable RGB LEDs try Adafruit's Neopixel line or WS2811. They come in strips, pixels, segments, modules of many varieties. Working with 5V (as opposed to 12V) will be easier for this tutorial, so that's what I recommend!
You can try this sample code if you want to use your ATTiny with lights and a push button or touch sensor!
Step 1: Download & Install ATTiny Core
After you've installed Arduino you'll need to add support for the ATTiny. You can do this with the ATTiny core for Arduino. Installing this is a lot like installing a software library, but instead of putting it in a /libraries/ folder you'll make a /hardware/ folder inside of your Sketchbook
Download it here
Make sure Arduino is not running and follow the instructions in the ReadMe:
* Ensure the "hardware" folder exists under the Arduino Sketch folder. For
example, if the Arduino Sketch folder is...
C:\Projects\Arduino\
Ensure this folder exists...
C:\Projects\Arduino\hardware\
* Extract the contents of the archive into the "hardware" folder, so you have something like:
C:\Projects\Arduino\hardware\tiny\
Create a new file in this folder called boards.txt.
Open the Prospective Boards.txt file that came with your ATTiny archive. We'll need to copy the configurations we want to use in Arduino. The ones we need are for ATTiny85, specifically ATTiny85 @ 16 MHz (internal PLL; 4.3 V BOD) but feel welcome to add others if you think they'll be useful.
Save the boards.txt file and try starting Arduino. If you don't see ATTinys as an option in the Tools -> Board menu you may have to place the files in the Arduino program folder. I have some weird old machines and here are my workarounds:
If placing ATTiny core inside /sketchbook/hardware doesn't work you can try the instructions below
On my very old Mac
Browse to Applications -> Arduino, (right click), choose Show Package Contents. It should look like you're browsing a folder, navigate to Contents -> Resources -> Java -> hardware
Copy the tiny folder, boards.txt here!
On Ubuntu
Put your tiny folder and boards.txt in /usr/share/arduino/hardware
sudo cp -R /path/to/your/tiny/folder /usr/share/arduino/hardware
Step 2: Download & Install Neopixel Library
Again make sure Arduino is closed.
Edit:
Download Adafruit's Neopixel library, which includes support for the ATTiny! Download Link
Unzip, place it in your Arduino libraries folder and start Arduino! In File -> Examples, you should see NeoPixel and a 'standtest' example sketch!
Step 3: Connect Your ATTiny and Programmer
Following this schematic connect the pins from your programmer to the ATTiny on the breadboard.
If you get confused about orientation of your programming cable or microcontroller (like I do), some hints:
ATTiny: look for a dot or bubble, this should be over the RESET pin (1)
Programmer cable: look for an arrow on the side of the ribbon cable, this is pin 1. Or you can find VCC and Ground via attaching wires and testing with a multimeter.
Schematic CC-BY-SA aurelient / Fritzing
Step 4: Test the Blink Sketch
Connect your programmer and start Arduino.
By default ATTiny runs at 1Mhz. To use the addressable lights, we need to set the fuses to 8Mhz or 16Mhz.
In Tools -> Board, select either
ATTiny85 8Mhz (Internal Oscillator) or ATTiny85 16MHz (Internal PLL), then in Tools choose Burn Bootloader (you'll only have to do this once per chip).
WS2811 pixels work with ATTiny at either speed. 8Mhz is fine for most things and will save you a tiny bit of power. The 16Mhz profile has brown-out detection enabled and will cut out at 4.3V. If you plan to run your lights at less than 5V, always use the 8Mhz profile. Read more about microcontroller fuses and brown-out detection.
Once this is done you can try uploading a sketch to make sure everything works correctly.
Choose Examples -> Basics -> Blink
Change the LED pin from 13 to 4.
Connect a simple LED, the positive end to digital pin 4 on the ATTiny and the negative to ground.
Upload and you should have a blinky LED!
Step 5: Setup Your WS2811 LEDs and Try Your Sketch
Upload your sketch
Connect your programmer and start Arduino
Open Examples -> NeoPixel -> standtest
Change the first parameter to the number of LEDs you have and the second (pin number) to pin 4.
The example below drives 10 LEDs on pin 4, though I was able to drive over 2 meters of high density strip (143 pixels) on one chip :-)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(10, 4, NEO_GRB + NEO_KHZ800);
Setting up your LEDs
If you're using one or two LEDs you probably can test them directly without an external power supply. Connect your LED's GND, 5V pins to 5V, GND on your breadboard.
Place a low value resistor between ATTiny pin 4 and your LED DATA/DIN Pin, this will help with signal integrity (if you want to a more thorough explanation, this post is really informative).
If you're using a strip or more pixels you'll need external power. As above connect ATTiny pin 4 to your strip with a low value resistor.
Connect your power supply's 5V, GND to the LED strip. You'll also need to tie the power supply GND to the GND pin of your ATTiny. See the schematic image for an example setup.
Step 6: Sit Back and Enjoy
Fingers crossed all of these steps worked and you're watching a light show right now! Look for some new Instructables soon with additional projects.
If you have improvements for this tutorial please let me know, I'm sure there are a few things I've missed!
Enjoy your light up creations and happy hacking :-)
If you're looking for some sample code you can give this a try, it is intended to switch between patterns on the tiny with a push button on pin 0!
43 Comments
2 years ago on Step 1
Hi Thank you very much. I've been trying to do this for several years now. I'm using an Omega MP shield. I was missing the burning the bootloader part. work perfectly now!!!
Question 2 years ago
i have a USB TTL 232R-5V-WE and Attiny13a will it Work ?
2 years ago
Thanks for the info!
It gives me lots of ideas! ;)
2 years ago
I am constantly getting nothing from my attiny85 connected to my 2811s. I programmed with my Arduino Uno on a POP os linux laptop. If I retask the Arduino Uno it runs the same code perfectly. It is some issue with the attiny85. I may try re-burning the boot loader on the Attiny85.
Tip 2 years ago
Great tutorial, but did not work for me at first. I did not know that you had to burn the boot loader.
5 years ago
Very nice tutorial! I'm somewhat familiar with neopixel's library, but i'm trying to lit a 5m strip w/ 300 leds total, but I can only make the 110 first to lit. Could be some kind of ATTiny limitation? I do not have a UNO board right now to try out.
Reply 3 years ago
Hi! Sorry for the (2 year) delay. I was seeing the same thing with a 5M string. At first I thought it might be a timing issue. But I also use an ATTiny85 to drive an 8x8 LED matrix - which worked well until I tried scrolling longer text messages across it. After which the matrix would go dark, just like my 5M string beyond trying to drive 110 pixels. Since the nature of this task never involves more than 64 pixels, it got me to thinking... Now I suspect it is running out of buffer space. The library display functions save all commands in memory and only send the results out when Pixels.show() is called. The ATTiny85 has limited memory storage. I'm working on a few "creative" ways to address pixels out beyond #110. One idea I'm toying with would send 0's out directly (via a loop) followed by the buffer contents. You could then skip the first "X" pixels (via a specified loop counter and then dump the buffer to output a separate group of 100, skipping the proper # of pixels each pass until all 300 have been sequenced. By sequencing groups of 100 quick enough one could "multiplex" out to 300. Suggestions? Ideas?
Question 5 years ago
Hello! In a project I would like to use a microcontroller dedicated to drive the 4 NeoPixel LEDs chains and receive data via I2C from another uC (which LEDs to drive).
I think Attiny85 falls a little short for this, I like Attiny1617 though.
Do you think it would work with the libraries NeoPixel or fast LED?
Thanks!
8 years ago on Introduction
I am stuck and cannot find anything that will tell me why everything works through the blink test but the LED's wont work after that . I have 5 programmable WS@811 Driver LED's linked in series with an ATTiny85 pu10 set to be loaded at 8 MHz. I have modified the strandtest example for my correct pin. When I run it all lights go full brightness white. No other change happens. Any ideas? Thanks.
Reply 8 years ago on Introduction
Here are a few things you can try:
* For the ATTiny, select the 8Mhz configuration in the boards menu and then try setting the fuses again (you can do this via 'Burn Bootloader' in Arduino IDE). Then re-upload your sketch.
* Double-check the wiring your WS2811s, make sure GND, 5V and Data are all correctly connected. Make sure output from microcontroller is going to Data In. Try your code on a full Arduino board to make sure the lights are okay.
* Try another library. I believe FastLED supports ATTiny
Reply 5 years ago
This was the exact problem I've been having for the past couple of hours, and this fixed it thank you so much!
Reply 8 years ago on Introduction
Thank you for the prompt reply and guidance! I had already tried everything but the bootloader. I bet that is it. I didn't catch that previously. Can't wait to try it out tonight.
5 years ago
Thank you very much for this tutorial. It has been really useful for me.
I am a beginner and I write you because I'm having problems trying to use a build a vu meter. I am using a microphone, ATtiny85 and 9 neopixels. My script works with Arduino but not with ATTiny. I have followed this tutorial having success using an example from Adafruit neopixels. My project also worked using ATtiny and "normal" LEDS... and I really don't know where the problem is when I try to work with neopixels.
Some data from my project: Input voltage in PB2 from microphone 0.88V ; Output voltage in PB1 (to pixels data in) 0.35V and it doesn't vary too much with the input voltage.
I will appreciate very much any idea you can give me. Thanks!
7 years ago
Thanks for this tutorial! Programming the ATTiny85 can sometimes be tricky, but you can do cool stuff like this with it.
8 years ago on Step 5
What is the purpose of the capacitor in the circuit?
Reply 8 years ago on Step 5
The mouse over on the schematic calles it a "Decoupling capacitor". http://en.wikipedia.org/wiki/Decoupling_capacitor
Reply 8 years ago on Introduction
The decoupling capacitor is helpful when there is noise / fluctuation in your power supply. It can also be a good practice to put a pull-up resistor on your reset line to keep it from floating and your board resetting. It will likely work without these things but can be a good practice.
Adafruit has some nice best practices for the lights themselves:
https://learn.adafruit.com/adafruit-neopixel-uberguide/best-practices
8 years ago on Introduction
hi! first off, great writeup! I have a question: So i'm not using just an Attiny85, but instead an Olimex85s arduino-compatable board that has the attiny built in. Since the people at olimex designed the board with a bootloader, I had to remove a couple modes from your sketch since I was using 120% of space on the chip. I was able to bring the size down and upload the sketch, but what i'm seeing though is that the mode only changes when the button is held down, then when released, it defaults back to the lasrson scanner mode, i'll hold the button down again, and then it'll change to another mode, but when released, back to the larson scanner. So my question is, did you write the code to change modes on a single button press, or when, and only when, the button is held down? thanks!
8 years ago on Introduction
I'm having trouble programming the ATTiny85. Details here. Any ideas?
https://forums.adafruit.com/viewtopic.php?f=20&t=71466&p=362416#p362416
8 years ago on Introduction
This was very helpful for me to get my first AtTiny project off the ground a few weeks ago. I made something else using this knowledge two days ago and posted the instructable at https://www.instructables.com/id/Fake-TV-Burglar-Deterrent/