Programming Peggy 2.0

Some notes on programming the Peggy 2.0. Basic example source code is included; we will be expanding this article soon with additional examples and libraries. Your own contributions will be welcome too, of course!

The Big Idea (or, how it works).

Peggy 2.0 is a multiplexed matrix display of 25x25 LEDs. That means that we turn on one row at a time, and write to that row which of its LEDs should be on. We then turn that row off, turn on selected LEDs in the next row and repeat, scanning through all 25 rows so quickly that there is no apparent flicker, just a continuous image.

This process is managed by the on-board microcontroller, an ATmega168, a type of AVR microcontroller. The row that is in use is picked by the PORTD I/O ports. Bits D0-D3 and D4-D7, respectively, are 4-bit output ports that go to two 74HC154 4-to-16 demultiplexers. Bits D0-D3 go to the first of these chips and the binary number that is sent, ranging from (decimal) 1 to 15 picks which of the rows 0-14 is powered on. A binary output of zero enables none of the rows. Similarly, bits D4-D7 pick which of rows 15-24 is powered on. Again, a binary output of zero on D4-D7 enables none of the rows.

An "enabled" row has power applied to the high side of each of the LEDs in that row. To determine which of the LEDs is turned on, we use two 16-bit LED driver chips (type STP16DP05) that are connected by a daisy-chained SPI output. Output four bytes on the SPI, of which the last 25 bits control the 25 columns of the LED display. To latch the SPI values in place, issue a brief pulse on pin PB1.

That's a bit of a mouthful, and fortunately, you don't have to do this all yourself, it's written for you. We have several sample applications that demonstrate different ways to run the display, in varying degrees of complexity. We will continue to add additional code examples in the near future.


grayscale


Peggy2 Arduino Library
We have written a simple-function high-level Arduino library to control Peggy 2.0. You can download the library, along with examples, and read usage notes here.


Additional Arduino Environment Examples
The following code examples are meant to be run with the Arduino environment and a USB-TTL cable.

(Use the Arduino software environment and tell the Arduino software that you have a Diecimila.)

These are simple, highly functional, and ready to use, but use fairly low-level access to the Arduino (AVR) hardware to accomplish their goals; these are not written in the format of most Arduino programs that use libraries.

peggy2_GrayTest.pde: A simple application that demonstrates running a Peggy 2.0 display with 16 levels of grayscale. (Pictured above, on a display with 10 mm white diffused LEDs.) It's slow software PWM and doesn't leave much room for animation, but isn't it cool? You could probably put your picture on Peggy and hang it on the wall. :)

peggy_life.pde: Conway's life application; a more complex and computationally intensive routine with interactive editor (i.e., drawing program) and display refresh routines built in.


AVR-GCC Examples
Some code examples meant to be run with the AVR-GCC toolchain and an ISP programmer

peggy2.zip: Default firmware; lights LEDs uniformly. Good to test for damaged LEDs, should that become necessary. Presents two unsigned int arrays that can be modified to change what's on the display.

peggy2_Sweep.zip: Simple example showing one row and then one column lit at a time.

Peggy2Life.zip: Conway's life application; a more complex and computationally intensive routine with interactive editor (i.e., drawing program) and display refresh routines built in.

(More coming soon!)

25 comments

The following comments are owned by whomever posted them. This site is not responsible for what they say.
Authored by: bitartist on Wednesday, May 07 2008 @ 08:26 PM PDT Programming Peggy 2.0
It is really Cool, can't wait to see for more cool projects using this v2 board! Nice work!
Authored by: Anonymous on Wednesday, May 14 2008 @ 02:04 AM PDT Programming Peggy 2.0
The above link 'peggy2.zip' is broken. I get the following error message:

<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>50222503DE021DA0</RequestId>
&#8722;
<HostId>
jqEawTJeNnzWfEk7LjDHqB/nyO+/stLFaPG1IKU9uWchTmm5ujuh1RQugvjHIdNn
</HostId>
</Error>
Authored by: Windell on Wednesday, May 14 2008 @ 02:32 AM PDT Programming Peggy 2.0
Oops-- thanks for catching that. Should be okay now. :)

