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

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!
<p>can we write a python code on Arduino IDE.</p><p>please do reply.</p><p>thank you!</p>
<p>Not on Arduino IDE but you can use another IDE, Zerynth Studio: <a href="https://www.zerynth.com/zerynth-studio/">https://www.zerynth.com/zerynth-studio/</a></p><p>With Zerynth you can program Arduino (32-bit MCU) boards directly in Python</p>
currently, no. the only way is using pySerial (and compiling a .py script to .ino (not sure its possible))
<p>Hello,</p><p>have a look at the Pumbaa project. It is available in the Arduino IDE.</p><p>https://forum.arduino.cc/index.php?topic=424405.0</p>
<p>Hi,</p><p>I have tried to make this project using a Relay rather than an LED. I can get the relay to switch using Arduino and Python together, but then the Arduino continues to loop, switching the relay on and off, rather than ending once it has sent &lsquo;0&rsquo; back to python to indicate it has done the loop. Any ideas why this may be? </p><p>Thanks</p><p>Martin</p>
<p>I love the combination of Python and the Arduino. So I have created a collection about it. I have added your instructable, you can see the collection at: &gt;&gt; <a href="https://www.instructables.com/id/Arduino-and-Python-and-perhaps-a-Rasberry-Pi/"> https://www.instructables.com/id/Arduino-and-Pytho...</a></p>
<p>Hi guys, if you're looking for programming Arduino in Python, take a look at VIPER!website: <a href="http://www.viperize.it" style=""> www.viperize.it</a></p><p>instructables profile: <a href="https://www.instructables.com/member/VIPER+IoT/">https://www.instructables.com/member/VIPER+IoT/</a></p>
At fiest I wanna thank you for submitting your tuotial here, its very nice and helpful however it has some flaws. <br>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(); <br>After doing this it works like a charm!
<p>Thx for the fix !</p>
<p>Thanks!!!! it helped me a lot.... </p>
<p>Thanks bra </p>
<p>Thanks for the tutorial!</p>
<p>when i run the python script it says access is denied...any ideas on how to fix? Thanks.</p>
<p>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. </p>
<p>To clarify, only 1 program can access the com port at a time. </p>
<p>for the COM port...</p>
<p>Thank you, I was looking for a nice simple example:</p><p>I used it with <strong>python3.3 </strong>with these changes :</p><p>Change the comport string COMX to a single integer one below the port# you are using , example:</p><p> ser = serial.Serial(COM6, 9600) becomes <strong>ser = serial.Serial(5, 9600)</strong></p><p>and add a b prefix for python3.3 serial write example:</p><p> ser.write( &quot;1&quot;) becomes <strong>ser.write(b&quot;1&quot;)</strong></p><p>Thanks again</p>
<p>Ah ,thanks! b&quot;string&quot; is simpler than bytearray(&quot;string&quot;) and explains to me better why I had to do that change ^_^</p>
<p>Great introduction to communicating between an Arduino and a computer</p>
<p>I just finished a home-study Python programming course (<a href="http://www.ciebookstore.com/python-programming-course" rel="nofollow">http://www.ciebookstore.com/python-programming-cou...</a> ) and this Instructable was an awesome test for my new programming skills. Thanks!</p>
sorry not working
what does <br>Serial.write(&quot;1&quot;); <br> <br>means, exactly that 1.. <br> <br>and how to stop that blink using python.
I love what you did ! Thanks !!
Hello, <br>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!
Hi, <br> <br>Can you post a mac version of the tutorial? <br>that would be nice. Good job on the tutorial though :)
I'm a bit confused. You say your &quot;while&quot; 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 &quot;true&quot; on the first iteration.<br> <br> I believe &quot;serin = ser.read()&quot; is meant to be your trigger here, that if it succeeds it will continue to go onto the next line &quot;connected == &quot;True&quot;&quot;, and skip to the next iteration if it fails.<br> <br> This however shouldn't work. If the device is not connected and you call &quot;serin = ser.read()&quot;, it will throw a &quot;ValueError&quot; exception. Even if this line of code passed fine through the prompt, the next line &quot;connected == &quot;True&quot;&quot; would run regardless of what happened in the previous line.<br> <br> Your proper while loop should look like this:<br> <br> <strong>while not connected:<br> &nbsp;&nbsp;&nbsp; try:<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; serin = ser.read()<br> &nbsp;&nbsp;&nbsp; except ValueError:<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; continue<br> &nbsp;&nbsp;&nbsp; connected = &quot;True&quot;</strong>
I also copied &amp; pasted your code. I get the same error as krithimagic. I'm also a novice at this. <br> <br> <br>File &quot;C:\Python32\lib\site-packages\serial\serialwin32.py&quot;, line 236, in read <br> raise SerialException(&quot;ReadFile failed (%s)&quot; % ctypes.WinError()) <br>serial.serialutil.SerialException: ReadFile failed ([Error 6] The handle is invalid.) <br> <br> <br>I am running Python 3.2 on Windows 7. <br> <br>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. <br> <br>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. <br> <br>I'd appreciated any help or advice you might be able to provide.
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.
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 <br><br>while not connected:<br> serin = ser.read()<br> connected = True<br><br>that's when I get an error:<br><br><br>Traceback (most recent call last):<br> File &quot;&quot;, line 2, in <br> serin = ser.read()<br> File &quot;C:\Python27\lib\site-packages\serial\serialwin32.py&quot;, line 236, in read<br> raise SerialException(&quot;ReadFile failed (%s)&quot; % ctypes.WinError())<br>SerialException: ReadFile failed ([Error 6] The handle is invalid.)<br><br>Please help me out!
Sorry for the late reply! <br>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. <br> <br>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. <br> <br>Check out the documentation for Python and PySerial on their website and mess around with the code a little.

About This Instructable




More by Bobbert72:Arduino and Python Heart-Shaped Chocolate holder 
Add instructable to: