Python Forum
time setup for realtime plotting of serial datas at high sampling rate
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
time setup for realtime plotting of serial datas at high sampling rate
#1
Hello,

I connect to the computer an Arduino 33 Nano BLE Sense with a sensor.
I would like to plot the datas with a sampling frequency of 256 Hz (eg 1 point every 3 ms) in real time and I am not sure how I should set up the 3 time parameters I have:
1) the delay in the Arduino program -> I guess it should be 3 ms or no delay
2) the timeout of the serial lfunction in python -> I thought it should be set at 0.003 s but I can't go below 0.1 without this error to occur :
File "c:\Users\lemarqua\Documents\python_arduino\plot IR\mesureIR_v1bis.py", line 62, in <module>
plt.scatter(i, float(data.decode()))
ValueError: could not convert string to float: ''
3) the time indicated in plt.pause() -> I don't really understand why this is needed

Here is my Arduino program:

#include <Wire.h>
#include "MAX30105.h"

MAX30105 particleSensor;

float ir;

void setup() {
  Serial.begin(115200);
  while (!Serial);

  // Initialize sensor
  if (!particleSensor.begin(Wire, I2C_SPEED_FAST)) //Use default I2C port, 400kHz speed
  {
    Serial.println("MAX30105 was not found. Please check wiring/power. ");
    while (1);
  }

    //Setup to sense a nice looking saw tooth on the plotter
  byte ledBrightness = 0x1F; //Options: 0=Off to 255=50mA
  byte sampleAverage = 8; //Options: 1, 2, 4, 8, 16, 32
  byte ledMode = 3; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
  int sampleRate = 100; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
  int pulseWidth = 411; //Options: 69, 118, 215, 411
  int adcRange = 4096; //Options: 2048, 4096, 8192, 16384

  particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange); //Configure sensor with these settings

}

void loop() {
  ir=particleSensor.getIR();
    Serial.println(ir); //Send raw data to plotter

  delay(3); // time in ms
}
And here the python one:
import serial
import matplotlib.pyplot as plt
import numpy as np
plt.ion()
fig=plt.figure()

nbPulse=3
tPause=0.1
tTimeout=0.003
xNumber=nbPulse/0.003/60

i=0
x=list()
y=list()
ser = serial.Serial('COM16',115200)
ser.timeout = tTimeout #specify timeout when using readline()
  
while True:

    print(i)
    data = ser.readline()
    print(data.decode())
    x.append(i)
    y.append(data.decode())

    if i>xNumber:
        plt.xlim(i-xNumber,i)
        x.pop(0)
        y.pop(0)
    plt.scatter(i, float(data.decode()))
    plt.show()
    plt.pause(tPause)  # time in second
    i += 1
Any help would be welcome !
Reply


Messages In This Thread
time setup for realtime plotting of serial datas at high sampling rate - by alice93 - Dec-23-2021, 03:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Mirror Video Image in realtime makingwithheld 1 467 Oct-30-2023, 02:45 PM
Last Post: Larz60+
  Modify an Energy Model to account for year dependent interest rate rather than only t giovanniandrean 0 447 Oct-10-2023, 07:00 AM
Last Post: giovanniandrean
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 4,588 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  Plotting by Time, error mansoorahs 1 763 May-16-2023, 09:46 AM
Last Post: Larz60+
  Non-blocking real-time plotting slow_rider 5 3,750 Jan-07-2023, 09:47 PM
Last Post: woooee
  Python Flask Realtime system printout (console) ffmpeg jttolleson 3 2,995 Apr-18-2022, 06:39 PM
Last Post: jttolleson
  Sampling frohr 7 2,244 Mar-22-2022, 08:21 AM
Last Post: Larz60+
  Plotting A Time Series With Shaded Recession Bars adamszymanski 1 3,196 Jan-24-2021, 09:08 PM
Last Post: nealc
  python realtime parsing logs anna 2 2,907 Jul-05-2020, 06:36 AM
Last Post: anna
  How to Calculate CPU, Disk, Memory and Network utilization rate skvivekanand 1 2,071 Jun-16-2020, 08:53 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020