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.
Bridge.begin();

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:

#!/usr/bin/python

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

chmod +x leds.py

Now you should be able to execute the python script:

./leds.py

Or simply:

python leds.py

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

Step 4: Python Code

#!/usr/bin/python 

# Test to (un)set pin 12 and 13 on the Arduino Yun.
# H.Zimmerman, 09-12-2014.
# henszimmerman@gmail.com
 
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):
    value.put('D12','0')
    value.put('D13','1')
    sleep(0.1)
    value.put('D12','1')
    value.put('D13','0')
    sleep(0.1)
 
print("I hope you enjoyed the light show\n")