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
$ 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

A.pinMode(led, A.OUTPUT)

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"

And a port of the basic Fade sketch:

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

A.pinMode(led, A.OUTPUT)

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 

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.

Refresh your Pi
Playing with the Raspberry Pi's camera
  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?


  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

    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


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

    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. :)


  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:

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

    Have fun :)

    • David Daniel
    • January 27th, 2015 4:13pm

    I have a problem while writing.
    The RX Light on the Arduino flashes once, than the Arduino shuts down and reboots and does nothing.

  5. Check out this post:
    My guess is the Arduino is getting sent a reset signal.

    • Martin
    • June 6th, 2015 3:46pm

    Hey Guys, Im currently researching for big project and i have worked with Arduino(My Comfort Zone) I want to work with Raspberry Pi too but i dont see how it comes into the picture. If its Automation. I can do that with Arduino. I just thought adding The Raspberry might deliver more functionality , power and flexibility. Advice guys

  6. Really need more info on what you’re doing: Can you describe the ‘big project’ and how you think a RPi would fit into it?

    • Nacanieli Tabua
    • August 7th, 2016 8:45pm


    I am trying to make a controller for a robotic arm. I would like to use the configuration as such as I will take readings from the 6 encoders on all my motors and my 5 limit switches via the Raspberry Pi GPIO terminals…..An algorithm will run (inverse kinematics and forward kinematics) to which it will generate the proper signals for the Arduino to receive via USB serial and produce the appropriate PWM signal needed for the motor controllers…..

    Is this possible for the master-slave protocol presented????

    • Plague
    • December 9th, 2016 8:22am

    Hi Nacanieli,

    It would be best to use the Arduino to take all the readings from your sensors and then use the Raspberry Pi to process all the stuff you want to do (Your algorithm)

    The Raspberry Pi is not usually set up for real-time. That’s why people connect an Arduino or other MCU to it.

    Sorry if this made no sense, but I’m still learning myself.

  1. No trackbacks yet.