Sending output from Mathematica to the Adafruit RGB LCD display on a Raspberry Pi

I had a birthday and bought the Adafruit RGB LCD kit for the Raspberry Pi.  Then I wrote some Wolfram code to send out put to the display.  Then I wrote a tutorial about it.  Finally, someone read that tutorial.



Part I - Prerequisites and MathLink code

The folks over at Adafruit have done a nice job with their tutorial on assembling the 16x2 LCD + Keypad kit for the Raspberry Pi.  I bought the kit along with a stacking header so I can connect other peripherals to my RPi. Assembly was a breeze and I initially used Adafruit's Python code to ensure that the display was functioning properly.  I'm interested in getting Mathematica to direct output to the display, so Python isn't going to cut it in the long run.  Instead, I am using Gordon Henderson's excellent WiringPi library and if you intend to use the code I describe in this project you'll install WiringPi beforehand.

Speaking of code, I just started learning about git. Code to compile the MathLink program can be found at  Bear with me as this is my first git project.  To get the code on your RPi, use the following:


git clone git://
cd rpi-lcdlink

If all goes well, you'll have an executable file, lcdlink, that you'll be able to install in Mathematica.  To try it out, start wolfram and enter the following commands

<<"!gpio load i2c"
link = Install["lcdlink"]

 I'm assuming that you didn't change the directory after compiling lcdlink.  If you did, simply set the directory with 


 or wherever you actually placed the executable file.  If all went well, your LCD screen should look like this:


Now that we know that Mathematica can output to the LCD, let's see how we can do it.  I've organized the code into a package called lcdlink and we can get a list of the functions provided by this package using:


You can get some basic usage information for each of these functions by typing a "?" prior to the function name, for example:

(* lcdColor[color] changes the backlight of the LCD.  Color is an integer from 0 to 7. *)

If this command is run from a notebook interface, the result is a set of clickable functions that provide the definition. In general, to write a message to the screen we will (a) clear the screen, (b) position the cursor if necessary and (c) write the string.  We can also set the color if we'd like.  For example:

lcdPuts["Hello, world"];


What may cause a bit of confusion is that the cursor positions start with 0 instead of 1, and the column is passed before the row.  The cursor is now positioned immediately after the "d", so if we write another string, it will start from that position.

Note that all of the library functions will return 0, which I suppress with the semicolon.  I intend to use the return value for passing error information in a future version of the package.  Naturally, since the code is provided to you, you are free to modify the functions to return void.

lcdPuts["A much longer string:];


Notice that text will automatically wrap around the display, so all we need to worry about is where the cursor is and the length of the string being written to the display.

It is possible to scroll a message using the package function lcdScroll, which accepts two arguments: the first is the row number (0 or 1) where the message will scroll and the 2nd is the string itself.

lcdScroll[0,"Writing a scrolling message to the RPi LCD with Mathematica."];


Mathematica will continue to evaluate the function above until you press the SELECT button on the LCD display.  The lcdScroll could benefit from some improvements such as adjusting the scroll speed to address the shadowing effect.  I wanted to put it there mainly to demonstrate that we can scroll a message.  (And Gordon did a nice job of providing the sample code in the WiringPi library that I feld obliged to use it.)

In Interfacing with an Interface part 2, I'll go through some ways to take advantage of Mathematica's dynamic capabilities to get more enhanced visuals on the LCD.