Recently I started learning python and was amazed by its simplicity and capabilities. So I thought let’s connect it to Arduino and see what I can do with it. Communication between Arduino and Python isn’t a new concept, There are many modules available for Python that help in the process. In this article, I’m going to use ‘pyserial’ package for communication. For this, you will need a basic understanding of Python and Arduino. I’m using ‘Python 2.7‘ here. So let’s get started…


Video Tutorial:

Video tutorial


Before writing any code first you need to install Arduino IDE & Python 2.7

  1. Install the IDE. visit Arduino’s website at and download the latest IDE compatible to your OS. Then follow the installation process and you are done.
  2. Download and install Python from . The installation is simple enough if you have doubts you can ask in comments or there are many good tutorials online.
  3. Once the Python is up and running. You have to install the ‘pyserial’ module. To do this open your command prompt(CMD) and type in ‘pip install pyserial’ (without the single quotes) hit enter and the installation process should start.

NOTE: You will need active internet connection to use pip.


First open up your Arduino IDE and write the code given below :-

//declaring variables

int LED = 13;      // pin 13 is given variable LED
int data;              // Variable to store data
void setup()
Serial.begin(9600);              //setting baud rate for communication
pinMode(LED,OUTPUT);    //Pin 13 set as output
digitalWrite(LED,LOW);       //LED is off by default
void loop()
while(Serial.available())                                 //check if data is available
data =;                                       //while data is available read the data
if(data == '1')                                                  //if data is value '1'
digitalWrite(LED,HIGH);                                //turn LED on
Serial.println("LED turned on");                     //print output on serial monitor
else if(data == '0')                                          //if data is value '0'
digitalWrite(LED,LOW);                                 //turn LED off
Serial.println("LED turned off");                     //print output on serial monitor

Compile the code and upload it to the Arduino. Next open Serial monitor goto>>tools>>serial monitor.

Enter ‘1’ to turn LED ‘on’ and ‘0’ to turn it ‘off’.

Python Script:

To write Python script open python IDLE Goto>> Files >> New file. Write the code given bellow:

o write Python script open python IDLE Goto>> Files >> New file.

Write the code given bellow:

import serial                                                                                #imports the pyserial module
import time                                                                                  #imports time module required for delays (sleep functions)
arduino = serial.Serial('COM3', 9600)                                         #Creates arduino object and establishes connection to port (Enter your port)
time.sleep(2)                                                                               #waits for connection to establish
print arduino.readline()
print ("Enter '1' to turn 'on' the LED and '0' to turn LED 'off'")      #asks for input
while 1:                                                                                        
   #while data is available
   var = raw_input()                                                                         
   #accepts input puts it in 
   variable 'var'
   print "You Entered :", var                                                             
   #prints input
   if(var == '1'):                                                                                
      #if input is 1
      #sends '1' to arduino
      print("LED turned on")
   if(var == '0'):                                                                                 
      #if var is 0 
      #sends '0' to arduino
      print("LED turned off")

After you have the code. Save it and goto Run or Hit ‘F5’ key to execute the code.

You will see a screen asking for input. just Enter 1/0 to turn LED on/off.

If the LED does not respond for the first time just close the IDLE and unplug Arduino and plug it again.


Here I have provided the code files so you can just download and use it.

LED.ino and the ino file is for Arduino IDE and py file is for python IDLE.

Hope this tutorial is useful to you. You can try many things with it. If you have any questions you can leave a comment.

0 0 votes
Article Rating