Categories
Projects

Arduino Mandelbrot Set Viewer

Arduino Mandelbrot Set Viewer

I got a few 128×64 graphics LCDs for Christmas. One of the first things I did with them was make a Mandelbrot Set viewer–with zoom!

Video

The video is available in HD, and viewable in a larger size if you click through. The music is “Mandelbrot Set” by Jonathan Coulton.

Description

The Mandelbrot Set is a fractal. You can zoom on the border and get pretty pictures. There are a bunch of cool facts about the Mandelbrot Set at Wikipedia, and Jonathan Coulton has a sweet song about it.

Download

Download the Arduino sketch for the Mandelbrot Set viewer.

This requires a GLCDks0108 library available on the Arduino Playground. Currently, to run, this library needs a simple modification! In ks0108.h, move the line

void WriteData(uint8_t data); // experts can make this public but the functionality is not documented

to the public: section. This function allows you to write a byte to the display, and advances the cursor. After a cursory browsing of the datasheet, it appears the fastest way to write data to the screen. I’m in the process of getting this change made in the library so this modification won’t be necessary.

Code

The code for this isn’t too difficult. The actual drawing of the Mandelbrot set is pretty much exactly as described in this nice Mandelbrot Set tutorial. It goes over the theory and the code for drawing the fractal. The only change I made in drawing the fractal was that I calculate 8 points, and then draw them to the screen as a group. This matches how the screen wants to receive data, and wastes less time interfacing with the LCD.

The zooming functionality is taken care of by a simple state machine in the loop() loop.

If you want to run this on a different microcontroller, the C should be fairly portable.

Schematics

Mandelbrot Set Viewer Schematic

Parts

In terms of hardware requirements, I used a ks0108 graphics LCD. These are available from a variety of vendors–but be careful and check your pinouts! The specific one I used was this 128×64 graphics LCD from Sparkfun, but there are a variety of displays listed in the Arduino Playground site for the graphics library I used.

Besides the LCD, I used a 220 ohm resistor (R1) and switch (SW1) to toggle the backlight, a 10k potentiometer (VR1) for setting the display contrast, two buttons (SW2, SW3) for back and forward, and a 10k potentiometer (VR2) for the cursor.

Future Work

I would like to take a look at fixing some of the rounding issues. There are some places where the drawing “looks funny” after enough zooms. I’m pretty sure it’s due to rounding issues with the limited floating point precision.

I would also like to improve the “back button” functionality so that you can go back in the middle of the zooming process if you make a mistake.

The controls could likely be made more intuitive by using something with a joystick. The software interface matches the input paradigm of a knob and some buttons, but if there was a cursor, and then moving the cursor made a box, the zoom would likely be more intuitive. This could be implemented with the Wii nunchuck.

Photo Gallery

More photos are available at my Mandelbrot Set photo set.

Questions, comments?

Feel free to contact me.

References