Instructables

Interface Python and Arduino with pySerial

Picture of Interface Python and Arduino with pySerial
Logo.png
Over the last few months I have learned how to program with Python. With one of the upcoming projects that I am working on it would be nice to have a computer’s display to view the data collected by a rover in real-time as well as crunch numbers while the rover completes its  mission. The rover will have an Arduino as a brain. What I found after some searching was pySerial. This is a really neat piece of software that allows Python to send and receive data much like the Serial Monitor does.

pySerial is available to download at


 
Remove these adsRemove these ads by Signing Up

Step 1: Installation

Picture of Installation
Once you download it open up Terminal and type in:

tar xfvz /Users/*Account*/Downloads/pyserial-2.6.tar.gz
cd pyserial-2.6
sudo python setup.py install



To make sure that everything installed correctly open up Idle and type in 'Import Serial'. If no errors appears then everything is good to go.

You can check the available ports with the line

ls /dev/tty.*

Step 2: Program the Arduino

Picture of Program the Arduino
Now to test it out, upload the below sketch to your Arduino. I do not know how this will or will not work on Arduino clones.


void setup() {
Serial.begin(9600); // set the baud rate
Serial.println("Ready"); // print "Ready" once
}
void loop() {
char inByte = ' ';
if(Serial.available()){ // only send data back if data has been sent
char inByte = Serial.read(); // read the incoming data
Serial.println(inByte); // send the data back in a new line so that it is not all one long line
}
delay(100); // delay for 1/10 of a second
}