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.
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.
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.
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:
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.
$ python blink.py
and watch the led on your Arduino blink. When you are ready, press control c to end the script.
Special thanks go to the following for inspiring and informing this post: