# Hello World Wolfram + Mathematica + Raspberry Pi + Arduino

## Introduction to Wolfram Language — #raspSerie 09

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*). Does not need any specific because this will be the task of the Wolfram language.

**Wolfram** : it already comes factory installed in raspi 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 run Wolfram from Raspberry Pi Model B**. It has a processor speed of 700 MHz and memory of 512 MB SDRAM @ 400 MHz. It is very scarce resource for run 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 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…

pi@raspberrypi:~ $ sudo wolfram

Wolfram Language (Raspberry Pi Pilot Release)

Copyright 1988–2016 Wolfram Research

Information & help: wolfram.com/raspi

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

— — — — — — —— — — — — — -Matematica — — -— — — — — — — — — —

Graphics ->

2dGraph:

In[1]:=

Plot[Sin[x], {x, 0, 5 Pi}]

3dGraph:

In[2]:=

Plot3D[x * Cos[x] * Sin[x + y], {x, -2 Pi, 2 Pi}, {y, -2 Pi, 2 Pi}]

Algebra ->

In[3]:=

N[Pi, 100]

3.14159265358979323846264338327950288419716939937510582097494459230781\

6406286208998628034825342117068

Solving Equations →

In[4]:=

Solve[2x+17=25, x]

In[5]:=

Solve[{

s³ == 27,

s t c² == 96,

c + s t == x},

{s , t , c, x},

Integers

]

Out[5]{{s -> 3, t -> 2, c -> -4, x -> 2}, {s -> 3, t -> 2, c -> 4,

x -> 10}, {s -> 3, t -> 8, c -> -2, x -> 22}, {s -> 3, t -> 8,

c -> 2, x -> 26}, {s -> 3, t -> 32, c -> -1, x -> 95}, {s -> 3,

t -> 32, c -> 1, x -> 97}}

— — — — — — — — — — — Raspberry Pi — — — — — — — — — — — — — — -

Basic Examples:

Find GPIO devices on the system:

In[6]:=

FindDevices[“GPIO”]

Configure pin 4 for writing and pin 17 for reading:

In[7]:=

DeviceConfigure[“GPIO”, {4 -> “Output”, 17 -> “Input”}]

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

In[8]:=

DeviceRead[“GPIO”, 11]

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

In[9]:=

DeviceWrite[“GPIO”, 4 -> 1]

DeviceWrite[“GPIO”, 4 -> 0]

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

In[10]:=

pins={4,17,27,22,18,23,24,25}

Out[10]= {4, 17, 27, 22, 18, 23, 24, 25}

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

In[11]:=

Do[DeviceWrite[ “GPIO”, pins[[i]]->1 ];Pause[.2];DeviceWrite[ “GPIO”, pins[[i]]->0 ];,{i,8}]

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.

In[12]:=

dev = DeviceOpen[“Arduino”,”/dev/ttyACM0"]

Out[12]= DeviceObject[{Arduino, 1}]

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

In[ ]:=

DeviceWrite[ device , n -> val ]

In[13]:=

DeviceWrite[“Arduino”, 13 -> 1 ]

Out[13]= 13 -> 1

In[14]:=

DeviceWrite[“Arduino”, 13 -> 0 ]

Out[14]= 13 -> 0

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 Quantity 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 read 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 times and see what results:

In[15]:=

DeviceRead[“Arduino”,{{15, 16}}]

Out[15]= <|15 -> 0 volts, 16 -> 5.00000 volts|>

In[16]:=

DeviceRead[“Arduino”,{{15, 16}}]

Out[16]= <|15 -> 3.57283 volts, 16 -> 5.00000 volts|>

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!

In[17]:=

DeviceRead[“Arduino”, {“A0”,”ReturnFunction” -> (Distance[#*1/58.2,”m”] &)}]

Out[17]= Distance[0., m]

In[18]:=

DeviceRead[“Arduino”, {“A0”,”ReturnFunction” -> (Distance[#*1/58.2,”m”] &)}]

Out[18]= Distance[0.0315442, m]

In[19]:=

DeviceRead[“Arduino”, {“A0”,”ReturnFunction” -> (Distance[#*1/58.2,”m”] &)}]

Out[19]= Distance[0.0515464, m]

— — — — — — — — — — — — — -Closing Arduino Device — — — — — — — —-

DeviceClose[dev]

Crt + C

Interrupt -> abort

or…

In[20]:=Exit[]

pi@raspberrypi:~ $ (returns for Raspberry Pi prompt)

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

**links**

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 Wolfran Language? Let me know in the comments or in my youtube page!

Goodbye!!

“Still, I hope that the power and beauty of the language that I have nurtured for more than half my life will shine through, and that many students and other people, with many diverse backgrounds, can use this book to get started with the Wolfram Language and get involved with the kind of computational thinking that is quickly becoming a defining feature of our times.”

Stephen Wolfram