# Using the GPIO with the Wolfram Language + Raspberry Pi

GROUPS:
 Arnoud Martijn Buzing ™ 12 Votes This post shows how to use the GPIO with the Wolfram Language on a Raspberry Pi. To recreate this experiment you will need the following hardware (in addition to the Raspberry Pi itself):Super Bright Blue 5mm LEDPi T-Cobbler Breakout KitFull sized breadboardMale/male jumper wiresA 400 ohm resistor (to prevent the LEDs from burning out).Set up the breadboard as shown: Plug the T-Cobbler into the breadboard with 13 pins in the E column and 13 pin in the G column. Use the jumper wiresto connect pins 4, 17, 27, 22, 18, 23, 24 and 25 to evenly spaced free rows lower on the breadboard. Connect 8 blue LEDs from each jumper wire row tothe blue - column, with the flattened cathode side on the blue - column. Complete the circuit by connecting the resistor from the blue - column to the GND pin.Connect the ribbon cable to the T-Cobbler and the Raspberry Pi correctly, and turn on your Raspberry Pi.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 wolframWolfram Language (Raspberry Pi Pilot Release)Copyright 1988-2013 Wolfram ResearchInformation & help: wolfram.com/raspiIn[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: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}]
3 years ago
18 Replies
 Andreas Kohlmajer 1 Vote Hello,have you already tested the GPIO speed? Currently, I am using wiringPi (http://wiringpi.com/) to use the GPIOs at high speed. If the Wolfram language is sufficiently fast, I could move from compiled C to Wolfram language. There is a nice speed summary available at http://codeandlife.com/2012/07/03/benchmarking-raspberry-pi-gpio-speed/. Unfortunately, I have no oscilloscope to test it...
3 years ago
 Arnoud Martijn Buzing ™ 3 Votes Andreas,The DeviceWrite function still needs work to be fast. If you use lower level Wolfram Language functions, like OpenWrite and Write you get about 3.1KHz to 3.5KHz: pi@raspberry-wri2 ~ $sudo wolfram Wolfram Language (Raspberry Pi Pilot Release) Copyright 1988-2013 Wolfram Research Information & help: wolfram.com/raspi In[1]:= file = OpenWrite["/sys/class/gpio/gpio4/value"] Out[1]= OutputStream[/sys/class/gpio/gpio4/value, 56] In[2]:= Do[Write[file,1];Write[file,0],{100000}] // AbsoluteTimingOut[2]= {32.227557, Null}In[3]:= 100000/32.227557Out[3]= 3102.93You can improve the speed to 3.5KHz by using BinaryWrite (in which case you have to write the ascii codes for 0 and 1 (48 and 49): pi@raspberry-wri2 ~$ sudo wolfram Wolfram Language (Raspberry Pi Pilot Release) Copyright 1988-2013 Wolfram Research Information & help: wolfram.com/raspi  In[1]:= file = OpenWrite["/sys/class/gpio/gpio4/value", BinaryFormat->True]  Out[1]= OutputStream[/sys/class/gpio/gpio4/value, 56] In[2]:= Do[ BinaryWrite[file,49];BinaryWrite[file,48], {100000}]; // AbsoluteTimingOut[2]= {28.047030, Null}In[3]:= 100000/28.047030Out[3]= 3565.44(Additionally you can link in a c compiled exectable using mathlink or librarylink, but that would just give you the c performance.)
3 years ago
 I have two each of red, blue, yellow and green leds, and 10K and 180 ohm resistors from a canakit ultimate starting kit.  Can I use these resistors to complete this project?
2 years ago
 I tried putting two 180 ohm resisters in series, thinking 360 ohms might be about right.Nothing happened.It's a lot of work entering commands in "sudo wolfram".  Will "sudo mathematica" start the notebook interface as root?
2 years ago
 BoB LeSuer 1 Vote You can use Ohm's law (V = IR)  to determine if the resistors you have are appropriate for your LEDs.  LEDs that I've seen have max current limits in the range of 15 to 20 mA.  If you are powering your LED from a 3.3V GPIO pin, then 3.3/180 ~ 18 mA which is the upper limit of your LED - it's possible that this might burn out your LED.  The 10K resistor will give you about 0.3 mA of current which will light your LED dimly, if at all. Resistors do add in series, so two 180 ohm resistors in series will give you 360 ohms of resistance, and this would result in a current of about 9 mA which is probably good for your application (assuming you didn't burn out the LED with the single 180 ohm resistor).As for sudo mathematica, yup, it works.
2 years ago
 > Will "sudo mathematica" start the notebook interface as root?Yes.> It's a lot of work entering commands in "sudo wolfram". This is true (and you lose all the typing at the end of your session). As an alternative, you canalso use:> sudo wolfram -script filename.mwhere filename.m is a file containing Wolfram Language commands. Or you can write a shell style script like this (using a shell editor like vi or pico or nano):#!/usr/bin/wolfram -scriptPrint["Hi there"];and then make it executable in the shell with 'chmod a+x hi.m' and then running it like a shell command:> ./hi.mHi there>
