Control your Arduino via Python with your Raspberry Pi

I recently ran across the nanpy library for Python when looking at different internet radio projects using the Raspberry Pi.  It allows you to easily control your Arduino from Python, and it installs on the Pi in a snap:

First, install Arduino:

$ sudo apt-get install arduino

Next, install the nanpy source:  This is needed to later build the new Arduino firmware:

$ cd ~
$ curl -O https://pypi.python.org/packages/source/n/nanpy/nanpy-v0.8.tar.gz
$ tar xvf nanpy-v0.8.tar.gz
$ rm nanpy-v0.8.tar.gz

Now install the required Python libs:

$ sudo pip install nanpy
$ sudo pip install pyserial

Hook up your Arduino to one of the Pi’s USB ports, and create/upload the new firmware (using an Arduino Uno as an example):

$ cd ~/nanpy/firmware
$ export BOARD=uno
$ make
$ make upload

From there, programming my Pi via the Adafruit WebIDE (and with my Arduino hooked up to a breadboard with the required led’s and resistors),  I recreated a few basic Arduino sketches to see how it worked.  It worked as expected, simple and easy.

Here is a port of the basic Arduino Blink sketch:

from nanpy import Arduino as A
led = 13

# SETUP:
A.pinMode(led, A.OUTPUT)

# LOOP:
while True:
    A.digitalWrite(led, A.HIGH); # turn the LED on (HIGH is the voltage level)
    print "blink on"
    A.delay(1000); # wait for a second
    A.digitalWrite(led, A.LOW); # turn the LED off by making the voltage LOW
    print "blink off"
    A.delay(1000);

And a port of the basic Fade sketch:

from nanpy import Arduino as A
led = 9
brightness = 0 
fadeAmount = 5

# SETUP:
A.pinMode(led, A.OUTPUT)

# LOOP:
while True:
    # set the brightness of pin 9:
    A.analogWrite(led, brightness)
    # change the brightness for next time through the loop:
    brightness += fadeAmount
    # reverse the direction of the fading at the ends of the fade: 
    if brightness == 0 or brightness == 255:
        fadeAmount = -fadeAmount
    # wait for 30 milliseconds to see the dimming effect 
    A.delay(30)

Only a few core libraries are currently supported.  To see the list, you can visit this page.

On a side note I should point out I went to great lengths to get this working on my Mac, without a lot of success.  You can check out my Python Wiki post on it, under the “Mac Notes” section.

  1. Hi,

    Once you downloaded the firmware to your Ardunio does that mean you can no longer use it with the standard Ardunio IDE or will downloading from the Ardunio IDE still work?

    Cheers,
    Alastair

  2. @Alastair Montgomery
    The Arduino IDE on my mac still recognized it, and I was still able to upload sketches to it that way, so all seems well :)

    • Alastair
    • January 13th, 2014 2:15am

    @AKeric
    Thanks :)

    • David
    • May 4th, 2014 3:11pm

    Great, great, great write up. I am having a problem with the nanpy firmware. When I am in the firmware directory I enter “export BOARD=uno” then I enter “make” I then get this error.

    make: *** makefile: Is a directory. Stop.

    Do you happen to know what that means. I have been stuck at that position for a week now. Any help would be greatly appreciated. Thanks in advance.

  3. Sorry, but I do not, my Linux kung-fu is weak. I’m surprised I get any of this stuff to work at all half the time :) Hopefully someone else will reply.

    • Nathan
    • May 20th, 2014 3:48pm

    @David

    Hi David, I encountered the same issue. My solution was to follow the first 4 steps exactly as described here:

    http://www.instructables.com/id/Arduino-Raspberry-Pi-Internet-Radio/?ALLSTEPS

    I know you will be thinking ‘but I have already done this’ – But this is the solution that fixed everything straight away for me. I hope it helps you too. :)

    Nathan

  4. Thanks Nathan!

    • Andrea Stagi
    • June 17th, 2014 4:20pm

    Hi I’m Andrea Stagi, creator of Nanpy! thanks for this interesting article, I’d like to let you know that Nanpy reached version 0.9 and now you need to compile the firmware with Arduino IDE. Please follow the step on the official Nanpy page: https://github.com/nanpy/nanpy

    You’ll also notice two different projects for firmware and python lib:

    https://github.com/nanpy/nanpy-firmware
    https://github.com/nanpy/nanpy

    Have fun :)

  1. No trackbacks yet.