#Atmega_eyes animated eyes for pumpkin

tl;dr

#atmega_eyes is an animated eyes for a pumpkin for Halloween. You can probably tell from the name it’s powered by a atmega328, The eyes are two 8×8 led matrix boards that are controlled by max7219 logic plus I’ve thrown in a RGB LED and a fake candle flicker LED which are controlled by a push button.

Its September lets begin

The last 2 years I’ve made Halloween projects first one in 2013 and another one in 2014. I normally start thinking about making a Halloween project at the start of September so it gives me 60 days or so to get the design done. This year however I forgot about the Halloween project and didn’t remember until the 11th of September so this only give me 50 days to get it all done. Plus I was going to be moving away from the arduino library’s and just coding in pure C so I really had my work cut out.

I wanted to build on my previous years projects but didn’t want to just re-hash the same ideas. I’ve always wanted to use these 8×8 led matrix board you find on eBay the one’s with a 7xxx serice logic chip running the show. After a quick look online for some example code I found exactly what I was looking for in this forum thread.

The hardware

Hardware wise this was going to be a relatively straight forward build. I already had all the parts I needed apart from the 2 LED matrix’s. Here is my BOM

  • Atmega328 + 16Mhz crystal and 22uf capacitors
  • RGB LED
  • Fake candle flicker LED
  • Four 220 Ω resistors
  • Push button
  • 2 stand off’s
  • 6 pin IDC header
  • Female headers
  • 240mAh lipo battery
  • lipo battery charger
  • 5v DC-DC booster
  • Heat shrink tubing

Like all my projects it all starts on a bread board to flesh out the design

Jpeg

Once im happy with the design I transfer it over to a cross-platform app called Fritzing to make it easier for when I layout my real schematic

atmega_eye_bb

Once I had layout finished my layout I ordered the PCB from oshpark.com, they are really affordable and relatively quick at delivering you your boards. They will send you 3 copy’s of your board so I decided to use one of the spare boards along with some stand-off’s as a sandwich to sandwich the battery in place

Battery power

In previous projects I had been using the lithium 18650 battery’s with a striped down emergency phone changer to let me charge and power my projects. For this project I wanted to try something new and have something smaller too. I wasn’t sure if my idea for a battery set up would work so I only ordered a small battery to test it. I ordered a small 3.7v 240mAh battery plus a lipo charger. because the lipo battery was only 3.7v I needed 5v to run the atmega328 so I also had to add a DC-DC booster to boost it up to 5v.

I wired the battery to the charger and also wired the battery to the DC-DC booster then from the boost output to the VCC on the atmega328. This only gave me about 2 hours run time but that should be all I need for Halloween night

One the left is the Lipo battery, In the middle is the battery charging board followed on the right by the DC-DC booster.

As a quick hack I just used some heat shrink tubing to hold everything together, was quite a challenge to get everything to stay together while applying heat.

The code

As I said earlier I found the prefect example code on this forum thread, This gave me a good base to work off and start playing around with what I wanted to do. I wanted to add a RGB led so I could light up my pumpkin in any colour I wanted, but I found this a real challenge to find enough PWM pins and use the right timers with them that wouldn’t get in the way of the example code I had found for the LED matrix.

Here is the example code I found so you can easily copy the bits you need or test your hardware out.

/*
One MAX7219 connected to an 8x8 LED matrix.
 */

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

#define CLK_HIGH()  PORTB |= (1<<PB2)
#define CLK_LOW()   PORTB &= ~(1<<PB2)
#define CS_HIGH()   PORTB |= (1<<PB1)
#define CS_LOW()    PORTB &= ~(1<<PB1)
#define DATA_HIGH() PORTB |= (1<<PB0)
#define DATA_LOW()  PORTB &= ~(1<<PB0)
#define INIT_PORT() DDRB |= (1<<PB0) | (1<<PB1) | (1<<PB2)

const __flash uint8_t smile[8] = {
        0b00000000,
        0b01100110,
        0b01100110,
        0b00011000,
        0b00011000,
        0b10000001,
        0b01000010,
        0b00111100};

