Project tutorial
Python3 and Arduino Communication

Python3 and Arduino Communication © GPL3+

Easily send commands from Python3 versions to Arduino.

  • 20,607 views
  • 6 comments
  • 20 respects

Components and supplies

About this project

Project Description

In this project we will be sending commands from Python3 to an Arduino board, which will make things easier to understand when communicating between Python3 and Arduino. We will make a "Hello world" of the Arduino platform which means turning ON/OFF built-in LED on the Arduino Uno.

So first of all let me tell you...

Why I Made This Project

Firstly, there are many tutorials uploaded on internet specially on youtube about this topic but they are using Python2 versions, and secondly, I had installed latest version which is Python3.7.2. There is a little bit of difference between Python2 and Python3 when you are using it to connect with Arduino. So after I solved the problem of sending command from Python3 to Arduino, I thought this should be shared to makers and the entire hobbyist community.

Let's start follow below steps:

Installation Procedure

  • How to install python3 version and PySerial package

Now you can search on YouTube about installation stuff. Below is video for installing both Python3 version and PySerial package.

Demos

  • Part 1 : Arduino Code
Coding in arduino platform
  • Part 2: Python Code
Coding in python3 platform

Note

Make sure that first you upload Arduino sketch and then Python code. :)

Let me know on comment section if you are having any issue while making this project.

Code

Arduino CodeArduino
This code is for arduino and you need to upload this code before running python code
int datafromUser=0;
void setup() {
  // put your setup code here, to run once:
  pinMode( LED_BUILTIN , OUTPUT );
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available() > 0)
  {
    datafromUser=Serial.read();
  }

  if(datafromUser == '1')
  {
    digitalWrite( LED_BUILTIN , HIGH );
  }
  else if(datafromUser == '0')
  {
    digitalWrite( LED_BUILTIN, LOW);
  }
  
}
Python3 CodePython
import serial
import time

arduino=serial.Serial('COM1', 9600)
time.sleep(2)

print("Enter 1 to turn ON LED and 0 to turn OFF LED")

while 1:
    
    datafromUser=input()

    if datafromUser == '1':
        arduino.write(b'1')
        print("LED  turned ON")
    elif datafromUser == '0':
        arduino.write(b'0')
        print("LED turned OFF")

Schematics

arduino_uno_nK1sUVJ7D8.jpg
For this project you just need arduino board and usb cable
Arduino uno nk1suvj7d8

Comments

Similar projects you might like

Morse Code Communication Using Arduino

Project tutorial by Jalal Mansoori

  • 36,793 views
  • 22 comments
  • 55 respects

Play Dinosaur Game Using Arduino and Python3

Project tutorial by Jalal Mansoori

  • 4,233 views
  • 0 comments
  • 12 respects

Low Power RF Beacon for Remote Sensing & Communication

Project tutorial by Shahariar

  • 11,629 views
  • 1 comment
  • 20 respects

Arduino To Excel Communication

by Mahamudul Karim Khondaker

  • 41,806 views
  • 15 comments
  • 67 respects

Morse Code Communication Using Laser Module (Both)

Project tutorial by SURYATEJA

  • 7,036 views
  • 6 comments
  • 16 respects
Add projectSign up / Login