---
Windell H. Oskay
drwho(at)evilmadscientist.com
http://www.evilmadscientist.com/
Authored by: Anonymous on Thursday, June 12 2008 @ 02:35 PM PDT Programming Peggy 2.0
Hi Wendell, can you explain to me how i can your Game of Life to my P2.0? I cant download the peggy_life.pde file.
This was one of the reasons i bought P2.0, its first GoL then my own programs, a i do have some ideas.
regards
devries
netherlands
080612
Authored by: Windell on Thursday, June 12 2008 @ 03:04 PM PDT Programming Peggy 2.0
Sorry, the permissions seem to have been scrambled again; I think that it's working now. (I can download the file.)

---
Windell H. Oskay
drwho(at)evilmadscientist.com
http://www.evilmadscientist.com/
Authored by: Anonymous on Tuesday, June 17 2008 @ 07:34 AM PDT Programming Peggy 2.0
Hi Windell,
prehaps im doing things wrong?
First i open web page
"http://www.evilmadscientist.com/article.php/programpeggy2";

then i select "peggy_life.pde"
then with my right mouse button i select : 'save target as"
it will result in saving a html page.

If i "open" the pde file in a new page
i get a lot of codes, all behind each other beginning with the text: "* Title: peggy_life Date Created: 3/23/08 Last Modified: 4/28/08 Target: Atmel ATmega168 Environment: Arduino - 0011 Purpose: Drive 25x25 LED array Application: Conway's Life Copyright 2008 Windell H."

Is this right? And do i have to copy all codes to the arduino software program????

Please your advice, as i cant catch any sleep...;-)
regards
devries
081017

Authored by: Windell on Tuesday, June 17 2008 @ 08:14 AM PDT Programming Peggy 2.0
Do not save it as an HTML file, just save the file directly as it is-- use the "download" option or "save linked file as..." option if you have one.

Once you've saved the file, open the Arduino application and from within that program, open up the peggy_life.pde file.
Don't copy anything to anything.

---
Windell H. Oskay
drwho(at)evilmadscientist.com
http://www.evilmadscientist.com/
Authored by: Anonymous on Saturday, July 19 2008 @ 05:13 AM PDT Programming Peggy 2.0
I think, I know what you can do. When the codes show up, copy them and paste them in notepad. The go and save the file with a .pde extension on it. Example: game_peggy.pde
Authored by: Anonymous on Tuesday, February 03 2009 @ 04:46 AM PST Programming Peggy 2.0
How much time is spent by the arduino just updating the led display?
ie How fast can a program be run in the "background" without disturbing the led matrix?
Authored by: Windell on Tuesday, February 03 2009 @ 10:24 AM PST Programming Peggy 2.0
>How much time is spent by the arduino just updating the led display?

The way that the standard Peggy 2 library is designed leaves this up to you. The screen is only redrawn when you call the command to redraw the screen. That means that you can pause for a moment-- leaving the screen off, and devote 100% of your processing time to your application. An example of a very processor intensive routine is our 25x25 game of life that wraps around; when we run at high evolution rates, it starts to get dimmer because we're spending most of our time computing the next iteration.

There are also alternative versions of the library that use refresh-based interrupt, where a constant amount of the CPU time is devoted to screen redraw; that may be more convenient for some applications.

---
Windell H. Oskay
drwho(at)evilmadscientist.com
http://www.evilmadscientist.com/
Authored by: Anonymous on Thursday, May 07 2009 @ 05:50 AM PDT Programming Peggy 2.0
I'm new to microcontroller programming (but to other kinds of programming), very interested in trying the Peggy 2, but a little confused about the following things:

1. I'd like to update the display, from my computer, very fast. How fast? Well, at least 1000 updates/sec. [Quick explanation: I'm doing experiments on vision, and I need this much speed.] How fast can I update all the 625 gray levels on the display, if I'm using AVR-GCC.

2. Do I just need a USB cable, or something else? Your page mentions an "ISP programmer". What's that?

Thanks a lot,
Mark
Authored by: Anonymous on Thursday, May 07 2009 @ 06:39 AM PDT Programming Peggy 2.0
Sorry, I just realized how stupid I was being in my previous posting. I now realize that there's a microcontroller actually *controlling* the display (duh), and therefore that it's not directly controlled by a computer. OK, fine, so let me re-phrase my questions:

1. So let's say I have a program loaded on the Peggy 2 microcontroller, either using Arduino or AVR-GCC. How fast can this resident program update the display, either a single LED or the whole 25x25 thing?

