Wednesday, January 1, 2014

Not music related, but...

I know it's been a long while since I posted here. I've been pretty busy with other things, but here's some stuff I did today.

Back in August, I thought it would be good to introduce my eight-year-old daughter to kit-building and soldering, so I ordered the It's a Through-Hole Christmas, Charlie Brown! christmas ornament kit from Sparkfun. It's an easy-to-build kit using LEDs, a resistor, and a pre-programmed ATTiny85 microcontroller (unfortunately, Sparkfun no longer makes it).

Construction of the kit went well, my daughter got an intro to soldering, and when we powered up the ornament for the first time, it lit up! And different lights faded in and out! My daughter and I exchanged high fives.

And then I noticed that after the initial light fade in-out sequence, the ornament seemed to freeze up. After a little googling, it looks like there is a bug in the firmware that Sparkfun shipped on the ATTiny85 chip.

With most vendors, I would be mightily peeved that I was sent something that didn't work as advertised, except:
  • I'd already gotten a great deal of value from building the kit with my daughter,

    and
  • Everything about this kit - schematics, PCB layouts, code, is freely available on github

    oh, and
     
  • I love Sparkfun
So, I went digging, and, if you've got an Arduino laying around, it's really easy to use it to reprogram the ATTiny85. First, a little background on the chip.

The ATTiny85

For those so don't know what an ATTiny85 is, quoting from the Atmel website:

The high-performance, low-power Atmel 8-bit AVR RISC-based microcontroller combines 8KB ISP flash memory, 512B EEPROM, 512-Byte SRAM, 6 general purpose I/O lines, 32 general purpose working registers, one 8-bit timer/counter with compare modes, one 8-bit high speed timer/counter, USI, internal and external Interrupts, 4-channel 10-bit A/D converter, programmable watchdog timer with internal oscillator, three software selectable power saving modes, and debugWIRE for on-chip debugging. The device achieves a throughput of 20 MIPS at 20 MHz and operates between 2.7-5.5 volts.

If you're familiar with Arduinos, this chip uses the same instruction set as the processor in the Arduino family, but has half the EEPROM, a quarter of the RAM, and far fewer I/O lines. The chip costs $1.32 in single quantities (Digi-Key price), and is made in through-hole versions like the one shown below, so it's a great thing to use in projects where you want to embed a cheap but capable CPU.


Reprogramming the ATTiny85

With a full-featured Arduino board, you can upload new code to the processor (usually an Atmel 328) via a USB port implemented on the Arduino board. With the bare ATTiny85 chip, there's no USB support, so we need to upload the code using a special protocol that allows you to read and write the flash memory on the chip using one of the 8 pins.

If you're doing this a lot, there's a nice kit from Sparkfun that allows you to plug the chip into a socket and use the Arduino IDE to directly upload sketches to the chip. Or, you can use an Arduino and a couple of extra parts to accomplish this.

There are a lot of good tutorials on the web like http://www.instructables.com/id/How-to-program-attiny-using-arduino-uno/ that show you how to wire up an Arduino as an ISP (in-circuit programmer) that can program the ATTiny85. I followed that tutorial, and here's the result:


The Arduino Uno I used as a programmer is at the top of the photo. The breadboard to the left is where the ATTiny85 is plugged in to be programmed.

Being the nerd I am, I wanted to be able to pull the ATTiny85 chip out of its programmer and test it with the least hassle. So the larger breadboard to the right is a copy of the Sparkfun ornament circuit. After uploading new code to the chip, I just unplug it from the programming breadboard on the left and plug it into the test breadboard on the right.

Results


Here's the new code (that I wrote) running on the ornament. In the future, I'll sit down with my daughter and let her tweak a couple of the timing parameters, or maybe add back the fade in-out, implemented with PWM, and see what she picks up about coding.

video




Happy New Year!