Official website for Linux User & Developer
FOLLOW US ON:
May
7

Arduino with Raspberry Pi part one

by Russell Barnes

Enjoy all the features and benefits of the Arduino microcontroller on your Raspberry Pi projects

You might be wondering why you might want to attach an Arduino to your Raspberry Pi. While there are lots of reasons, probably the most poignant is the extra six PWM-capable pins and another six analogue pins that a standard Arduino
Uno offers.

You see, while the Raspberry Pi has an excellent array of pins and capabilities, it can’t do analogue and it can’t do real-time processing out of the box. With an Arduino, additions like servos, potentiometers and a whole array of analog sensors are trivially easy to trigger and control.

The best part is that you don’t even have to program in Arduino’s quasi-C++ language. All you need is a standard USB connection between your Raspberry Pi and Arduino and a small Python package called Nanpy. Here’s how it’s done…

The Arduino is better at dealing with things like servos and analog input
The Arduino is better at dealing with things like servos and analog input

Resources

Arduino Uno
Internet connectivity
Nanpy
Tutorial files

Step-by-step

Step 01 Grab an Arduino

Before you can do anything, you need an Arduino. We recommend the Uno, since it’s the default choice with the best balance of features, convenience and affordability. Since you’ll want to put it to use straight away, we recommend investing in a ‘starter kit’ that includes LEDs, servos and all that fun stuff.

Step 02 Satisfying dependencies

We’re assuming you’re using Raspbian (recommended), so open your terminal because we need to get setuptools so we can install Nanpy. At the terminal, type:

$ wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py

$ python ez_setup.py user

Once this is complete, you’ll be able to use the easy_install command to install pyserial…

Step 3 Final preparations

Since the communication between the Arduino and Raspberry Pi will happen over the USB serial connection, we need to get the Python-serial library. At the terminal, type:

$ easy_install pyserial

We also need to install the Arduino software so the Pi knows how to deal with the device when it’s plugged in. In the terminal, type:

$ sudo apt-get update

$ sudo apt-get install arduino

Step 04 Install Nanpy

There are only two steps remaining in the configuration. First, we need to get the Nanpy package downloaded and installed on the Pi. Our preferred way is to clone it with Git. Navigate to your home folder in the terminal (cd ~) and do the following in the terminal, one after the other:

$ easy_install nanpy

$ sudo apt-get install git

$ git clone https://github.com/nanpy/nanpy.git

Step 05 Configure your Arduino Uno

Why have we cloned the original Git repository? Nanpy relies on an update to the Arduino firmware to function correctly, so you’ll need to access the firmware folder from the nanpy project directory to do it. Before typing the following into the terminal, plug your Arduino Uno into a spare port on the Raspberry Pi. Beware: the following takes some time!

$ cd nanpy/firmware

$ export BOARD=uno

$ make

$ make upload

Step 06 Testing Arduino with your Pi

With the installation finally complete, we can test the setup to make sure it works properly. Before we do a proper ‘Hello World’ application in the code segment to the right, let’s first ensure Nanpy is properly installed and the connection between Pi and Arduino is working. From your home folder (cd ~), type the following into the terminal:

$ nano nanpy_test.py

In the nano editor, simply write:

$ from nanpy imort Arduino

Now press Ctrl+X, Y, then Enter to save your new file.

Finally, in the terminal, type:

$ Python nanpy_test.py

If you don’t see an error, then everything should be working fine. Now you can play with the code across the page to start learning your way around Nanpy.

  • Tell a Friend
  • Follow our Twitter to find out about all the latest Linux news, reviews, previews, interviews, features and a whole more.
    • jamesh65

      How about the Gertduino? An Arduino type device that simply slots on to the Raspi – no cables needed.