Is it possible to start/stop a script from running using Python?

When I connect my Arduino Uno to my computer via USB, it immediately goes into the setup() and loop() functions as expected. I know I could add a conditional into loop() to control how often it executes, but for my purposes, I would really like to control the arduino with a python script on my computer.

To be specific, I am using an ultrasonic sensor and using pyserial to read in measurements from the serial port, but I would prefer for the arduino to wait until my Python script has finished some tasks before starting to print the values to the serial monitor.

Thanks

If you want the Arduino to respond to requests for data from your PC, read this: Serial Input Basics - updated

Your arduino loop() will continue to do its thing, and then, when it receives some command like 'r' or "read" or whatever, it prints out the data. Depending on what you want, it could also be something like "start" and "stop" which toggles a flag to print data. "start" toggles the flag to true, "stop" toggles the flag to false...

I would recommend single letter commands since reading a single char is easier than dealing with char arrays, especially if you are new to this.

1 Like

Thanks!

And there is this python complement: Demo of PC-Arduino comms using Python

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.