2. Is there a simple and fast way to trigger such a program (i.e., tell it to start running) from a computer, for example by sending a signal on a USB port?

Thanks again, and sorry for the learning curve,
Mark
Authored by: Windell on Thursday, May 07 2009 @ 11:45 AM PDT Programming Peggy 2.0
The absolute upper limit of how fast the Peggy screen can be refreshed is approximately 4 kHz, although it would take concerted effort to achieve this in practice. Data is clocked out from the microcontroller to the display chips (for a given row) at a rate of 4 MHz. There are 32 bits per row (25 columns, plus a few extra bits to round out even bytes), so the whole row is read out in about 8 microseconds. If we repeat this for all 25 rows, that takes a total of 200 microseconds, and there's a little bit of additional overhead to "latch" the rows, so the whole screen scans in about 250 microseconds, hence the 4 kHz figure. Monochrome data can go to the display up to 4000 times per second. BUT, it will be difficult to get interesting data ready at that rate, since the processor will be *very* busy with displaying the screen.

Most often, we drive the screen at around 1.5 kHz-- Displaying the screen 15 times to represent 16 levels of gray scale, giving a final 16-shade image that can change 100 times per second. This also leaves enough time between display computations to perform computations about what the next frame should be. The input source for data can be internal-- using its own computations, from sensors like buttons or the internal analog-to-digital converter channels, or from data streamed over a computer link with I2C.

Now, I suspect that your display requirements are not as strict as you are saying... because if they are, then you should already know that a solution to do what you need is going to cost multiple thousands of dollars. If you really need to display 625 gray levels, you'll need 625 passes to represent that, and if you really need to change *that* 1000 times per second, that means that you need to completely refresh the whole screen at a rate of 625*1000=625 kHz. That means that you'd have 1.6 microseconds to display the entire screen. Using a 16 MHz AVR microcontroller like we do, you would not be able to achieve this display rate-- 1.6 microseconds isn't even enough time to clock out a single *byte* to the screen. If you went to a 20 MHz processor, you could clock out a single byte in 1.6 microseconds, so if you dedicated a microcontroller AND an 8-bit LED driver chip to for every eight LEDs, you could update each one of them every 1.6 microseconds in principle... but that wouldn't leave time for necessary overhead of loading data onto the processors. A better approach would be to use (more expensive) LED driver chips that have internal 10-bit PWM, but you'd still be looking at a solution with many (maybe 80) LED driver chips and microcontrollers. Or, you would need to go to a larger faster microcontroller-- maybe ARM based or an FPGA. None of these solutions would be quick, easy, or anywhere near as cost effective as the Peggy design.

---
Windell H. Oskay
drwho(at)evilmadscientist.com
http://www.evilmadscientist.com/
Authored by: Anonymous on Sunday, May 17 2009 @ 10:06 AM PDT Programming Peggy 2.0
I have been looking at the schematics, and I'm trying to figure out how the grey-scale brightness levels are achieved. At first glance, the STP16DP05 chips seem to operate like basic shift registers with binary output. So there must be some kind of funky LED switching going on. Can anyone explain how the brightness of each LED is controlled? Am I correct to assume the frame buffer contains 4-bit brightness values for each pixel, which is converted to some kind of a LED duty cycle in software?
Authored by: Windell on Sunday, May 17 2009 @ 11:39 AM PDT Programming Peggy 2.0
Yes, that's correct.

See also: http://code.google.com/p/peggy/wiki/Pegg2Wiki16Levels

---
Windell H. Oskay
drwho(at)evilmadscientist.com
http://www.evilmadscientist.com/

Authored by: Anonymous on Tuesday, May 26 2009 @ 05:56 PM PDT Gang Programming Peggy 2.0
I'm wondering about an application I have in mind. If I were to mount six Peggy 2.0's in a 2x3 array, is there a way to aggregate them into a 50x75 matrix and program it collectively, or would each Peggy 2.0 be it's own "island?"
Authored by: Windell on Tuesday, May 26 2009 @ 07:10 PM PDT Gang Programming Peggy 2.0
Each has its own processor, so they are generally independent. You could stream data to all six at the same time, or if running off the internal memory only, send a sync signal between them to keep them in lock step.

