Project tutorial
Python3 and Arduino Communication

Python3 and Arduino Communication © GPL3+

Easily send commands from Python3 versions to Arduino.

  • 1,159 views
  • 0 comments
  • 6 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

Author

Jalal mansoori
Jalal_Mansoori
  • 4 projects
  • 16 followers

Additional contributors

  • Step by step tutorial of communication between python2 version and arduino. by Aswinth Raj

Published on

January 23, 2019

Members who respect this project

ElazafranErospeter

and 4 others

See similar projects
you might like

Similar projects you might like

Morse Code Communication Using Arduino

Project tutorial by Jalal_Mansoori

  • 3,129 views
  • 7 comments
  • 20 respects

Low Power RF Beacon for Remote Sensing & Communication

Project tutorial by Shahariar

  • 2,052 views
  • 1 comment
  • 16 respects

Morse Code Communication Using Laser Module (Both)

Project tutorial by SURYATEJA

  • 896 views
  • 1 comment
  • 9 respects

Arduino I2C communication with Raspi 2 WIOT

Project tutorial by Christiano Faig

  • 35,820 views
  • 25 comments
  • 113 respects

Mobile Arduino Communication BLE device

by dfornaciari

  • 5,066 views
  • 1 comment
  • 9 respects
Add projectSign up / Login