- Published on
How to Send Commands to an Arduino from a Python Script
- Authors
- Name
- Tinker Assist
If you are looking to transmit data from your computer or similar device to your Arduino to command it, this is the perfect tutorial. We will walk through how to set up both devices to do just that.
Python Script
We will start by writing our python script to transmit data over a user-selected COM port. As usual, we want to make sure we have all of our script dependencies installed prior to building out our script. We know that to access our device's COM ports, we will need to install pyserial from the command line.
>> pip install pyserial
Now that we have all of our dependencies taken care of, we can go ahead and write our script.
Lines 1-21 of our Python script is described in our article covering Serial port reading. You can navigate there to see it explained.
Line 23 on, however, is unique as we are looking to transmit user's command line input to our Arduino. This looks as follows:
while True:
command = input("Arduino Command: (ON/OFF): ")
serialInst.write(command.encode('utf-8'))
if command == 'exit':
exit()
Arduino Code
Now that we have covered how to transmit data from our PC, we can write our Arduino Code that looks for the python commands and processes it.
We will start by defining some pins to reflect the state of our Arduino
#define LED_pin 2
#define LED_error_pin 3
In our setup function, we need to initialize the serial port with the same baud rate as our python script, and set our pins to be digital outputs.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LED_pin, OUTPUT);
pinMode(LED_error_pin, OUTPUT);
}
In our loop function, we simply want to read the serial port for any incoming data from our PC, and to flip our LEDs on/off depending on the command.
If we read "ON" or "OFF" in a packet of data transmitted from the PC, we want to set our LED pin output to the commanded state. If we don't recognize the data packet from our PC as one of those commands, we will quickly flash our error LED.
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0){
String msg = Serial.readString();
if (msg == "ON"){
digitalWrite(LED_pin, HIGH);
}
else if (msg == "OFF"){
digitalWrite(LED_pin,LOW);
}
else{
digitalWrite(LED_error_pin, HIGH);
delay(100);
digitalWrite(LED_error_pin, LOW);
}
}
}
Now, we can upload our Arduino code to our Arduino, after which we can execute our python script, set our COM port to our Arduino port (likely the same port over which we programmed the Arduino) and command our Arduino's LEDs!
Full Python Script and Arduino Code
Python Script
import serial.tools.list_ports
ports = serial.tools.list_ports.comports()
serialInst = serial.Serial()
portsList = []
for onePort in ports:
portsList.append(str(onePort))
print(str(onePort))
val = input("Select Port: COM")
for x in range(0, len(portsList)):
if portsList[x].startswith("COM" + str(val)):
portVar = "COM" + str(val)
print(portVar)
serialInst.baudrate = 9600
serialInst.port = portVar
serialInst.open()
while True:
command = input("Arduino Command: (ON/OFF): ")
serialInst.write(command.encode('utf-8'))
if command == 'exit':
exit()
Arduino Code
#define LED_pin 2
#define LED_error_pin 3
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LED_pin, OUTPUT);
pinMode(LED_error_pin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0){
String msg = Serial.readString();
if (msg == "ON"){
digitalWrite(LED_pin, HIGH);
}
else if (msg == "OFF"){
digitalWrite(LED_pin,LOW);
}
else{
digitalWrite(LED_error_pin, HIGH);
delay(100);
digitalWrite(LED_error_pin, LOW);
}
}
}