Mathematica and the Wolfram language on Raspberry Pi

Traffic Light

To further explore the capabilities of Mathematica and the Wolfram language, I'll describe how to create a more complex circuit with more LEDs to simulate a traffic light. Start with the following parts:

  • 1x red LED (Digi-Key part 67-1120-ND)
  • 1x green LED (Digi-Key part 67-1119-ND)
  • 1x yellow LED (Digi-Key part 1080-1094-ND)
  • 3x 330-ohm resistors (Digi-Key part A105936CT-ND)
  • 1x breadboard (Adafruit part 239)
  • 5x male-to-female jumper cable (SparkFun part PRT-09140)

Set up the circuit as shown in Figure 11. Once again, remember to turn the Raspberry Pi off before you begin.

Figure 11: Simulating a traffic light.

Turn the Raspberry Pi on and start Mathematica or the Wolfram command-line interface. You then need to define which pins you are using to automate the LED on/off process:

ledpins = {14, 15, 18}

Then, you can turn all the LEDs on in sequence as follows:

       DeviceWrite["GPIO", ledpins[[i]]->1];
       , {i, 3}]

This code turns on the green LED first, then the yellow, and then the red, with a gap of 0.5 seconds between each. You can then turn them off again using the same command by just replacing the 1 with a 0:

       DeviceWrite["GPIO", ledpins[[i]]->0];
       , {i, 3}]

To simulate a real traffic light system (at least the way they work in the UK – the sequence will be different depending on where you live), you will want the red LED to come on first, then the yellow LED should come on briefly while the red is still on, and then the yellow and red should go out and the green should come on; then, the yellow should show briefly, followed by the red. This sequence should then repeat.

The code to achieve this pattern is shown in Listing 4. After entering the code, press Shift+Enter to execute. The program will execute the traffic light simulation and repeat it three times.

Listing 4

Simulating a Traffic Light

01 Do[
02    DeviceWrite["GPIO", ledpins[[1]]->1];
03    Pause[10];
04    DeviceWrite["GPIO", ledpins[[2]]->1];
05    Pause[2];
06    DeviceWrite["GPIO", ledpins[[1]]->0];
07    DeviceWrite["GPIO", ledpins[[2]]->0];
08    DeviceWrite["GPIO", ledpins[[3]]->1];
09    Pause [10];
10    DeviceWrite["GPIO", ledpins[[2]]->1];
11    DeviceWrite["GPIO", ledpins[[3]]->0];
12    Pause [2];
13    DeviceWrite["GPIO", ledpins[[2]]->0];
14    , {3}]

The Wolfram language, and hence Mathematica, is capable of interfacing with many devices over the GPIO pins, and this is just a basic example. If you have enjoyed this intro to GPIO interfacing in Wolfram and would like to try some more complex projects, I highly recommend searching the Wolfram community Raspberry Pi forum [5], which includes a large number of project examples posted by friendly community members – including controlling stepper motors and importing data from GPS modules and accelerometers.

The Raspberry Pi Camera

Mathematica also offers some interesting options for accessing and programming the Raspberry Pi camera module. (The commands described in this section will also work at the Wolfram command-line interface, but because the command line doesn't offer a graphical interface, you won't see the graphical output from the camera, making the experience a bit lackluster.) If you want to see pictures, use Mathematica.

Before you start, you need to attach your camera and set it up as described in a previous article in Raspberry Pi Geek [6]. You'll also find a description of how to use the camera at the Raspberry Pi Foundaion website [7]. Once you have set up the camera, you are ready to start using your camera module with Wolfram.

I'll describe two methods for using the Raspberry Pi camera in the Wolfram language. The first option is very simple; however, it only provides very basic functionality and does not allow for any of the parameters that can be used with the command-line camera function raspistill. To use this first method, the only code you have to type is


and hit Shift+Enter: Mathematica takes a picture with the camera module. A red light should appear on the camera module when this is happening, so you know it is in the process of working. You can also do things like assign the picture to a variable for use later in your program:

image = DeviceRead["RaspiCam"]

This DeviceRead function is a really short and nice bit of code; however, as mentioned before, it unfortunately does not allow you to make use of the plethora of options for image sizing, effects, and parameters normally available to you in the raspistill command-line function. To use these additional functionalities of the camera module, try the code:

image = Import["!raspistill -n -t 1 -o -", "JPG"]

This code creates a variable called image and then assigns a picture to it, which is then imported from the camera module. The -n option means that no preview window is shown for output; the -t handle adds a time delay of 1 millisecond (do not put 0 here, or it will cause Mathematica to crash). The -o **- option tells raspistill to output the image to the standard output channel (stdout) instead of saving it as a JPG to a file. Finally, adding the exclamation mark in front of the raspistill command tells the Import function to read the output of the command from the standard output channel (stdout) and the JPG at the very end of the command lets the Import function know what file format it should expect to find.

You'll find a large number of other options for using the Raspberry Pi camera, and the full documentation is available at the Raspberry Pi website [8].

If you do not have a Raspberry Pi camera module at hand but would still like to have a go with the image processing options supported by Mathematica, you should be able to import an existing image, either from file or from a web page, using the Import command:

image = Import["\

This particular example will import the Raspberry Pi logo; however, you just need to replace the URL in the above example with the file location or image URL for the picture you want to import, making sure to keep the quotation marks. For example, to import a local file:

image = Import["/path/to/your/image.jpg"]

Replace /path/to/your/image.jpg with the actual path to your image. For a file called image.jpg stored in your Raspberry Pi's home directory, this path would be /home/pi/image.jpg.

Buy this article as PDF

Express-Checkout as PDF

Pages: 2

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content