# Pulse Width Modulation using Mathematica on the RPi

Posted 9 years ago
17456 Views
|
12 Replies
|
3 Total Likes
|
 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.
12 Replies
Sort By:
Posted 8 years ago
 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.
Posted 8 years ago
 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:And after not too much fussing (mostly just a bit of reading up on Get[ ] it turns out that running Get["wiringPiLink",Path->{"/usr/bin/wiringPiLink"}] successfully loads the library and we now have the slightly more polished experience of MMA10 with wiringPiLink. Posted 8 years ago  Thanks BoB! As soon as I enabled SPI it all worked a perfectly. Looking forward to using it. Cheers Hans Posted 8 years ago  Hi BoB,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]:= < Posted 8 years ago  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. Posted 8 years ago  Hi Bob,Thanks for the reply, I was able to follow your install instructions seemingly without any problems, however I cant load the library from sudo wolfram.All smooth sailing with github clone: pi@raspberrypi ~$ git clone git://github.com/bobthechemist/wiringPiLink.git Cloning into 'wiringPiLink'... remote: Counting objects: 31, done. remote: Total 31 (delta 0), reused 0 (delta 0), pack-reused 31 Receiving objects: 100% (31/31), 7.93 KiB, done. Resolving deltas: 100% (10/10), done. 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]:= <
Posted 8 years ago
 Hans,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.
Posted 8 years ago
 Hans,Also (and perhaps try this first) from the command prompt type gpio load spi prior to starting wolfram.
Posted 8 years ago
 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?
Posted 8 years ago
 Hans,I do have a v2 and will check my github code and get back to you. I haven't tested the code on that device yet.
Posted 8 years ago
 Hans,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. git clone git://github.com/bobthechemist/wiringPiLink.git Now, cd ~/test/wiringPiLink and make two directories mkdir wiringPiLink/LibraryResources mkdir wiringPiLink/LibraryResources/Linux-ARM 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 ~/test/wiringPiLink/compile.sh cp ~/test/wiringPiLink/libwiringpi.so.1.0.2 wiringPiLink/LibraryResources/Linux-ARM 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 `<
Posted 9 years ago
 This is amazing Bob. I'll definitely give it a try! Here is the bouncing ball effect from your demo: