In an earlier post I mentioned briefly about using pySerial to send data to and from the arduino using Python, in this post I’ll go through a bit more about how to set this up.
First you’ll need pySerial, which can be downloaded from here. For windows users there is an installer package. For other OSs, download the source from the same link and in a terminal enter the pyserial-x.y directory and then for python3 users enter _python3 setup.py install _else enter python setup.py install.
Usage
I’ll walk you through an example of how to get a python script sending and receiving data from an arduino. The main idea is that we’ll open up a connection to the arduino, send a message and then have the arduino send back the message to the python script.
Python
First we import the pySerial library and then as we’ll need to set the script to sleep later on we’ll also need to import _time. _
In line 9 we open a connection to the arduino that in my case was connected to _‘/dev/tty.usbserial-A600agDn’ _but may be different for you. The easiest way to find out what port your arduino is using is whilst your arduino is connected to the PC open the arduino IDE and then Tools->Serial Port. The port your device is connected to will have a tick next to it. The second argument, 9600, refers to the speed of the connection, I’m using an arduino duemilanove so it may be different if you’re using an different board.
Line 12 is very important, when a connection is made via serial to the arduino, the arduino board will reset itself. I didn’t know this at first and it caused me a whole heap of pain later on with the script being unable to read data the arduino had sent. So, we set the script to sleep for 2 seconds which is just enough time for the arduino to reset itself.
On line 23 we send a single character to the arduino, this could be anything but I’ve just used ‘3’ for now. If this fails than an exception will be thrown, an error message displayed and the connection will be closed gracefully.
On line 32 we set the script to sleep for a second, this gives the arduino enough time to reply back to the message it was sent. In line 37 we then just read a single byte from the serial and output the message and afterwards close the connection.
Arduino Code
So, now for the arduino code, this quite a simple sketch and I’ll go through the main bits.
In line 1 we just define a char variable that will hold the received message. I’ve just set this to ‘0’ so it is easy to compare to when we have received data. In the setup function we begin the serial connection with the correct speed.
In the _loop _we use a while loop with the condition Serial.available() > 0, if there is data available this will be greater than 0 and the body of the while loop will execute. In the body we just set msg to be the data available.
In line 14 we do a quick check to see whether msg is not the defined value, i.e. it’s been given some value from reading serial and then we print the value back to the serial connection and set msg back to ‘0’.
And that’s how to send and receive data to an ardunio. If you have any issues with the code then just post a comment and I’ll see if I can help