Picture of Arduino and Python
For a project (check out my blog for updates) I'm working on I needed to be able to communicate with my arduino, luckily the arduino can communicate though serial. It turns out almost any programming language can be used for serial communication but python seems to be the easiest so far.
I had some problems getting it all to work and finding a basic tutorial online proved very difficult. So hopefully this guide will explain the basics to anyone that is looking to start using the serial functions that the arduino provides.

I'm going to assume that if you're reading this you have some knowledge of how an arduino works and how to upload sketches and what not, however I will explain the arduino code when we get there. The python code is very basic but I will do my best to explain it as well. 

The code is not that hard to understand, the hard part is understanding how the serial communication works.

So, I'm going to show you how to tell your arduino to blink using your computer. Once you understand this you should be able to expand both the python code and the arduino code to fit your own projects. 

Step 1: Sorting out Python

Picture of Sorting out Python
Now, obviously, we're going to need python if we want to do anything so we better get that! If you don't have it installed, head over to the python website and download it! (http://python.org/download/)

Once we have python installed were going to need a new library called PySerial. This is going to provide all the functions and methods we will need to talk to our arduino! 

If you're using a windows machince check out their source forge page for the windows installer.
If you're using MacOS/linux your going to have to look around the PySerial website. 

Also, if you're familiar with using Eclipse you might be intersted in the Python add-on for eclipse. Check it out if you would like to program in the eclipse environment.

Now if everything is installed we can actually start writing our Python program!
Harish AG28 days ago

Thanks!!!! it helped me a lot....

prappo.prince3 months ago

Thanks bra

Arduino4Me5 months ago

Thanks for the tutorial!

when i run the python script it says access is denied...any ideas on how to fix? Thanks.

I'm late to mention it, but for future reference that usually means you have the COM port already open in the arduino IDE. or sometimes it can just be that you didn't close the window properly. It's annoying thing I used to have more problems with early on, the handling of the COM window is finicky. I sometimes would have to restart my computer to get it to clear.

To clarify, only 1 program can access the com port at a time.

for the COM port...

mccreamark9 months ago

Thank you, I was looking for a nice simple example:

I used it with python3.3 with these changes :

Change the comport string COMX to a single integer one below the port# you are using , example:

ser = serial.Serial(COM6, 9600) becomes ser = serial.Serial(5, 9600)

and add a b prefix for python3.3 serial write example:

ser.write( "1") becomes ser.write(b"1")

Thanks again

Ah ,thanks! b"string" is simpler than bytearray("string") and explains to me better why I had to do that change ^_^

BradB112 months ago

Great introduction to communicating between an Arduino and a computer

Tbus1 year ago

I just finished a home-study Python programming course (http://www.ciebookstore.com/python-programming-cou... ) and this Instructable was an awesome test for my new programming skills. Thanks!

jsalam11 year ago
sorry not working
aatifof062 years ago
what does

means, exactly that 1..

and how to stop that blink using python.
amaituino2 years ago
I love what you did ! Thanks !!
tchen22 years ago
Im running python using mac. I have pyserial installed and was able to communicate with Arduino using terminal. However, when i tried to use eclipse, it stops me at import serial. Any idea how to over come this problem? Thanks!
Thanks works linke a charm!
Hustleer2 years ago

Can you post a mac version of the tutorial?
that would be nice. Good job on the tutorial though :)
zaaap2 years ago
At fiest I wanna thank you for submitting your tuotial here, its very nice and helpful however it has some flaws.
This Arduino code will blink for infinity UNLESS you 'clear' memory (which stores one value at a time). One way is to read from serial (eg. in Arduino software) and second one is replacing Serial.wite('0'); with Serial.read();
After doing this it works like a charm!
I'm a bit confused. You say your "while" loop is meant to continue looping until the device is plugged in, yet it seems to me it will either throw an error or valuate "true" on the first iteration.

I believe "serin = ser.read()" is meant to be your trigger here, that if it succeeds it will continue to go onto the next line "connected == "True"", and skip to the next iteration if it fails.

This however shouldn't work. If the device is not connected and you call "serin = ser.read()", it will throw a "ValueError" exception. Even if this line of code passed fine through the prompt, the next line "connected == "True"" would run regardless of what happened in the previous line.

Your proper while loop should look like this:

while not connected:
       serin = ser.read()
    except ValueError:
    connected = "True"
jbolt3 years ago
I also copied & pasted your code. I get the same error as krithimagic. I'm also a novice at this.

File "C:\Python32\lib\site-packages\serial\serialwin32.py", line 236, in read
raise SerialException("ReadFile failed (%s)" % ctypes.WinError())
serial.serialutil.SerialException: ReadFile failed ([Error 6] The handle is invalid.)

I am running Python 3.2 on Windows 7.

I believe this problem has something to do with Windows 7 being 64 bit. I have tried the various solutions I have found on the internet, but nothing seems to work.

Using a different sketch, I can see results that come back from the Arduino board in the Arduino Serial Monitor, so serial communication must be working. This leads me to think the problem is with Python or perhaps pySerial.

I'd appreciated any help or advice you might be able to provide.
jbolt jbolt3 years ago
As a follow-up for anyone who may be reading this in the future, my problem was due to improperly installing pySerial. After I got that problem straightened out, Bobbert72's software worked as described.
krithimagic3 years ago
Hey! I'm a real novice to python coding and I copied your Python code word to word. It works great until I have to write

while not connected:
serin = ser.read()
connected = True

that's when I get an error:

Traceback (most recent call last):
File "", line 2, in
serin = ser.read()
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 236, in read
raise SerialException("ReadFile failed (%s)" % ctypes.WinError())
SerialException: ReadFile failed ([Error 6] The handle is invalid.)

Please help me out!
Bobbert72 (author)  krithimagic3 years ago
Sorry for the late reply!
At first glance it looks like your python code is trying to read from a file and not from the arduino's serial port. Make sure you have PySerial and your importing it properly. Although that really shouldn't be a problem since you aren't getting an error at the line above where you declare the variable ser.

Is your arduino plugged in to your computer? If it is, make sure the program is pointing to the right COM port. Ill edit the instructable to explain that process more clearly.

Check out the documentation for Python and PySerial on their website and mess around with the code a little.