วันพุธที่ 1 พฤษภาคม พ.ศ. 2556

Raspberry Pi and Arduino USB Communication


Raspberry Pi and Arduino

Note. There is now a followup to this post here.

The Raspberry Pi is creating quite a storm of interest. I have just got mine and one of the first things that I wanted to try was to get it talking to an Arduino over USB using Python.



.. and you know what? It proved to be a lot easier than I expected. This is mainly because, after all, despite its diminutive price tag, the Pi is just a Linux box. I got communication working both ways, with the Arduino sending 'Hello Pi' to the Pi and at the same time, testing for a digit coming in. When it receives a digit, it flashes the number of times indicated by the digit.

Arduino

Let's start with the Arduino end. I used an Arduino Uno and Arduino software version 1.0. I haven't tried an older board, but I suspect the FTDI generation Arduinos before the Uno may have trouble with USB.

Here is the sketch - paste it into a new Arduino IDE window and load it up onto your Arduino using your regular computer.


const int ledPin = 13;


void setup()
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}


void loop()
{
  Serial.println("Hello Pi");
  if (Serial.available())
  {
     flash(Serial.read() - '0');
  }
  delay(1000);
}


void flash(int n)
{
  for (int i = 0; i < n; i++)
  {
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    delay(100);
  }
}



Raspberry Pi

There is a Python library for serial communications called 'pySerial' which has history with Arduino. So, I stood on the shoulders of giants and adapted the instructions found here.

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. I saved it to the 'other' folder on 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



Step 5. Run Python 2. You will find this from the menu under Programming - Use Python 2 not 3.

Thats it! Now we just need to write some Python to access the Serial port. So type the commands shown in the transcript below.


You type the parts after >>>

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

Note that the second argument here (9600) is the baud rate and should match whatever you put in your Arduino sketch.


/dev/ttyACM0 is the name for the USB interface to the Uno, at least it was for my Uno. The way to discover the port name is to run the following command in the terminal without the Uno plugged in.

ls /dev/tty*


Then plug in your Arduio and run the command again. If there is a new name, then this is the name of your port.

Now lets start a loop listening for messages from the Arduino.

while 1 :
    ser.readline()

You will need two hit enter twice after you type the second line. Messages should now start to appear!

You can see in the Blue writing where the Arduino is talking to the Pi. Then some error trace as you press ctrl-C to interrupt the messages coming from the Arduino.

When you type

ser.write('5') 

you should see the LED on the Arduino flash 5 times.

There are many possibilities here, we could put a motor shield or LCD shield onto the Arduino and control it from your Pi.

ไม่มีความคิดเห็น:

แสดงความคิดเห็น