Menu Close

Python with Arduino – LED Blinking

This tutorial of Robo India explains, how to use python code with Arduino to control LED blinking.

1. Introduction:

Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. In this tutorial, we have explained how to install python in computer and how to use python code with arduino using basic example of LED blinking.

1.1 Software Required:

1. Python: The first thing we need to install in our computer is python. You may download python (32 bit) from here.

Please note that 64-bit version or updated versions of python are not provide support for our Arduino Libraries. Even if your Computer is 64-bit machine. And this installation is applicable only for windows 32-bit or 64-bit OS.

After downloading, open the .exe file and follow the instruction. Do not change the directory in which the python is getting installed. It will be C:\Python27 by default.

2. PySerial: pyserial is pyhton API module to read and write serial data. You may download pyserial from here.

After downloading, open the .exe file and follow the instruction as well.  Do not change the directory.

3. Arduino IDE:By which you are familier.

1.2 Hardware Required:

For controlling inbuilt arduino led, we only required one hardware.

S.No.ItemQuantity
1Arduino 1

2. Programming:

Arduino Program

You may download this code (Arduino Sketch) from here.

int data;

void setup() 
{ 
  Serial.begin(9600); 
  pinMode(LED_BUILTIN, OUTPUT); 
  digitalWrite (LED_BUILTIN, LOW); //initially set to low
  Serial.println("This is my First Example.");
}
 
void loop() 
{
while (Serial.available())
  {
    data = Serial.read();
  }

  if (data == '1')
  digitalWrite (LED_BUILTIN, HIGH);

  else if (data == '0')
  digitalWrite (LED_BUILTIN, LOW);

}

Python Program

You may download this Python code from here.

import serial #for Serial communication
import time   #for delay functions
 
arduino = serial.Serial('com4',9600) #Create Serial port object called arduinoSerialData
time.sleep(2) #wait for 2 secounds for the communication to get established

print arduino.readline() #read the serial data and print it as line
print ("Enter 1 to get LED ON & 0 to get OFF")

 
while 1:      #Do this in loop

    var = raw_input() #get input from user
   
    
    if (var == '1'): #if the value is 1
        arduino.write('1') #send 1
        print ("LED turned ON")
        time.sleep(1)
    
    if (var == '0'): #if the value is 0
        arduino.write('0') #send 0
        print ("LED turned OFF")
        time.sleep(1)

Python Program

You may download this Python code from here.

import serial #for Serial communication
import time   #for delay functions
 
arduino = serial.Serial('com4',9600) #Create Serial port object called arduinoSerialData
time.sleep(2) #wait for 2 secounds for the communication to get established

print arduino.readline() #read the serial data and print it as line
print ("Enter 1 to get LED ON & 0 to get OFF")

 
while 1:      #Do this in loop

    var = raw_input() #get input from user
   
    
    if (var == '1'): #if the value is 1
        arduino.write('1') #send 1
        print ("LED turned ON")
        time.sleep(1)
    
    if (var == '0'): #if the value is 0
        arduino.write('0') #send 0
        print ("LED turned OFF")
        time.sleep(1)

3. How to write and upload python code:

Search for “IDLE (Python GUI)” in window’s search box and then open it. The screen which is open is python shell.

Click on the File menu and open the new window. Copy and paste the above written python code or download it.

Save your file with any name. It will automatically saved with .py extension.

Run the code, by clicking on run menu or by using F5 key.

Before uploading, Make sure to write the correct port in the code.

4. Output:

After uploading the Arduino code, open the Serial Monitor, Select baud rate to 9600. When you send 1, Arduino’s onboard led will get ON and if you send 0 , it will get OFF.

And after uploading the python code, a new output window will automatically appear. Same as above, when you send 1 , led will get ON and OFF when you send 0.

If you have any query please write us at support@roboindia.com

Thanks and Regards
Content Development Team 
Robo India
https://roboindia.com

5 Comments

Leave a Reply