Arduino Mandelbrot Set ViewerPosted: January 15, 2010
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!
The video is available in HD, and viewable in a larger size if you click through. The music is “Mandelbrot Set” by Jonathan Coulton.
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 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.
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.
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.
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.
More photos are available at my Mandelbrot Set photo set.
Feel free to contact me.
- Mandelbrot set – Wikipedia, the free encyclopedia
- Mandelbrot set tutorial
- Arduino playground – GLCDks0108