Automation of Sending data from Arduino to Python

Hello I am trying to make a device that sends data From Arduino to Python. I am having trouble with automating the process as of the moment this is the code that I use for the Arduino

int EMGPin = A1;
int EMGVal = 0;

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

void loop() {
EMGVal = analogRead(EMGPin);
Serial.println(EMGVal);
}

this sends data from an EMG module to the Arduino and prints out the output on a Serial Monitor the Data that I get is int values ranging from 0 to 1023. with this I manually collect the data using Coolterm save it as TXT then convert it to a CSV file then convert that CSV file into a PNG which I will be using in python. what I need is to automate the process of data collection and send it to python as either a CSV file or a PNG file. if anyone could help me out that would be great or if anyone knows an alternative to Coolterm that could communicate with python directly that would be great.

You may want to have a look at the pySerial library.

just curious why you want a PNG (image format??) in your python program and not the raw data


regarding your code, the way it's written, depending on how you saturate the output buffer you won't get a totally uniform sampling rate. Do you care ? may be using a small piece of code with milis() will let you choose more precisely how often you take a sample and spit it out.

How does the Arduino program know when to stop collecting and sending data? does it matter (a max sample count?) or it's for the python code to decide and stop reading what's coming?

remmener that if you connect a python script to the Serial port of the arduino and open that port, the Arduino will reboot - which takes a bit of time.

hello thank you for replying
Its for a CNN project I need the data to be an Image format

as for the uniform sampling rate as long as every test is the same format. like there wont be a difference in the result due to the device if I test it on one and another person there would only be a difference in the human factor then it should be alright

the arduino program will stop collecting data after a set amount of time which I have not implemented yet I just manually collect the data then just turn of the device to stop it but it would most likely be implemented on the arduino not on the python code.

thank you for that I think I'm going to add a delay on the python code before collecting data from the arduino

My experience is that I found best to use tasks in python to listen to the Serial port, I gave an example in this post .

Basically you have a task listening and filling up a queue with each received line and another task (the main one) « unstacking » the queue and processing the lines.
You can also have a simple ACK process for the data if you want

Maybe you’ll find that code useful

PS: instead of a random delay to wait for the arduino to boot up, have the arduino send a known code when it’s ready and wait for that signal to start the processing. It will be more robust

1 Like

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