Skip to content

Instantly share code, notes, and snippets.

@matdombrock
Last active July 14, 2022 22:23
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save matdombrock/fb4833cb3b288a3530eb9a03e3550ecf to your computer and use it in GitHub Desktop.
Save matdombrock/fb4833cb3b288a3530eb9a03e3550ecf to your computer and use it in GitHub Desktop.
Simple Arduino Serial Monitor - Python
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(7, INPUT);
pinMode(6, INPUT);
pinMode(5, INPUT);
pinMode(4, INPUT);
pinMode(3, INPUT);
pinMode(2, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print(digitalRead(7));
Serial.print("/");
Serial.print(digitalRead(6));
Serial.print("/");
Serial.print(digitalRead(5));
Serial.print("/");
Serial.print(digitalRead(4));
Serial.print("/");
Serial.print(digitalRead(3));
Serial.print("/");
Serial.print(digitalRead(2));
Serial.print("/");
Serial.print(analogRead(5));
Serial.print("/");
Serial.print(analogRead(4));
Serial.print("/");
Serial.print(analogRead(3));
Serial.print("/");
Serial.print(analogRead(2));
Serial.print("/");
Serial.print(analogRead(1));
Serial.print("/");
Serial.print(analogRead(0));
Serial.print("/");
Serial.println(" ");
}
import serial
from time import sleep
import sys
COM = 'COM3'# /dev/ttyACM0 (Linux)
BAUD = 9600
ser = serial.Serial(COM, BAUD, timeout = .1)
print('Waiting for device');
sleep(3)
print(ser.name)
#check args
if("-m" in sys.argv or "--monitor" in sys.argv):
monitor = True
else:
monitor= False
while True:
val = str(ser.readline().decode().strip('\r\n'))#Capture serial output as a decoded string
valA = val.split("/")
#print()
if(monitor == True):
print(val, end="\r", flush=True)

what's this??

A simple set of scripts for getting output from an Arduino to act is input for Python.

how to use?

  • Install python3
  • Install pyserial
  • adjust monitor.py to include your COM location as the COM variable (ie COM3 or /dev/ttyACM0)
  • run with python monitor.py --monitor (You may have to run as root on linux)

what are these numbers?

1/0/1/1/0/0/277/0/0/0/0/0/

D7 / D6 / D5 / D4 / D3 / D2 / A5 / A4 / A3 / A2 / A1 / A0 /

valA[0] / valA[1] / valA[2] / valA[3] / valA[4] / valA[5] / valA[6] / valA[7] / valA[8] / valA[9] / valA[10] / valA[11] /

  • The First 6 numbers in this sequence represent the digital input pins 7-2 (in reverse numerical order). So the first number is port 7, the second number is port 6 and so on. These are always 1 or 0; on or off.
  • The final six parts of the sequence represent the values of analog pins 5-0 (in reverse numerical order). These will be a number between 0 and 1023.
  • Values are converted to a string so they can be easily parsed. By refernceing valS[0] you would get the value of digital input pin 7. By referencing valS[7] you will get the value of analog input pin 4

Monitor.ino could be a lot better by using a for loop inside of loop()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment