Sunday, February 23, 2014

Driving 4 RGB LEDs from an ATTiny85

I'm not going to do anything with this circuit right away and I'm going to tear it down to use the board for something else, so I'm squirreling it away here.  On the left are lines from the ATTiny85.
I originally just laid this out R/G/B on all lines, but when I started driving them, I started getting the red LEDs coming on due to current looping through when driving other channels.  This is because the red LEDs have the lowest forward voltage and at 5 volts two of them can glow in series so for instance if I try to turn on the LED0 green, I put + on PB0 and GND on PB2, but if there's a red LED between PB0 and PB1 and another between PB1 and PB2, they can come on as well.

This uses my usual cheat of using the fact that only about 120 ma of current will go through the ATTiny, and I'm multiplexing these so they can't be on more than about 20% of the time at most so on average I'm staying within the LED specs.

The code for this is "4xRGB-test.c" (and .hex) at

Here's a video:

Saturday, February 15, 2014

The first new blinkie of 2014

For the last few years I've been running a "learn to solder" room at a Chicago area science fiction convention, where members can put together a little wearable circuit board with blinking LEDs, known locally as "blinkies."  For 2014 we will have probably two new blinkies.  The first is intended to be the basic unit, low priced and very easy to build but still fun.

Since price is such an important consideration, more time is put into reducing costs than if only one unit was to be made for fun.  The smallest, cheapest CPU that can do the job is used, and as much of the work is done in software as is possible.  The circuit is designed to run the desired number of LEDs with as few pins of CPU I/O as possible.

The generally accepted method of controlling as many LEDs as possible with a fixed number of output pins is called "Charlieplexing" (because it was invented by a fellow named Charlie).  It relies on the fact that LEDs only work if power is applied in one direction across them, combined with the fact that if you flash lights very fast, your eye thinks they're on constantly.  It also uses the fact that microcontrollers can put their output pins into THREE different states - on, off, and disconnected.

A downside to Charlieplexing is that the software to drive it is a lot more complicated than simpler solutions that use more wires.

I've used Charlieplexing in the past, but for this particular blinkie I didn't need to drive that many LEDs, 9 to 12 was all I really wanted.  This also greatly reduces the complexity of the software.  What I used is basically the same thing but I've left out some of the more complex cases that are normally used to get a few more LEDs running.

Here's the circuit that I used for this blinkie:

Before I go further, I should point out that I am well aware that this circuit SHOULD have resistors in series with all the LEDs.  However, I am taking advantage of the fact that not only can the CR2032 battery not source more than about 120ma and the CPU can't sink more than that much anyway, the LEDs are only on at maximum about 1/4 of the time, so on average the maximum current rating of the LEDs is not exceeded.
This circuit should NOT be taken as an indication that you can just hook LEDs up straight to power without limiting the current to them in some way.  You will fry LEDs if you hook them up to a power source that can deliver more than their maximum current.

You can see that the nine LEDs are set up in three groups of three.  The top LED in each group I refer to as bank 0, the others banks 1 and 2.  To drive bank 0 (LEDs 0, 1 and 2), pin 7 (PB2) on the chip is set to low (ground), and pins 5 (PB0), 6 (PB1) and 3 (PB4) are set high for each LED that is to be lit.

After a bit of time, pin 7 (PB2 again) is changed to high (+voltage), and the other pins are now set LOW for each LED that is to be lit - note that for instance D3 is hooked up exactly like D0 but backwards.

After some more time, pin 7 is set to floating (it's not either ground or +), and instead pin 2 (PB3) is set to low and one more bank of 3 is driven in the same way.  A fourth bank could also be driven on PB3 but nine LEDs is all we want for this circuit.

In order to test a circuit out, it's assembled on a solderless breadboard. In this case two circuits are built.  The one on the right in the photo is the circuit above, the one on the left is just the wiring for the USB programmer that is used to flash the software into the chip.  The bare circuit board in the front with the cable, and the one on the breadboard that the cable goes to is the USBASP programmer.  The large green block is a programming socket, the chip is moved all the way to the left to program and to the right to test.  The battery and switch are at the far right.

After the circuit is proven to work, I will lay out a circuit board for it. There are several pieces of software that will do this, I currently use KiCAD which is free and open source, and works about as well as the previous software I used, EagleCAD, but without the limitations of EagleCAD.

After completing the design, I sent it off to a place that specializes in making small run prototype boards called OSHPark.  They produced a mock-up of what the board will look like by processing the production files I sent them.  This is the image they sent.

Brightness control
While one way to control the brightness of an LED is to control the amount of current passing through it, that is complex and usually wasteful (the energy that does NOT go to the LED is usually burned as heat). Instead, almost all LED brightness control is done by turning the LED on for only a very short period of time. I will get into describing the LED driver software in the next post.

Friday, February 14, 2014

Name change

It only took me a couple of years but I thought up a name for the blog and the lame hobby junk that I do here.  This page can now be reached via  Same stupid content, but now there's a name for it.

Sunday, February 9, 2014

Programming AVR processors

I'm going to to through the process of getting the simplest possible program working on a simple AVR chip. When done, the chip will blink an LED.  From there we can move on to other stuff.

I'm going to use the Atmel ATTiny85 chip as an example, because I've used it for my last few projects.

Before you start, check out this link for equipment.  You'll want a breadboard and jumper wires, whatever chip you decide to use, and a programmer that is compatible with that chip.

Since we're using a bare chip, you'll need to wire things to it.  The best way is to use a solderless breadboard, as shown in the link above.

You'll also need some way to plug the programming plug into it. I have hand built a little 6 pin breakout board that I just hand wire into my boards, but this is a great alternative:
I found this on ebay. There are different ones for different chips. This is very handy for prototyping and saves 5 minutes of wiring every time you want to breadboard a circuit, and saves having to make a little header breakout board.  If you just use this in-circuit, the ISP header will provide +5V power for your circuit as well so you can throw together quick circuits quite fast.

You'll need a programmer - USBASP is available on ebay for almost nothing (see above link again)

You need a programming environment - WinAVR or AVR Studio (I use the latter- use studio 4.  The newer versions are a ridiculous amount of overkill unless you're developing large systems using advanced chips)  Install WinAVR first, then AVR Studio.  WinAVR contains the compiler, AVR studio gives you a nice environment to use it in.

For the first program, we're just going to cause an LED to blink with an ATTiny85 chip.  Plug it into the board above and plug that into the breadboard, or plug the chip straight in and wire in an ISP socket, whichever you want.

The LED and a 150 ohm resistor will be wired between pins 4 and 5 (ground and output 0) on the ATTiny85 as shown.

Open AVR Studio 4, create a new project called "helloworld" specifying AVR Simulator 2 and ATTiny85 as your chip.  Create "helloworld.c" and put this into it:

#define F_CPU 2000000 // 8 MHz clock

#include <avr/io.h>
#include <util/delay.h>

// set port B bit 0 to output
DDRB = 1;
while (1)
PORTB = 1; // turn LED on
PORTB = 0;

Click on Build/Build.  Hopefully it will compile without errors.

Go out to the DOS prompt and switch to the directory where you saved the project.  There should now be a "helloworld.hex" file which is the compiled version of your little program.  This needs to be sent to the chip using the programmer.  With the programmer plugged into the chip, run this command from the command line:
avrdude -p t85 -c usbasp -U flash:w:helloworld.hex

avrdude should write the code to the chip, and the LED should immediately start flashing.

New printer: JGAurora A3

This week I decided I'd had it with all the other printers in my stable.  The CTC is stable and decent but it just bugs me (can't st...