Porting SparkFun Inventor’s Kit to Raspberry Pi and Python using nanpy

Aim: having just figured out how to make an Arduino slave to the Raspberry Pi (see this post), it’s time to see what is possible using this set up. SparkFun have created (IMHO) a really nice set of circuits as part of their ‘Inventor’s Kit v3.2′. The aim for the next twelve posts is going to be to follow their circuits, porting the Arduino code to Python using the nanpy Python library.

Circuit 1: Blinking an LED – now we covered this in our first post looking at setting up the Arduino as slave (here).

Circuit 2: Potentiometer – this involves using a potentiometer to change the rate at which an LED blinks. A potentiometer is just a fancy name for a variable resistor. A resistor is very simply, something we use in electronics to control the current flowing through a circuit. The higher the resistance, the lower the current (see Ohm’s law for more on this relationship). We are now able to measure this using an analogue pin on the Arduino and make use of it within a Python programme running on our Pi!

IMG_20141221_193117.jpg [continue reading]

Tim Greenwood

Tim currently works for Living Systems. His background is in psychology, and he has been working as a psychologist within education and the community for the last ten years. He recently gained a Masters of Arts in Management, Learning, and Leadership from Lancaster Business school focusing on the use of Action Research to promote curious and evolving ways of working with individuals, groups and organisations.

More Posts - Website

Circuit 2: Potentiometer

Circuit 2: Potentiometer – this involves using a potentiometer to change the rate at which an LED blinks. A potentiometer is just a fancy name for a variable resistor. A resistor is very simply, something we use in electronics to control the current flowing through a circuit. The higher the resistance, the lower the current (see Ohm’s law for more on this relationship). We are now able to measure this using an analogue pin on the Arduino and make use of it within a Python programme running on our Pi!

It will be a good idea to read the SparkFun online guide to circuit #2 first – here.

Step 1: set up your Raspberry Pi and Arduino nanpy mashup (see here to find out how)

If you have used your Arduino using the Arduino IDE since you uploaded the special nanpy ‘firmware’ you will need to upload it again.

Step 2: write a cool Python script (or just use the code here)

''' import modules needed '''
from nanpy import Arduino
from nanpy import serial_manager
from time import sleep

''' create a serial connection '''
serial_manager.connect('/dev/ttyACM0')

''' Set analogue pin A0 to input mode '''
POT = 0 
Arduino.pinMode(POT, Arduino.INPUT)

''' Set digital pin 13 to output mode '''
LED = 13
Arduino.pinMode(LED, Arduino.OUTPUT)

''' decide if you want to print out delay each loop '''
printDelay = True

''' let people know the LED fun is about to start '''
print "starting"

''' while True basically means do this forever (or until someone presses control and c) '''
while True:
        # turn on LED
        Arduino.digitalWrite(LED, Arduino.HIGH)
        # wait (according to reading from potentiometer)	
        delay = Arduino.analogRead(POT)/1500.0
        if printDelay:
                # if printDelay == True, print out delay
                print 'delay: ' + str(delay)
                
        sleep(delay)
        # Turn off LED
        Arduino.digitalWrite(LED, Arduino.LOW)
        # wait (according to reading from potentiometer)
        sleep(delay)

Step 3: set up the circuit

See SparkFun documentation here.

Step 4: play with the potentiometer and watch to see your potentiometer blink at different rates

What happens if you change the number 1500.0 in our code?

What happens if we just use the reading from A0 without dividing it by a number at all?

Tim Greenwood

Tim currently works for Living Systems. His background is in psychology, and he has been working as a psychologist within education and the community for the last ten years. He recently gained a Masters of Arts in Management, Learning, and Leadership from Lancaster Business school focusing on the use of Action Research to promote curious and evolving ways of working with individuals, groups and organisations.

More Posts - Website

Experiment: use an arduino as a slave to your raspberry pi

IMG_20141219_180749.jpg

 

Why do this?

If you have got past the initial excitement of having a Raspberry pi, and tried getting into experimenting with inputs and outputs (i/o for short), you might have stumbled across the fact that the Raspberry Pi only has the ability to read digitally from it’s GPIO (general purpose input/output) pins.

IMG_20141219_175420.jpg

So if you have a variable resistor (potentiometer) or a light dependent resistor (or any other analogue sensor) – you won’t be able to measure anything but 1 or 0 (that is how digital works, you are either HIGH or LOW, as opposed to analogue which can be anything in between). Now there are some cool ways around this if you are into your electronics (see here or here). But there are a couple of good reasons for using an Arduino as ‘slave’ to read analogue inputs.

IMG_20141219_180655.jpg

Firstly, the Arduino is very reliable and safe – it has been designed for tinkering. You can make a few mistakes (within reason) when plugging in wires and choosing resistors. The Pi on the other hand is a big wus. Plug a 5v wire into one of its GPIO digital pins (designed for 3.3 volts) and you are likely to fry it! The other big reason to slave the Arduino – is that you can get the full power of Python (including libraries like PyBrain for Neural Networks) hooked up to the outside world through the Arduino’s analogue capabilities.

IMG_20141219_182802.jpg

