Getting started with Arduino + Pyhton

Learning Python opens many doors for any newbie. As codeacademy helped me realize my dream to learn Python, I want to use this knowledge to make some embedded stuff. The first think that came in my mind was Arduino. Yes, anyone who is familiar with Arduino & want to apply his/her Python skills then this post may be beneficial for them.

Hardware requirement

Arduino UNO or Arduino Atmega328 will be enough to get started.

Software requirement

Here come little tricky part. I hope Python is already installed on you system (Windows/Linux). Since python will communicate over serial there is an addition package you need to download that is called Pyserial, as the name seems obvious. Windows users can run setup directly, Linux users can too. But Linux users must be searching for command so I may ease their work following these steps:-

1. Open terminal -> Get super user access -> user password

sudo su

2. You can use any one of below two methods

pip install pyserial


easy_install -U pyserial

3. That’s it, you are done with installation part. Now open you Python IDE (windows user) and Linux users (terminal -> idle)


4. Python shell -> file -> New window -> save as -> ->



 5. These two lines will import necessary modules from library.
     Now this is tricky part, how to connect to serial port. Go to device manager (Windows) & find in ports. But for Linux users           there is a command to find out COM port to which Arduino is connected.
dmesg | grep tty

it will show COM port to which device is connected. Now you need to assign a variable in Python IDE for arduino board,             name it Arduino itself. Now you code will look like this.


You should change the COM port, if it is connected to different port.
Note: Be careful with the Baud Rate. The baud rate mentioned in Python IDE should be same as the baud rate chosen for Arduino serial monitor. For example, some Sonar sensor libraries talks with Arduino on 115200 baud rate (Better to take care of it rather than scratch your head for hour when things doesn’t work).
6.  Now finally if you are familiar with Python then you can get whole idea of how code is written & how it is working. Final coding looks like this-
Screenshot from 2013-10-22 13:32:32
7. Finally run the program from    ( run -> run module) or you can press F5 that will prompt to save. Save & view the Python         shell. It will look like this-
But wait, what is going on their? The LED (Pin 13) is not blinking what is going on? Python code seems to be working fine but what is the matter with Arduino board?
Well, well, well here is the matter with the stuff. Python is sending some string ‘H’ and ‘L’ over serial pin but Arduino is not able to understand what to do with these command. Perhaps I need to program Arduino first with instructions that tell it to make LED high when it receive ‘H’ over serial & make LED low for ‘L’ .  Follow my other post to do that. After uploading the program, run your saved Python program & all things will work fine. DONE!!!!!
Source Code
You can directly use these codes into your Python IDE.

import serial
import time
Arduino = serial.Serial(‘/dev/ttyACM0’, 9600)
time.sleep(2) # waiting the initialization…
while True:
Arduino.write(‘H’) # turns LED ON
print(“LED ON”)
time.sleep(2) # waits for 2 secondArduino.write(‘L’) # turns LED OFF
print(“LED OFF”)
time.sleep(2) # waits for 1 s
Arduino.close() #say goodbye to Arduino

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s