How to make Program your Arduino with Raspberry Pi
![]() |
How to make Program your Arduino with Raspberry Pi |
Enjoy all the features and benefi ts 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 realtime 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…
What you’ll need
Arduino Uno
Internet connectivity
Nanpy
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.
02 Satisfying dependencies
We’re assuming you’re using Raspbian (recommended), so open your terminal because we need to get
The best part is that you don’t even have to program in Arduino’s quasi-C++ language
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…
The best part is that you don’t even have to program in Arduino’s quasi-C++ language
03 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 updatesudo apt-get install arduino
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 nanpysudo apt-get install gitgit clone https://github.com/nanpy/nanpy.git
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/firmwareexport BOARD=unomakemake upload
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
You’ll learn
1. Playing to strengths
While the RasPi is much more powerfu than Arduino, the latter has the upper hand when it comes to interfacing with the real world. Leverage both their
strengths to make better projects.
0 Commentaires