Yes, using the Arduino IDE (integrated development environment aka a program that makes it easy to write Arduino software) is good and more than enough to keep anyone going even on very complex projects. But using BOTH the Pi and Arduino has it’s advantages IMHO. You get the power of the Pi and Python (think camera, image processing, audio etc) as well as the analogue, safe, reliability of the arduino. (some will be thinking it might be easier to just get a breadboard and a 10 bit ADC, and you might be right, but I would argue that, once set up, this is easier for those new to electronics).

[all experiments are conducted at your own risk]

 

Step 1: Install the Arduino IDE

Make sure you have an internet connection. Open up LXTerminal and type in:

$ sudo apt-get update

This updates the Raspberry Pi with an up to date list of software that is available.

Then type in the following:

$ sudo apt-get -y install arduino

This installs the Arduino IDE on your Pi using the program ‘apt-get’. The -y just means say yes to any questions during the installation.

You might be wondering why we have to install the Arduino IDE after all the fuss about using Python to connect with your Arduino. The reason is – we can use it to install Sketches/firmware on the Arduino, which is really handy for trouble shooting (more on this later).

 

Step 2: Install nanpy (a Python library) using setup tools 

nanpy will help us write the Python code which will help us set up the Arduino as slave to the Pi! AKA we will be able to read and write from the Arduino’s analogue pins.

$ wget http://bootstrap.pypa.io/ez_setup.py - O - | sudo python

(Make sure to use a capital O, not a zero, or you will get errors.)

 

Step 3: Install pyserial

nanpy needs to be able to talk with the Arduino using what is called a ‘serial connection’. To do this we need to install pyserial.

First download the pyserial tar.gz file using wget. 

$ wget http://pypi.python.org/packages/source/p/pyserial/pyserial-2.7.tar.gz

Now use tar to ‘unzip’ it:

$ tar zxf pyserial-2.7.tar.gz

(hint: if you press tab after starting to type ‘pyserial-2.7.tar.gz’ it will autocomplete the rest – which means no more typos! If you get nothing, try pressing tab twice)

Next move into the newly ‘unziped’ directory:

$ cd pyserial-2.7 (again tab works a treat here)

We can now use python to install the pyserial library:

~/pyserial-2.7 $ sudo python setup.py install

Step 4: Install nanpy 0.8

Move back to the home directory if you haven’t already:

~/ cd ~

Then download the nanpy 0.8 tar.gz:

$ wget https://pypi.python.org/packages/source/n/nanpy/nanpy-v0.8.tar.gz

Like we did in step 3, just go ahead and unzip it and install it.

$ tar zxf nanpy-v0.8.tar.gz

$ cd nanpy-0.8

~/nanpy-0.8 $ sudo python setup.py install

Step 5: This step is not entirely needed (see step 1) but is a good way to check everything is okay with the Pi → Arduino usb connection.

 Open the Arduino IDE from the Pi menu:

IMG_20141219_182856.jpg

Menu → Programming → Arduino IDE

Plug in the Arduino to a powered usb hub – not directly into your pi. This makes sure the Pi gets enough current to work properly.

Click Tools → Serial Port and select ‘/dev/ttyACM0’

This tells the Arduino IDE where to connect, in this case ‘/dev/ttyACM0’. 

Click open, which opens a menu. Chose 01. Basics → Blink

This opens a ‘sketch’ which will blink on and off the orange LED built onto the Arduino (pin 13).

Click upload to compile the sketch and load it onto your Arduino.

If everything is okay, you should see a blinking light on your Arduino?

 

Step 6: Upload a special firmware used to work with nanpy v0.8

Your Arduino will need you to load a special sketch on it (firmware) for it to work with nanpy. To do this, you will need to make it first.

Move from the nanpy-0.8 directory into the firmware directory using cd:

~/nanpy-0.8 $ cd firmware

Then type in the following:

~/nanpy-0.8/firmware $ export BOARD=uno

~/nanpy-0.8/firmware $ make

(you will get lots of output)

~/nanpy-0.8/firmware $ make upload

(you should see upload progress displayed)

 

Step 7: Run a simple python script to see if it is working 

Now to test out your first Python AND Arduino experiment!

First we will write a simple python script using the text editor nano.

$ nano blink.py

When nano opens, type in the following Python code:

''' import from nanpy and time modules '''
from nanpy import Arduino
from nanpy import serial_manager
from time import sleep

''' create a connection to the Arduino '''
serial_manager.connect('/dev/ttyACM0')

''' set pin 3 as an output '''
LED = 13
Arduino.pinMode(LED, Arduino.OUTPUT)

''' set the blink rate '''
delay = 1

print "starting"

''' while true aka start an infinite loop of led blinking '''
while True:
    Arduino.digitalWrite(LED, Arduino.HIGH)
    print delay
    sleep(delay)
    Arduino.digitalWrite(LED, Arduino.LOW)
    sleep(delay)

Press control and x to exit the file.

Type y to save changes.

Then type:

$ python blink.py

and watch the led on your Arduino blink. When you are ready, press control c to end the script.

 

Sources

Special thanks go to the following for inspiring and informing this post:

themagpi.com

Tony Goodhew and Raspberry Pi forum community

nanpy V0.8

Tim Greenwood

Tim currently works for Living Systems. His background is in psychology, and he has been working as a psychologist within education and the community for the last ten years. He recently gained a Masters of Arts in Management, Learning, and Leadership from Lancaster Business school focusing on the use of Action Research to promote curious and evolving ways of working with individuals, groups and organisations.

More Posts - Website