const __flash uint8_t sad[8] = {
        0b00000000,
        0b01100110,
        0b01100110,
        0b00011000,
        0b00011000,
        0b00000000,
        0b00111100,
        0b01000010,
};

void spi_send(uint8_t data)
{
    uint8_t i;

    for (i = 0; i < 8; i++, data <<= 1)
    {
	CLK_LOW();
	if (data & 0x80)
	    DATA_HIGH();
	else
	    DATA_LOW();
	CLK_HIGH();
    }

}

void max7219_writec(uint8_t high_byte, uint8_t low_byte)
{
    CS_LOW();
    spi_send(high_byte);
    spi_send(low_byte);
    CS_HIGH();
}

void max7219_clear(void)
{
    uint8_t i;
    for (i = 0; i < 8; i++)
    {
	max7219_writec(i+1, 0);
    }
}

void max7219_init(void)
{
    INIT_PORT();
    // Decode mode: none
    max7219_writec(0x09, 0);
    // Intensity: 3 (0-15)
    max7219_writec(0x0A, 1);
    // Scan limit: All "digits" (rows) on
    max7219_writec(0x0B, 7);
    // Shutdown register: Display on
    max7219_writec(0x0C, 1);
    // Display test: off
    max7219_writec(0x0F, 0);
    max7219_clear();
}

uint8_t display[8];

void update_display(void)
{
    uint8_t i;

    for (i = 0; i < 8; i++)
    {
	max7219_writec(i+1, display[i]);
    }
}

void image(const __flash uint8_t im[8])
{
    uint8_t i;

    for (i = 0; i < 8; i++) display[i] = im[i]; } void set_pixel(uint8_t r, uint8_t c, uint8_t value) { switch (value) { case 0: // Clear bit display[r] &= (uint8_t) ~(0x80 >> c);
	break;
    case 1: // Set bit
	display[r] |= (0x80 >> c);
	break;
    default: // XOR bit
	display[r] ^= (0x80 >> c);
	break;
    }
}

int main(void)
{
    uint8_t i;

    max7219_init();

    while(1)
    {
	image(sad);
	update_display();
	_delay_ms(500);
	image(smile);
	update_display();
	_delay_ms(500);

	// Invert display
	for (i = 0 ; i < 8*8; i++)
	{
	    set_pixel(i / 8, i % 8, 2);
	    update_display();
	    _delay_ms(10);
	}
	_delay_ms(500);
    }
}

I wrote a quick bit of test code so I could test out my animations before trying them in my real code (Github link) just uncommnet the different animations Ive given as examples or try your own designes out. My final code can be found on my Github page Here

The video

Ive made two videos one is a hardware tour and the second one is with it installed in a pumpkin

First video, Hardware tour –

Second video, Inside pumpkin – 

Breaking free of Arduino

I got my first arduino for Christmas in 2012, At the time I had been learning to code python a the start of that year so I had an idea of the principals of coding. You can’t beat the feeling of getting your first LED to be blink (hardware hello world). Going from python to arduino C++ was a steep learning curve but I soon go the hang of it.

As I became a better coder I wanted to give VIM a try because pointing and clicking in gedit was becoming annoying, This lead me to wanting to use the command line to also upload my arduino code so I wouldn’t have to keep jumping back a forth to the IDE (which is terrible). I found out you can use arduion-mk to compile and upload your arduino sketches from the command line.

After spending a few years making projects and getting more proficient with the arduion environment I wanted to spread my wings and break free of the arduino library’s that had been holding me back and clogging up vital code space on the atmega328. After reading this article on hackaday I decided to delve into the AVR world without the helping hand of arduino. It was a massive learning curve but after learning the basics I was back on track tinkering with the atmega328

Advertisements

3 responses to “#Atmega_eyes animated eyes for pumpkin

  1. Pingback: #Atmega_tree | Facelesstech·

  2. Pingback: 7 Ideas for Automating Your Halloween Porch Display | Make:·

  3. Pingback: Electro_pet | Facelesstech·

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s