---

Tuesday, July 3, 2012

Raspberry Pi meets Arduino - part 2

I gave a short talk and demonstration yesterday evening at the #raspberryjam event in Preston. I demonstrated the Pi raising some servo driven flags attached to an Arduino. I would have demonstrated communication in the other direction using a ultrasonic rangefinder, but I ran out of time. That can be my next post.

Here is the code and details of the electronics for all this.

Flags



First of all the electronics. Here is a wiring diagram for the servos. 


The servo control pins are wired to D2 and D3.

The Arduino is powered by a separate power supply into its DC power socket. I used a 9V 1.5A power supply. The USB lead is connected from the Arduino to the Pi. I was running tightVNC on the Pi, so I could remote control it from my Mac without having to attache keyboard mouse and TV.

Arduino

Here is the Arduino sketch.
#include <Servo.h>

#define MIN_ANGLE 30
#define MAX_ANGLE 160

#define SERVO_1_PIN 3
#define SERVO_2_PIN 2

Servo servo1;
Servo servo2;
int servo1pos = MIN_ANGLE;
int servo2pos = MIN_ANGLE;

void setup()
{
  servo1.attach(SERVO_1_PIN);
  servo2.attach(SERVO_2_PIN);
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available())
  {
    char ch = Serial.read();
    if (ch == 'a')  servo1pos = MAX_ANGLE;
    else if (ch == 'A')  servo1pos = MIN_ANGLE;
    else if (ch == 'b')  servo2pos = MAX_ANGLE;
    else if (ch == 'B')  servo2pos = MIN_ANGLE;

  }
  servo1.write(servo1pos);
  servo2.write(servo2pos);
  delay(20);
}

All nice standard stuff. It listens on the serial connection for one of the single character commands 'a', 'A', 'b' or 'B' and when it gets one it raises or lowers the appropriate flag.

Python

The Python script from the Raspberry Pi end is equally simple. However you will need to install the pyserial library.

Step 1. If you are not reading this page on your Pi, then switch now, so you can copy and paste.

Step 2. Browse to here and download pyserial-2.5.tar.gz (106.3 kB) and save it somewhere convenient like the Desktop.

Step 3. This is a gziped tar file. Which needs unzipping and untaring. To unzip it open a Terminal, which you will find from the 'start menu' under 'accessories'. Now paste the following commands into it.
cd /home/pi/Desktop/other
gunzip pyserial-2.5.tar.gz
tar - xvf pyserial-2.5.tar

Step 4. Install pySerial, by typing these lines in your terminal window:
cd pyserial-2.5
sudo python setup.py install



Startup IDLE (use Python 2 NOT 3) and open a new file, pasting the following code into it:


from Tkinter import *
import serial
import time

DEVICE = '/dev/ttyACM0'
BAUD = 9600
ser = serial.Serial(DEVICE, BAUD)

root = Tk()

def aUp() :
    ser.write('a')
    return

def aDown() :
    ser.write('A')
    return

def bUp() :
    ser.write('b')
    return

def bDown() :
    ser.write('B')
    return

Button(text='A up', command=aUp).pack()
Button(text='A down', command=aDown).pack()
Button(text='B up', command=bUp).pack()
Button(text='B down', command=bDown).pack()


root.mainloop()


Then run the script and the four buttons should appear.

Here is a short video showing the flags in action.






About the Author
These are my books. Click on the image below to find out more about them.




                                                                                                                           

             


8 comments:

NBV said...

I would actually love to know which servo's you are using, if you would say or even link where to get them that would be totally epic and i thank you

Simon Monk said...

Hi, they are pretty standard 9g servos. I bought them on eBay ages ago, so sorry, no link. Any small servo should be fine.

Bob H said...

Should code up a semaphore library, that would be very fun with the flags and servos!

Unknown said...

This was a good suggestion that you put up here...dude…..hope that it benefits all the ones who land up here. 
Electrical Wholesalers 40 spanyank

Anonymous said...

This is great but I want to be able to use a keyboard rather than a mouse so how could I do it so that whenever the 'A' key is down pin whatever on the arduino is HIGH and when the 'A' key is released it is low again? I want to be able to control 4 pins with 4 keys. Thanks

MotoCiclante said...

Hi Simon. Impressive job, for me. You think that servo could be driven pressing a specific key on the keyboard?
Key (A) means flag up
Key (B) means flag down

Thank you

Mr B said...

Hi Doc, this is very cool! I've been looking at using arduino with my Rpi for a while and this is a cracking method! Thank you

Avoidance Behaviour said...

I am finding that IDLE won't run from my command line. Any ideas as to why this might be? Anyone else had this problem? Some of other forums suggest doing a fresh install of the OS which is annoying.