Introduction: Having Python Talk to the Arduino Yun

So you want python 2 (running on the Linino side of your Arduino Yun) to turn pins on and off at the Arduino side of your Yun. Here's how to do that.

Step 1:

In the image you can see two LEDs that are connected to digital pins 12 and 13 via a 220 Ohm resistor. If that's Greek to you, check out the Arduino tutorial that explains how to blink a LED.

To talk to the Arduino from the Linino side of your Arduino Yun, you will use the Bridge library. It's standard part of your Arduino installation. To include it in your Arduino code:

#include <Bridge.h>

To start the bridge, initialize it in your setup() routine:

// Start using the Bridge.

In this example, Python will talk to the bridge via two variables:

// Here we will hold the values coming from Python via Bridge.
char D12value[2];

char D13value[2];

Then it's just a matter of asking for the current value:

Bridge.get("D12", D12value, 2);

Bridge.get("D13", D13value, 2);

In the next step, you see the complete code for the Arduino.

Step 2: Arduino Code

// Arduino Yun listens to python script via Bridge library to turn digital pins on/off.
// H.Zimmerman, 9-12-2014. // Arduino Yun. #include <Bridge.h> #include <stdio.h> // Here we will hold the values coming from Python via Bridge. char D12value[2]; char D13value[2]; void setup() { // Zero out the memory we're using for the Bridge. memset(D12value, 0, 2); memset(D13value, 0, 2); // Initialize digital pins 12 and 13 as output. pinMode(12, OUTPUT); pinMode(13, OUTPUT); // Start using the Bridge. Bridge.begin(); } void loop() { // Write current value of D12 to the pin (basically turning it on or off). Bridge.get("D12", D12value, 2); int D12int = atoi(D12value); digitalWrite(12, D12int); // An arbitrary amount of delay to make the whole thing more reliable. YMMV delay(10); // Write current value of D13 to the pin (basically turning it on or off). Bridge.get("D13", D13value, 2); int D13int = atoi(D13value); digitalWrite(13, D13int); // An arbitrary amount of delay to make the whole thing more reliable. YMMV delay(10); }

Step 3: Now the Python Side of Things

The python script running on the Linux Linino installation on your Yun also uses the Bridge library. The example in the next step will simply blink the lights in succession. I'm sure you can make it do something more interesting. In fact, I made this instructable after having made an Arduino Yun night light for my daughter (see picture). Pin 12 connects to an orange LED that tells her it's okay to wake up and pin 13 connects to a red LED that tells her it's still sleepy time. She loves it as much as her parents do! Because the Arduino Yun is connected to the home network, I can simply ssh into it from any location and change the times the LEDs go on and off. This is all taken care of by a cron script.

To make sure the python script can be executed, you must do two things. First, make sure this is the first line in your python script:


Next, make the script executable. E.g. if it is called, you will type this command in the Terminal that connects to the Arduino Yun Linino shell:

chmod +x

Now you should be able to execute the python script:


Or simply:


The complete python script is in the next and last step. Happy Yun'ing!

Step 4: Python Code


# Test to (un)set pin 12 and 13 on the Arduino Yun.
# H.Zimmerman, 09-12-2014.
import sys
sys.path.insert(0, '/usr/lib/python2.7/bridge')
from time import sleep
from bridgeclient import BridgeClient as bridgeclient
value = bridgeclient()
for idx in range(0, 100):
print("I hope you enjoyed the light show\n")


russ_hensel (author)2016-01-23

I love the combination of Python and the Arduino. So I have created a collection about it. I have added your instructable, you can see the collection at: >>

ARRUMOLANE (author)2015-09-20

hi sorry totally new at this, i downloaded winscp, and logged on succesfully, but im lost, where to i place the .py file and do this>>chmod +x , sorry if its too dumb a question.

hzimmerman (author)ARRUMOLANE2015-09-21

Sure, no problem. You can use ssh to get command line access to the unix shell of your Yun. In the case of Windows, you can download the free PuTTY ssh client. Then place the python scripts wherever you want to execute them, probably in the home directory of the root user. ssh lets you execute unix commands (like chmod) from the command line. Good luck!

amberrayh (author)2015-02-06

Thanks for sharing! Congratulations on your first Instructable!

About This Instructable




Bio: Too much television as a kid.
More by hzimmerman:Computing complex circuits with a TI calculatorHow to make your own delicious and healthy breadControlling a relay board with an Arduino Uno
Add instructable to: