Message Boards Message Boards

Using the GPIO with the Wolfram Language + Raspberry Pi (updated)

Posted 11 years ago

This post will demonstrate how to use the GPIO pins with the Wolfram Language on a Raspberry Pi. This is an update of this post.

The following hardware is necessary:

First, setup up the breadboard as shown below in the Fritzing Diagram. Pins 4, 17, 18, 22, 23, 24, 25, & 27 should all be hooked up to an LED, with each LED getting its own resistor. Alternatively, as long as no more than one of the LEDs is on at a time, you can do this with one resistor. The Cobbler breakout board plugs into the Raspberry Pi, and make sure that you plug it in correctly, as nothing will work if it is plugged in backwards.

Fritzing diagram of Cobbler breakout board hooked up to 8 LEDs

The GPIO interface requires root privilege for access so the Wolfram Language or Mathematica needs to be started as root for this experiment.

In a terminal start the Wolfram Language using the following command (as root):

> sudo wolfram

Wolfram Language (Raspberry Pi Pilot Release)
Copyright 1988-2013 Wolfram Research
Information & help: wolfram.com/raspi

In[1]:= 

First we define the pins that correspond to connected LEDs:

pins = {4,17,27,22,18,23,24,25}

Next we can turn on individual LEDs by writing the value '1' to it, which corresponds to turning it on:

DeviceWrite[ "GPIO", First[pins] -> 1 ]

And of course turn it back off, by writing the value '0':

DeviceWrite[ "GPIO", First[pins] -> 0 ]

Or turn the LEDs on and off one at a time:

Do[ 
 DeviceWrite[ "GPIO", pins[[i]]->1 ]; 
 Pause[.2]; 
 DeviceWrite[ "GPIO", pins[[i]]->0 ];
 ,{i,8}] 
POSTED BY: Ian Johnson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract