In this tutorial, we are going to learn how we can install python on our computer and how to use it with Arduino, it allows us to send data between a computer though Arduino's serial.
Step 1: Install Python on Your ComputerYou can skip this step if you have installed the Python IDLE already in your computer.
1. Go to the python website and download it (here).
2. Once you have done downloading, you can move on to installation by keeping the directory in which the python is getting installed by default.
Step 2: Install PySerialPySerial is a Python API module which is used to read and write serial data to Arduino or any other Microcontroller. To install on Windows, simply visit PySerial's Download Page and following the steps bellow :
1. Download the PySerial from the link above or Open CMD and type
pip install pyserial
2. Install it by keeping the setting as the default. You should be sure that Pyserial worked correctly, To check this You can open IDLE and type in
import serial
If you are not getting any error, it means you installed it correct, else you can check your installation.
Step 3: Python CodeFirst up, we need a simple program to get the Python sending data over the serial port.
# Importing Libraries
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: ") # Taking input from user
value = write_read(num)
print(value) # printing the value
Step 4: Arduino CodeTo initiate a connection with the Arduino from Python, we first have to figure out which COM Port the Arduino is on. We can simply see in which port our Arduino is on.
int x;
void setup() {
Serial.begin(115200);
Serial.setTimeout(1);
}
void loop() {
while (!Serial.available());
x = Serial.readString().toInt();
Serial.print(x + 1);
}
Applications:This project finds its application in debugging any embedded systems, communication to any household or any industrial devices. A robotics enthusiast or hobbyist is expected to implement these to their projects with ease. For them, I would suggest our upcoming course on robotics by Dr. Arun Dayal Udai on his Youtube channel. WATCH THIS!
Comments