Connect Arduino With Python


In my previous article, I explained about Arduino Uno and Arduino Mega; i.e., how to connect and display in the serial monitor. In this article, it is the next level of Arduino. If you are familiar with my previous article, it is very easy to learn Python with Arduino. In Arduino, it is a very simple output with the user in the serial monitor. In  Python, it will be displayed in the new world.
Software Requirement
  • Arduino IDE
  • Python 2.7.8 or Python 2.7.11 or 3.2
Install the python Software
Download the link given below the line or copy the link.
Click the software and a new Window will open.
In this Window, click the run option to install the software.
Click the next button to install for the user.
Click the next button.
In this Window, the files are copying and when the files are copied, click next.
Click finish. Python has been installed successfully.
Now, we have seen how Python is installed. In the next article, I will explain about connecting Arduino and Python in a single coding and the output will be displayed in Python's new world.
Let us start by combining Arduino with Python,
  • First, we can start with a simple program by blinking the LED in the serial monitor.
  • Tak the  LED and connect with the pin. The positive pin is to be connected with 13 and the negative pin is to be connected to Gnd.
    Program in Arduino IDE
    1. int led = 13;  
    2. void setup()  
    3. {  
    4.     Serial.begin(9600);  
    5.     pinMode(led, OUTPUT);  
    6. }  
    7. void loop()  
    8. {  
    9.     char leitura =;  
    10.     if (leitura == '1')  
    11.     {  
    12.         digitalWrite(led, HIGH);  
    13.     }  
    14.     else if (leitura == '0')  
    15.     {  
    16.         digitalWrite(led, LOW);  
    17.     }  
    18. }  
  • Upload the program.
  • This is to be done on the Arduino side and then we have to write the Python program in the IDLE (Python GUI).
Python Program
  1. import serial     
  3. porta= "COM6"     
  4. velocidade = 9600     
  5. conexao = serial.Serial(porta, velocidade);     
  7. opcao = 0;     
  9. while opcao != "2":     
  10.       opcao = raw_input("Digita1 para Ligar\nDigital 0 para Desligar\nDigital 2 para sair!\n")     
  11. if opcao != "2":     
  12.       conexao.write(opcao);     
  13.       conexao.close()    
    Write the Python program and click the run button to view the output.
    • In Python, the output will be displayed in the new world. It is very simple to connect Arduino and Python. In Python, we have to give the port of the Arduino board in Python. Afterward, it will be connected.
    • In the run Window, type 1  and the LED will switch ON.
    • In the run Window type 0 and the LED will switch OFF.