2 years ago
 Roger Kirchner 1 Vote Sucess, with two 180 ohm resisters in series.  (Also one 180 ohm resister works.)  Fortunately, I didn't ruin my Pi having the ribbon cable plugged in backwards!  When I switched it around, the LEDs blinked like they are supposed to.  I bought the CanaKit Ultimate starter kit from Amazon for \$89.95, which includes a new model RPi, power supply, nice black case, 8 GB SD with several operating systems, usb wifi, HDMI cable (which I connect to a TV), small breadboard (I put 4 LEDs on each side of the board.), ribbon cable, Pi Cobbler, wires, eight LEDs, a tri LED, ten 10K and five 180 ohm resisters, and a couple of switches.   I also got a powered USB hub into which I plug an Apple keyboard/mouse.Wolfram language installed with 'sudo apt-get update && sudo apt-get install wolfram-engine', but only wolfram, not Mathematica was found under the Education menu.  Was able to start Mathematica from a root terminal with '/opt/Wolfram/WolframEngine/10.0/Executables/Mathematica',  but then read 'wolfram' and 'mathematica' start the command line and notebook interfaces from a terminal.  Just found http://reference.wolfram.com/language/guide/RaspberryPi.html.It's really neat to be able to control a Pi with Mathematica.
2 years ago
 Anyone know what would be needed to generate control voltages (with 1 volt per musical octave) via this GPIO? I'd love to be able to use Mathematica to control a modular synthesizer
2 years ago
 We need DeviceRead examples.  Here is one.In the example above,pins = {4,17,27,22,18,23,24,25}I modified the (equivalent of) the above breadboard by replacing the LED at  #4 with a button, using a 10K resister to hold #4 to 1.  Pressing the button will set #4 to 0.  One might expect DeviceRead["GPIO",4] to return 0 or 1, depending on whether the button is pressed or not.  Actually it returns either {4->0} or {4->1}.The following will turn the LED connected to #17 on when the button is pressed and off when it is released:inv=DeviceRead["GPIO",4];While[inv=={4->1}, inv=DeviceRead["GPIO",4]];DeviceWrite["GPIO", 17->1];While[inv=={4->0}, inv=DeviceRead["GPIO",4]];DeviceWrite["GPIO", 17->0],The following will successively light the LEDs connected to 17,27,22,18,23,24,25 when the button is pressed 7 times.Do[  inv=DeviceRead["GPIO",4];  While[inv=={4->1}, inv=DeviceRead["GPIO",4]];  DeviceWrite["GPIO", pins[[i]]->1];  While[inv=={4->0}, inv=DeviceRead["GPIO",4]];  DeviceWrite["GPIO", pins[[i]]->0],  {i,2,8}]Be sure the button is properly seated.  If it isn't, you might experience what I did:  The first time the button was pressed, the LEDs at pins[[2]],...,pins[[6]] flickered and the LED at pins[[7]] stayed on.  The second time it was pressed, the LED at pins[[8]] stayed on.  And that was it.Note:  This post has been edited to provide more detail.
2 years ago
 My apologies to the group.When I made the previous post, I was getting a bizzare result and was wondering why.  The button had not been properly seated.  The post has been edited.Another mistake led to my post about not being able to run Mathematica on the Pi using VNC.  I could see the Raspian desktop, but could not start Mathematica.   The problem was a typo in the autostart file for tightvncserver.  Using VNC Viewer on an iPad, it feels like you're running Mathematica on the iPad.  That is really neat.At least I didn't make a post about nothing happening when I first tried to light LEDs, when I had the ribbon connector backwards.
2 years ago
 There is an updated version of this post available here.
2 years ago
 Dear All, I have trying toggle LED through GPIO. Toggling single LED works. But i tried, the Do loop getting the - A pin number of list of pin numbers is expected.Please refer the attached screen shot for details. Any suggestion welcome. (hope it will be useful for others as well)With regards, Chandra
1 year ago
 Hi Chandra, I think the error you're seeing is from not taking the fourth part of the variable pins. For example, I think what you need ispins[[4]]and not pins[4]The first one will take the fourth member of the pins list, while the second one will try and call a function named pins with an argument of 4. Hope this helps, and let us know how it goes! Thanks, Ian
1 year ago
 Dear Ian,I have tried with pins[[4]] as well. I am using the second script on shown in the screen shot. It gives the same error Not sure..why? Further, I have connected single LED on GPIO pin 4. Appreciate you help. Any documentaion ? Many thanks Chandra
1 year ago
 Okay, so when you use the Do loop, you only need to have {4} and not{4,4}You can check the documentation for Do athttp://reference.wolfram.com/language/ref/Do.htmlHere, you are using form 2 as mentioned in the documentatuon, and you need form 1, as you're not iterating over anything, you just want to run it a certain number of times.