---
Windell H. Oskay
drwho(at)evilmadscientist.com
http://www.evilmadscientist.com/
Authored by: Anonymous on Thursday, June 18 2009 @ 11:50 AM PDT Gang Programming Peggy 2.0
So is it possible to replace the microcontrollers in an array of Peggy 2's with an external controller/computer? Or, can you interface a computer with those microcontrollers in an array so that the array acts like one huge board? For example, using the Peggy 2 RGB to display ripples, such as you would find on the surface of water when it rains. Do I need an engineering degree to be able to do this? :)
Authored by: Windell on Thursday, June 18 2009 @ 11:54 AM PDT Gang Programming Peggy 2.0
Peggy 2 has an available "I2C" AKA "TWI" interface. This is a multipoint serial interface, over which you can send data to multiple Peggy 2 boards. You can either send full-frame data or just commands that rely on the on-board microcontroller to draw different things on the screen.

Not much electrical engineering knowledge would be needed-- the hookup is easy-- but programming experience would be helpful.

---
Windell H. Oskay
drwho(at)evilmadscientist.com
http://www.evilmadscientist.com/
Authored by: Anonymous on Sunday, June 07 2009 @ 11:07 AM PDT Programming Peggy 2.0
Is the code available for the radiating rainbow animation from a moving point seen on youtube? If so, where can I find the link. Thanks.
Authored by: Windell on Sunday, June 07 2009 @ 11:35 AM PDT Programming Peggy 2.0
Yes, that project is here, complete with source code. :)

---
Windell H. Oskay
drwho(at)evilmadscientist.com
http://www.evilmadscientist.com/

Authored by: Anonymous on Friday, November 13 2009 @ 09:29 AM PST Programming Peggy 2.0
CAN I DOWNLOAD ARDUINO STUFF ON WINDOWS VISTA?
Authored by: Windell on Friday, November 13 2009 @ 10:02 AM PST Programming Peggy 2.0
Yes.

---
Windell H. Oskay
drwho(at)evilmadscientist.com
http://www.evilmadscientist.com/
Authored by: Anonymous on Sunday, November 15 2009 @ 06:23 AM PST Programming Peggy 2.0
Hi
Im new to this but have a particular project in mind and want to know what is needed before purchasing one of your units.
At the top of this (http://evilmadscience.com/tinykitlist/75-peggy2) EMS page there is a picture of the peggy displaying an image of a person.
Is this source from video?
I want to know what I need in order to get a peggy to display video like in that picture.
My basic idea is having a camera signal live feeding into the peggy.
Is this possible?
Authored by: Windell on Sunday, November 15 2009 @ 11:08 AM PST Programming Peggy 2.0
See this article for more information about that.

---
Windell H. Oskay
drwho(at)evilmadscientist.com
http://www.evilmadscientist.com/

Welcome to Evil Mad Scientist Laboratories. New projects are posted every Wednesday.


Bookmark EMSL

EMSL RSS Feed
Twitter: EMSL
del.icio.us
feedburner
Feed on Google Reader
YouTube Channel
Add technorati Fave

technorati search

Subscribe to get new articles by E-mail:

E-mail address:


Preview | Powered by FeedBlitz

My Account





Sign up as a New User
Lost your password?

Who's Online

Guest Users: 11

Forumposts

Order: New Views Posts
Latest 5 Forum Posts
 
Re: Ferritc core sub..
 By:  npkeith
 On:  Friday, November 20 2009 @ 10:34 AM PST
 Views 0 Replies 0
Re: alphanumeric led..
 By:  karlgg
 On:  Thursday, November 19 2009 @ 10:14 PM PST
 Views 0 Replies 0
Re: alphanumeric led..
 By:  Leviathan34
 On:  Thursday, November 19 2009 @ 07:21 PM PST
 Views 0 Replies 0
Re: 0 of 9 LEDs work..
 By:  Windell
 On:  Thursday, November 19 2009 @ 12:52 PM PST
 Views 0 Replies 0
Re: 0 of 9 LEDs work..
 By:  michael pechner
 On:  Thursday, November 19 2009 @ 11:40 AM PST
 Views 0 Replies 0

DIY Hardware for Electronic Art


Interactive LED Panels


Meggy Jr RGB
LED matrix game
development kit.


Business-card sized
AVR target boards


Peggy 2
LED Pegboard kits