Project tutorial
Controlling an Arduino Using Python Shell

Controlling an Arduino Using Python Shell © GPL3+

One of the many ways to tinker with your board!

  • 8 respects

Components and supplies

Apps and online services

About this project

Posted on September 20, 2017 by electromaniaweb

The most effective and easy to learn programming language in today’s world is python. With millions of applications and the most user friendly interface we can almost do about anything with it.

Here I have tried to club  Arduino environment with that of a python user.

First, you will need to download python on your PC to continue. Follow the following steps   :-

 The steps mentioned below are applicable only for windows users running either 32-bit or 64-bit OS. The installation procedure for MAC and Linux is different.

  • Click on 32-bit Python-2.7.9 and this will install the 32-bit Python IDLE on your Computer. Do not download the 64-bit version or updated versions since they do not provide support for our Arduino Libraries. Even if your Computer is operating on 64-bit you can use 32-bit Python itself.
  • Open the downloaded exe file and follow through the instruction. Do not change the directory in which the python is getting installed. It will be C:\Python27 by default and leave it as such.
  • While the installation takes place you might get a warning from your anti-virus (if any) in that case click on allow.

Now you will need to import the Pyserial module in your IDLE.

PySerial is a Python API module which is used to read and write serial data to Arduino or any other Microcontroller.

Click on Pyserial Windows to download PySerial. The resulting download will be a exe file which can be directly installed. Do not change any setting while installing. Leave it to the default directory and default settings.

Now, let us check if PySerial is installed properly. To do this, open Python Shell again and type in

import serialIf the library was successfully installed then you should not get any error messages as shown in the picture below. If you get any errors post them on the comment section and we will try resolving it.

Inside the setup function we initialize the serial communication at 9600 baud rate and declare that we will be using the built in led as output and turn it low during program start. We have also sent a welcome message to python via serial print as shown in code "Py12":

Inside the loop function,  we read whatever the data that is coming in serially and assigning the value to the variable “data”. Now based on the value of this variable (“data”) we toggle the built in led as shown in code "Py23"

The above mentioned pieces of code will be written in the Arduino IDE. The above mentioned code must be written in the python idle for windows. Next you simply have to upload the code named "Ard123" onto your python shell for controlling the on-board LED on your arduino.

For any queries on understanding the code or any step seeming unclear, drop a comment.


import serial #Serial imported for Serial communication
import time #Required to use delay functions

ArduinoSerial = serial.Serial(com18,9600) #Create Serial port object called arduinoSerialData
time.sleep(2) #wait for 2 secounds for the communication to get established

print ArduinoSerial.readline() #read the serial data and print it as line
print (Enter 1 to turn ON LED and 0 to turn OFF LED)
while 1: #Do this forever

var = raw_input() #get input from user
print you entered, var #print the intput for confirmation

if (var == 1): #if the value is 1
ArduinoSerial.write(1) #send 1
print (LED turned ON)

if (var == 0): #if the value is 0
ArduinoSerial.write(0) #send 0
print (LED turned OFF)
void loop()
while (Serial.available())
data =;
if (data == ‘1’)
digitalWrite (LED_BUILTIN, HIGH);
else if (data == ‘0’)
digitalWrite (LED_BUILTIN, LOW);
void setup()
Serial.begin(9600); //initialize serial COM at 9600 baudrate
pinMode(LED_BUILTIN, OUTPUT); //make the LED pin (13) as output
digitalWrite (LED_BUILTIN, LOW);
Serial.println(“Hi!, I am Arduino”);


Similar projects you might like

Controlling LED Brightness Using Bolt and Arduino

Project tutorial by Devashish Gupta

  • 13 respects

Aquarium Controlling and Monitoring Using Arduino + 1Sheeld

Project tutorial by Khaled Mohamed

  • 1 comment
  • 15 respects

Controlling an RGB LED with Color Detector Camera

Project tutorial by Zowail

  • 1 respect

Controlling an LED Matrix with Arduino Uno

Project tutorial by Igor Fonseca Albuquerque

  • 28 respects

Controlling an Arduino Turret with IR Remote

Project tutorial by RobotGeek Projects Team

  • 1 comment
  • 17 respects
Add projectSign up / Login