ansh2919
Published © GPL3+

Serial Communication between Python and Arduino

Use Python to communicate between Arduino.

BeginnerFull instructions provided229,828
Serial Communication between Python and Arduino

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1

Software apps and online services

Arduino IDE
Arduino IDE
Python IDLE
PySerial Library

Story

Read more

Code

Arduino Code

Arduino
int x;

void setup() {
  Serial.begin(115200);
  Serial.setTimeout(1);
}

void loop() {
  while (!Serial.available());
  x = Serial.readString().toInt();
  Serial.print(x + 1);
}

Python Code

Python
import serial
import time

arduino = serial.Serial(port='COM4', baudrate=115200, timeout=.1)


def write_read(x):
    arduino.write(bytes(x, 'utf-8'))
    time.sleep(0.05)
    data = arduino.readline()
    return data


while True:
    num = input("Enter a number: ")
    value = write_read(num)
    print(value)

Credits

ansh2919

ansh2919

1 project • 7 followers

Comments