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:
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 += 1Any help would be welcome !
