Dr Blinken

blog, bentosheets and railspages.

Programming Arduino Workshop @ Informatica Feminale 2013


Table of Contents

Time     Content
Mi 11.00-12.30 1.5 Getting Started: Installation and first blinking LED
Do 09.00-12.30 3 12-LED Shields and Dice Library
Do 14.00-15.30 1.5 Dice Library Extensions
Fr 09.00-12.30 3 TBD
Fr 14.00-15.30 1.5 TBD

Getting Started: Installation and first blinking LED

Please prepare this before coming to Informatica Feminale! We will use the first (short!) session on Wednesday to clarify questions with it, connect the Arduino and let the LED blink.

Arduino is a hardware prototyping platform based on Atmel Atmega Microprocessors, in case of the Arduino Uno which we will be using in the workshop the Atmel Atmega 328. Probably the best thing about Arduino is the vast amount of documentation and project examples you can find on the net, which is why I will not try to replicate any of that here and dive right into a short step-by-step guide to getting started programming it.


one led on arduino

Introduction: 12-LED-Shields

I’ve prepared simple leds shields to get you started with a bit of Arduino-Programming without the need to put together or solder own prototypes. The shields let you try out some first microcontroller programming with physical interaction with the real world: reading a button state and switching leds on and off! (hey, it’s not a computer display!)

12 LED Shield

The shields have 12 LEDs directly connected to 12 (output) pins, and one or two buttons connected to two other pins, as shown in the diagram below (for just four LEDs and 1 Button).

The Buttons feature a pull down resistor making sure that you read a proper “LOW” on the input pins while the button is not pressed. You can explore the button starting with the 02.Digital->Button Example.

12 LED Shield

As most of the shields have been soldered a bit differently, I’ve provided a header file containing variables with the pin information: pins.h which you can download and use in our own sketches. To use them, you have to declare your board before including pins.h like this:

  #define BOARDn

where n is the number of your board, e.g. for B4 as in the picture above you would change the line to

  #define BOARD4

LEDs: use the int array int pins[] which lists the pins from the lower right corner to the upper left like this:

 11 10 09 08
 07 06 05 04
 03 02 01 00

e.g. to switch on the two middle leds, use:


(Note that I rather use ON and OFF than LOW and HIGH - one of the shields is connected the other way around, so this is abstracted as well ;-)

If you use pins.h, you can read the button state like this:

buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);

Have a look at the BoardTest Sketch in the arduino workshop example repository on GitHub

Note: some of the shields still have leds connected to pins 0 and 1 which are also used for serial communication. If you have trouble uploading sketches while animations are running, make sure to disconnect them (just don’t push those two leads in when clipping the shield on) - they should be connected to two other pins as well.

First Day: Dice Library Programming Exercise

Write a Sketch that implements a simple Die: The Goal is to implement a simple six-sided Die that can be rolled by pressing a button.

To implement it, you will need the random() function provided by the Arduino library (Reference). It’s also a good idea to write a function showing different numbers on the die, e.g.

showNumber(int number)

You can use the Sketch DiceLibrary as a starting point. It features a header and a class file for the DiceLibrary class where you just have to fill out the methods:

void roll(); // rolls the Die and displays result
void showNumber(int number) // shows a number between 1 and 6

Ideas for Extensions

Second Day: Arduino BlinkenFilm or own Project

This is a suggestion for a project for the second day of our workshop. It can be done with the 12 LED-Shields, and features somewhat more complex programming.

This is only a suggestion, though - if you’d rather work on your own project, go ahead!

Check out project blinkenlights - http://blinkenlights.net/project - at least watch the video http://blinkenlights.net/project/videos to get an idea about what the project blinkenlights was and what you could have done with a BlinkenLightsMovie back in 2001.

This exercise is about playing blinkenlights movies in BLM format on the arduino and the 12led shield by sending a movie file to the Arduino which it then stores and plays in a loop. Reading a file in and switching the according LEDs on and off doesn’t seem to be a big issue. But there are two mayor obstacles: First, the Arduino doesn’t have a file system. Second, it has very restricted memory available, so we want to store the file in a very compressed format.


Playing BLM files

For playing BLM files on the Arduino, we will first compress them and send the compressed version to the Arduino, to make the processing on the Arduino fast enough to not loose any sent data. This is a lot of work, so you might want to split up and have one person working on the Arduino part, and the other on the file compression. Check out the section about “BLM Compression” below.

You can use the This Sketch as a starting point for your implementation.

Part1: Show a frame.

Create a method showFrame(int frame){} in an Arduino Sketch  that interprets the passed integer as a movie frame in the described compression. Also provide helper methods to normalize and denormalize the delay in bits 16-13 (these are also provided with the scaffold). Test this method by implementing a binary count up to 4096 using the loop() function rather than a for loop to be able to check for incoming films later at every count step (that is, implement doCount() in the scaffold).

Part2: Receive and store a received Blinkenfilm.

Receive a compressed film and store it in an byte/integer array or in EEPROM. Use Serial.read to receive films send via the Serial interface by receiving single bytes and storing them somewhere - every first byte represents the upper byte in the integer frame, every second the lower byte. Test this by sending bytes manually using the Serial Console in the Arduino software. The Arduino has only 1k memory for variables, so you may want to use the EEPROM to store the film. This enables you to make the Serial Buffer large enough to handle even long films by editing HardwareSerial.cpp:


you’ll find the file among the libraries installed with the arduino software, e.g. on the Mac here:

/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/HardwareSerial.cpp On Linux and the Mac, you can send Bytes and Files to the arduino by using this:

echo 'BARBARA' > /dev/tty.usbmodemfd121


cat film.blm > /dev/tty.usbmodemfd121

On Windows, you can just copy & paste the compressed bytestring from the file into the serial monitor. Just make sure no formatting information is added - use a plain text editor and select “no line ending” in the monitor.

Extra Part

C++ (or other language) Command Line Utility: BLM Compression: write a simple C++ command line utility that converts regular BLM files into the compressed format by simply chaining the bytes together. Use the state machine you’ve designed in the prelab and implement it with a simple switch statement. This is a brief program that could be done in C essentially - that is, don’t bother about creating classes, just write one or two subroutines called by main that parse the file.

BLM Compression

Here’s a simple Idea on how the BLM could be compressed for the Arduino: If you have a frame with a duration and three rows with 4 leds:


These can be compressed into two bytes (or one integer) by simply converting the rows into nibbles (4 bits) using the 12 rightmost bits:


Now, compressing the duration is a bit harder, and can’t really be done with the remaining nibble, as it can only represent 16 different states. One way is to restrict the films to 50 ms duration steps up to 800 ms, which is actually quite sufficient. 100/50 = 2 = 0b0010, such that you get this altogether:


You might, however, choose to compress BLM differently - this is just a suggestion!

AVR-Programming with Eclipse

If you want “the real thing”, you can also set up Eclipse for AVR-Programming, you find a manual here. You basically need the CDT (C development tools) for eclipse, the AVR Kit, the Arduino libraries and a lot of configuration, but it looks more complicated as it actually is. In any case, I recommend using the Arduino Software for browsing through the examples, and you’ll need it for the libraries, so make sure to have that, too.