Hello World Wolfram + Mathematica + Raspberry Pi + Arduino

Introduction to Wolfram Language — #raspSeries 09

J3
Jungletronics
7 min readDec 3, 2016

--

Hi Hobbyist!
This is Wolfram Mathematica !!! The world’s definitive system for modern technical computing! At least that’s what they promise…

I have made this neat little and the next tutorial to show you interaction with Arduino and raspberry pi in a very fun journey, you can bet !!!
You can go deeper into the Wolfram language later. But first, let’s practice what we can do with it!

Here is my hardware/software base:

Raspberry Pi: with an image with PIXEL desktop based on Debian Jessie: RASPBIAN JESSIE WITH PIXEL; Menu > Programmer > Arduino loaded into raspi -> do this in a terminal: “sudo apt-get install arduino” after “sudo apt-get update” and “sudo apt-get upgrade” as usual…

Arduino Uno: with the sketch ‘Example/Basic/BareMinimum’ loaded (I´m using Arduino v.1.6.12). It does not need any specific because this will be the task of the Wolfram Language.

Wolfram: it already comes factory installed in Raspberry Pi for our convenience!!!

A full version of the Wolfram Language is available for the Raspberry Pi computer and comes bundled with the Raspbian operating system, as I said.
Programs can be run from a Pi command line or as a background process, as well as through a notebook interface on the Pi or on a remote computer.
On the Pi, the Wolfram Language supports direct programmatic access to standard Pi ports and devices.

In my initial experience is been difficult to run, Wolfram, from Raspberry Pi Model B. It has a processor speed of 700 MHz and a memory of 512 MB SDRAM @ 400 MHz. It is a very scarce resource for run a notebook in its glory on desktop operations. But it works fine at the Command-Line Programs!

Here’s a good reason for me to buy version 3 of the raspberry pi that has 1.2GHz 64-bit quad-core ARMv8 CPU and 1GB RAM. Wooh!!

Options using Wolfram Language:

>Command-Line Programs

>wolfram — Wolfram Language command-line interface (/usr/bin/wolfram)

>mathematica — Mathematica notebook interface (/usr/bin/mathematica)

I won’t be trying to create something visually stunning (although we can do it with the adoption of the Computable Document Format — CDF.

Get you Raspberry (Mine is Model B) and access it via SSH session and type…

Here are twenty commands for you to appreciate Wolfram Language power:

— — — — —— — — — — — -Matematica -— — — — — — — — — —
Graphics ->

2dGraph:

3dGraph:

Algebra ->

Solving Equations →

— — — — — — — — — Raspberry Pi — — — — — — — — — — — — — -
Basic Examples:

Find GPIO devices on the system:

Configure pin 4 for writing and pin 17 for reading:

Read the digital value on pin 11 — press a button hold and type again:

Set pin 4 to digital “High”: the first led on breadboard’s led row:

Set an array of value for pin 4, 17, 27, 22, 18, 23, 24, 25 — in a Single-Row-LED-Light-Bar mode:

A simple script for loop from pin i to 8, turning on and off each led:

DeviceWrite[“GPIO”, First[pins] ->0 ]

— — — — — — — — — — -Configure Arduino — — — — — — — — —
Opening the Device
In[ ]:= DeviceOpen[“Arduino”,name]
opens the Arduino on the serial port with the specified name. Typical names on Unix-based systems are “/dev/ttyXXX” or “/dev/tty.usbserialXXX” and under Windows are “COM1”, “COM2”, etc.

— — — — — — — — — — -Writing Data on Arduino — — — — — — —

In[ ]:=
DeviceWrite[ device , n -> val ]

The value val must be either 0 or 1, unless pin n supports Pulse Width Modulation (PWM).
DeviceWrite also supports lists of rules.
PWM is supported on pins 3, 5, 6, 9, 10, and 11. On these pins, val can be any real number between 0 and 1, or any rational number of the form, where is an integer between 0 and 255. This corresponds to the duty cycle of the PWM timer on that pin.
PWM pins also support writing a Number of volts between 0 and 5 that is scaled to a duty cycle.
Note: By default, “Arduino” allows any pin to be used interchangeably in reading and write mode. DeviceConfigure can be used to configure specific pins as read or write.

— — — — — — — Reading Data from Arduino — — — — — — — — -
DeviceRead[dev,{{n1,n2,…}}]
reads the value of each pin ni, returning an association of ni to the pin’s value.

In[ ]:=
DeviceRead[ device , n ]
reads the value of pin n.

Increase knobs pin 15 to max value them get it back- [pin 15 (A1) attached on 3v3 and pin 16 (A2) attached on 5v]; repeat a couple of times and see what results:

DeviceRead[dev,{n,”ReadMode”-> mode}]
reads the mode value of pin n.
Possible values for “ReadMode” are “Digital” and “Analog”.
Reading from an analog pin defaults to performing an analog read of that pin. Reading a digital value can be obtained by using “ReadMode”->”Digital”.
Only pins “A0” (14), “A1” (15), “A2” (16), “A3” (17), “A4” (18), and “A5” (19) support analog reading. Any pin supports digital reading.

— — — — — — — — HC-SR04 Ultrasonic Sensor simulation — — — —
DeviceRead[dev,{n,”ReturnFunction”-> (func)}]
reads the value of pin n, applying func to the value before returning it.

HC-SR04 Ultrasonic Sensor
Calculate the distance (in cm) based on the speed of sound. distance = duration/58.2 — turns the pot(pin A1) and see…transform var on-the-fly!

— — — — — — — — — — — -Closing Arduino Device — — — — — —-
DeviceClose[dev]
Crt + C
Interrupt -> abort
or…

That’s it!

As Wolfram Language is still new we do not have many tutorials out there !! But, yes, I think the tool is interesting, especially for speakers and hobbyists. To the next episode!!!

Download All Archives for This Project

Credits & References
Wolfram — Arduino
Wolfram — HowToUse
Arduino and MathematicaComputation in the Maker World

Inspiration: Arnoud Buzing Tutorial

Have some cool ideas or already created some apps using Wolfram Language? Let me know in the comments or in my youtube page!
Goodbye!!

Related Posts

10 #raspSerie —Wolfram & Reading potentiometer — HowTo playing with graphs — Wolfram Mathematica!

00#raspiSeries — Hello Pi!!!

01#raspiSeries — Meet the Raspberry Pi!!!

02#raspiSeries — How to Flash an SD Card for Raspberry Pi !!!

03#raspiSeries — Raspberry Pi — Passwordless SSH Access — Windows 10 !!!

04#raspSeries — Raspberry Pi — Setting Up a VNC On Your Pi !!!

Credits & References

--

--

J3
Jungletronics

😎 Gilberto Oliveira Jr | 🖥️ Computer Engineer | 🐍 Python | 🧩 C | 💎 Rails | 🤖 AI & IoT | ✍️