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.

Refresh your Pi
Playing with the Raspberry Pi's camera
  • Trackback are closed
  • Comments (21)
  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 :)

    • 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:
    http://www.akeric.com/blog/?p=1140
    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

    HI…

    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.

    • Hemanth Kumar
    • April 5th, 2017 11:18am

    Hi,
    I’m working on an Adafruit Trinket 3V. Is there a way that I can write a similar program in python to access the GPIO on the trinket like the Arduino?
    I found a lot of examples and code to write the program to the trinket but, I couldn’t find any python program to directly write it to the trinket and get data from it.

    Any help is appreciated!

    Thanks.

  7. While I’ve used the Trinket before myself, I’ve never tried to do what you’re asking: Maybe someone else will chime in.

    • Hemanth Kumar
    • April 5th, 2017 11:51am

    @AKeric
    Thank you.
    Hope someone does.

    • Sushant Shetty
    • April 22nd, 2017 9:53am

    How do you import servo library for arduino using this method?

  8. Look at this page for what libraries they support. Looks like servo is in there:
    https://github.com/nanpy/nanpy/blob/master/nanpy/__init__.py

    • Sushant Shetty
    • April 23rd, 2017 5:16am

    Thanks a lot for your help.
    But I get this error while importing arduino-“ImportError: cannot import name Arduino”
    The code works fine if I use ArduinoApi instead of Arduino in “from nanpy import Arduino” in the code. All the above steps are performed without any issues. I’m using python-2 IDLE.

  9. There’s a good chance they’ve changed the API since I first authored that post. But it sounds like you figured it out 😉

Comment are closed.