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:
And here the python one:
Any help would be welcome !
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#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 } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
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 |