10 Raspberry Pi Arduino as an Interface Using Firmata

ard plus pi header

The Arduino serves as a great front-end for the Raspberry Pi. The Raspberry Pi can control the Arduino via the Firmata protocol ‘pyfirmata’.

alert alert

Prerequisite: Arduino Project #12: Firmata Test: Control Board for Arduino. Do that project first to set up the Arduino with the Firmata Standard sketch.

learn first

Silly ‘Rivalry’

040210_rock_em_sock_em_robots_2

Our culture has a competitive spirit: who’s sports teams is the best; what is the best game system; who’s the greatest? When writes try to pit the Arduino against the Raspberry Pi it is just silly because they are completely different kinds of hardware; each great and impactful in their own right. Arduino is a microcontroller; it has no operating system; though it has rugged hardware and can provide a lot more current than the Raspberry Pi’s GPIO ports (can sink or source up to 40mA at 5.0V; the Pi is 3.3V and can only manage a measly 3mA). The Arduino also has 6 analog ports and hardware PWM; the Pi has none. The Raspberry Pi, on the other hand, is a complete computer system with a choice of operating systems and vastly more memory. The Arduino has a 16MHz clock while the Pi runs at 2GHz. The Pi3 has built in WiFi and Ethernet. Both have enabled more mere mortals to engage technology more than any previous platforms.

Specification Comparison

comparison1

Due to open source; an Arduino clone may be purchased off of E-Bay for $5 while the ‘Genuino’ from Italy can run up to $30.

Some applications are risky for hardware; especially where high current or environmental exposure are involved. It would make more sense to sacrifice an Uno clone than a Pi due to their cost of replacement. So their strengths and weaknesses complement each other; they work wonderfully together as a team: the Pi for control and network or Web interface and the Arduino for interfacing to sensors, motors, etc.

Python Control of an Arduino

py-and-pi

raspberry-pi-to-arduino-via-usb-cable

An Arduino is normally programmed using C++ while the most common language used on the Raspberry Pi is Python (though practically any programming language could be used). Python is preferred for its readability and ease of use; it is a higher-level language; meaning that there is more abstraction concerning minute hardware details. Python has less dependence upon cryptic syntax and symbols; though it can be persnickety about indentation and the number of spaces.

handson

pot shield LED

Connect a Grove potentiometer to its shield’s A0 port using a Grove cable. Connect a Grove LED to D2 with another Grove cable.

Firmata Control Panel Sanity Check

firmata control z.png

Open the Firmata Test icon and toggle Pin 2 high and low to make sure the Firmata system is working. Adjust the potentiometer and note the values on port A0 vary between 0 and 100%. (If this does not work; go back to Arduino Project 12). Close the Firmata Test Panel.

nav to idle3

Open the Python 3 (IDLE) editor.

python shell

This will seem tedious; but it will help to see what the program does later.

Type:
>>> from pyfirmata import Arduino, util, serial, time
>>> board = Arduino(‘/dev/ttyUSB0’)
>>> board.digital[2].write(1)    {this should turn on LED}

>>> board.digital[2].write(0)   {this should turn off LED}
>>> it = util.Iterator(board)
>>> it.start()
>>> board.analog[0].enable_reporting()
>>> board.analog[0].read()   {this should return a number between 0 and 100%}
{rotate the pot a little}
>>> board.analog[0].read()  {this should return a different number}

[In an actual program you would turn off the reporting of analog values by the call disable_reporting() on the pin object so as not to flood the serial buffer with unwanted data]
All of this is a little tedious in IDLE; but the aim is to learn the commands so that an actual Python program can run the code automatically.

arduinopifirmata.py Program

open ardatapy

Open arduinopifirmata.py program.

#firmata LED control: Python program controls Arduino


import serial
import time

from time import sleep

from pyfirmata import Arduino, util
board = Arduino('/dev/ttyUSB0')


it = util.Iterator(board)
it.start()
board.analog[0].enable_reporting()
time.sleep(1)   #delay gives the A/D time to settle
x = board.analog[0].read()
x = float(x)



while True:
        try:
            x = board.analog[0].read()
            x = float(x)
            board.digital[2].write(1)
            time.sleep(x)
            board.digital[2].write(0)
            time.sleep(x)
        except KeyboardInterrupt: 
            board.digital[2].write(1)
            break
        except IOError:
            print("Error")
         



# print board.digital[2].read()


# board.analog[0].read()

Run the program. As you rotate the potentiometer the LED’s rate of flashing should vary from to fast to perceive to very slow.

Do This At Home

do this at home

For Firmata Pi control of Arduino
sudo apt-get install python-pip python-serial
sudo pip install pyfirmata {yo, lower-case ‘f’}

navigate to pyfirmata folder: (probably cd pifirmata)
sudo python3 setup.py install

Consider building a data logger to record some physical property over time.

m