To my knowledge the “GPIO” device driver that ships with the RPi version of Mathematica doesn’t handle pulse width modulation. I was interested in playing around with LEDs, making them flicker like candles. Essentially, I wanted to reproduce this python code in Mathematica.
I approached this problem by creating a shared library that can be accessed using Mathematica’s LibraryFunctionLoad. The shared library uses Gordon’s wiringPi functions to interface with the GPIO on the RPi. (For what it’s worth, this method, while seriously lacking in error checking, results in GPIO access frequencies of up to 13 kHz, which is much higher than what DeviceWrite[“GPIO”…] can do at this point.)
In the python code referenced above, the flickering effect is obtained by randomizing the brightness and the update interval of the LED. A high update interval is used to create a high “wind” effect. I was able to take a different approach in Mathematica by using a BinomialDistribution. This results in what I think is a more realistic flickering effect and the influence of wind can be simulated by a lower p-value (corresponding to a broader distribution of brightnesses.) If you are interested, please have a look at my video demonstrating the results. I encourage you to try it out for yourself, though, since the flickering effect doesn’t really translate well under the conditions I filmed this video.
There are a couple cool, if silly, things that can be done now that we have PWM in Mathematica. I show how you can create a fading LED with the Sin[] function, and we can even go so far as to visualize solutions to differential equations (like the documentations demonstration of a bouncing ball) using the LED. I show that at the end of the video.
I keep the code at my github repository and would be interested in hearing comments if you happen to give it a try.
Great post Bob, thanks for sharing. It’d be nice to have DeviceWrite work fast enough for PWM straight out of the box though. I haven’t been able to get it to work following using your github files on my pi yet, but will certainly keep persisting. Have you got yourself an RPi2 and tested it out on that yet?
I have not done an exhaustive check, but it looks like my code runs out of the box on the v2 model of the RPi. There are, however, some typos in my instructions. Here’s the process I followed, so please tell me where things go wrong.
clone the repository. I made a directory called ~/test/ and put it there.
I am fairly new to software distribution, and this is clearly not the best implementation. Learning how to distribute my projects more effectively is on the list of things to do…
Compile the library and move it to the directory you just created
Create a simple circuit with an LED and resistor. I have connected the circuit’s ground to the RPi’s ground and the circuit’s positive to the RPi pin 16 (GPIO 23 or in the wiring pi definition, pin 4). Note that my software currently only recognizes the wiringPi definition. Load up wolfram (don’t forget to sudo)
sudo wolfram
Now you should be able to load the library and use it
Thanks for the reply, I was able to follow your install instructions seemingly without any problems, however I cant load the library from sudo wolfram.
compile.sh neatly produces two files libwiringpi.so.1.0.2 and wiringpilink.o
With the file copied into the Linux-ARM directory (using cp ~/test/wiringPiLink/libwiringpi.so.1.0.2 wiringPiLink/LibraryResources/Linux-ARM) and running inside sudo wolfram, I get the following:
In[1]:= <<wiringPiLink`
Unable to open SPI device: No such file or directory
I have Gordons most recent wiringPi installation in ~/wiringPi and am able to compile and run his examples. It’d be great to know if you have any ideas where I have made an error, staying inside Mathematica to access pwm is awesome and Id love to get your code up and running.
Please try one more thing; make sure the the lib file is in ~/test/wiringPiLink/wiringPiLink/LinuxResources/Linux-ARM and that you are running sudo wolfram from ~/test/wiringPiLink (the directory tree structure is quirky, I know). I’m not convinced this is the actual problem, since I get a different error when I reproduce this. However, I want to rule out directory structure.
I have just the lib file in the Linux-ARM directory
~/test/wiringPiLink/wiringPiLink/LibraryResources/Linux-ARM $ ls
libwiringpi.so.1.0.2
Running sudo wolfram from ~/test/wiringPiLink
pi@raspberrypi ~/test/wiringPiLink $ sudo wolfram
still produces an SPI error
In[1]:= <<wiringPiLink`
Unable to open SPI device: No such file or directory
I then ran gpio load spi and got an error
pi@raspberrypi ~/test/wiringPiLink $ gpio load spi
ERROR: could not insert 'spi_bcm2708': No such device
gpio: Unable to load spi_bcm2708
pi@raspberrypi ~ $ gpio load spi
ERROR: could not insert 'spi_bcm2708': No such device
gpio: Unable to load spi_bcm2708
I think we are close. That error indicates that you don’t have the SPI interface enabled, which isn’t necessary for blinky lights, but I started to work on some MCP3008 interfacing, which does use SPI, so it needs to be enabled in order to get the library to work.
sudo raspi-config
under advanced options, select SPI and enable it. You can also select to have it automatically loaded at boot (which should prevent the need to do gpio load spi). Reboot and give it a shot.
Thanks again for your efforts here BoB. Managed to get my class of first time users of Raspberry Pi and who had never coded anything before to build an RC type vehicle out of lego (precision blocks with great range of gearing and steering systems) and control it over wifi using two DC motors, an L293E and an RPi2; powered with a portable $9 5V 1A power supply. All coding was done with Wolfram Language, and with wiringPiLink, PWM was a walk in the park.
I was wondering whether there was any way to call wiringPiLink` from a mathematica session running a remote kernel on the pi from my macbook?
It turns out you can. Which is super great!!
I installed wiringPiLink, following previous instruction, but changing the directory to /usr/bin/. After opening a remote kernel session in Mathematica 10.2.0.0 on my macbook with the following remote kernel advanced options configuration:
Thanks for the update, Hans. I’m glad you’ve been able to make something out of WiringPiLink. (And I’m particularly impressed that you got the remote kernel up and running; I have found it very buggy and temperamental, but as you’ve no doubt noticed, being able to run the interface on a normal computer makes the Wolfram/RPi experience much more enjoyable.)
For what it’s worth, Raspbian Jessie appears to drop the need for GPIO users to be sudo’d. I am finishing up another project at the moment, but my intention is to revisit WiringPiLink to make sure it works with the current hardware/software. Stay tuned.