# Using the GPIO with the Wolfram Language + Raspberry Pi

Posted 5 years ago
83707 Views
|
23 Replies
|
18 Total Likes
|
 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}]
23 Replies
Sort By:
Posted 5 years ago
 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...
Posted 5 years ago
 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.)
Posted 1 year ago
 The program at the opening of this post is critically flawed. I appreciate the insight into the programing of Mathematica but there needs to be two pauses in the program. If the program follows the pattern {on,pause,off} the light will flick on and off so fast it will not be visible. The program should follow the pattern {on,pause,off,pause} before it repeats. If one more pause is added the program it works as expected.
Posted 1 year ago
 ...Don't look now but DeviceWrite["GPIO", 18->1] doesn't work on MMA V11.0 anymore, at least not on my Raspberry Pi. It throws up a bunch of error messages. Can anyone please confirm this issue on their own machine?
Posted 1 year ago
 Hi Francesco,This is a known problem that had a fix pushed out a few weeks ago. It appears as if your machine didn't automatically pull down the update, you can manually update with the following: PacletUpdate["MRAALink"] This should return something like Paclet[MRAALink, 1.0.5, <>]. Then restart the kernel and try again. I just tested this with a fresh install of Raspian and Mathematica v11.0.1 and it worked for me.Thanks,Ian
Posted 1 year ago
 I'm late to this discussion but I wondered about something I noted on the 40 pin connector on the Pi. Pin 12 and pin 33 are marked as PWM0 and PWM1 respectively. In my experience You set the Pulse Width Modulator's frequency and the pulses symmetry then just turn it on. No further code is necessary. Has anyone looked into this?Robert Dieter
Community posts can be styled and formatted using the Markdown syntax.