Reading multiples i2c sensor and grab datas regularly

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Reading multiples i2c sensor and grab datas regularly

Post by zaord » Wed Dec 29, 2021 7:23 pm

Hi here !

I have 2 i2c sensors one flowmeter and one bmp280 (temp / pressure sensor).
My fondamental question is I wants to grab my datas on micropython array, encode it with json fromat and send it to serial protocol to a PC Cpython script which will collect various datas.

When I will do my measurements in micropython, I wants to plot my data and compare them versus time.

In my idea, when beetwen the time I will get the value of the first sensor, and the secon a certain amount of time will pass because the measurement process is not instantaneous. So should I feed two array with the value of the sensor and the timestamp of the measurement for each of the different sensor ?

Because, if I have well understood, I think the value stands into the sensor memory but the clocks of the differents sensors are not sync, so I suppose that I need to fix the same sampling rate for both sensor, but I don't know to snyc the time.

Cheers, Ewen

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Reading multiples i2c sensor and grab datas regularly

Post by davef » Wed Dec 29, 2021 7:46 pm

Interesting problem ...

1) how often do you want to take samples?
2) how fast do each of the sensors respond to multiple "take a reading requests"?
3) how close to each other do the two samples need to be taken?

For example (2) 1-wire DS18b20 normal conversion period takes about 750ms.

zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Re: Reading multiples i2c sensor and grab datas regularly

Post by zaord » Wed Dec 29, 2021 8:57 pm

davef wrote:
Wed Dec 29, 2021 7:46 pm
Interesting problem ...

1) how often do you want to take samples ?
2) how fast do each of the sensors respond to multiple "take a reading requests" ?
3) how close to each other do the two samples need to be taken ?

For example (2) 1-wire DS18b20 normal conversion period takes about 750ms.
1) I needs maybe 150 Samples / s (150 Hz) for each sensors. This is not critical because it's fluid dynamics measurements for musical acoustics and those processes are slow.

2) The max sampling frequency of the sensor is 200 Hz

3) It's not really the question. I need to be sure that the time interval beetwen two sample really steady and the samples on the two sensors taken on the same sampling frequency, but I could also use a propoer python script to resample my signal , because i could afterwards delay it of make data analysis and resync them.

zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Re: Reading multiples i2c sensor and grab datas regularly

Post by zaord » Wed Dec 29, 2021 9:04 pm

I plan to reuse this code founded here :

Code: Select all

import time
from micropython import const
from machine import Pin, I2C

def read_samples(self, acquisition_time):
    # set up
    n_exp_meas = floor(acquisition_time * ceil(self.sampling_rate))
    buf_sens1 = bytearray(6 * n_exp_meas)
    buf_sens2 = bytearray(6 * n_exp_meas)
    m1 = memoryview(buf_sens1 )
    m2 = memoryview(buf_sens2)
    n_act_meas = 0
    
    # read bytes as fast as possible
    start = time.ticks_us()
    while time.ticks_diff(time.ticks_us(), start) < acquisition_time * 1000000:
      curr_time = time.ticks_us()
      if time.ticks_diff(curr_time, start) < (n_act_meas * 999999. / self.sampling_rate):
        continue
      # read sensor 1
      self.i2c.readfrom_mem_into(self.address, self.regAddress, m1[n_act_meas * 6:n_act_meas * 6 + 6])
      # grab the time of the measure 1
      time_sensor1[n_act_meas ]= curr_time  
      # read sensor 1
      self.i2c.readfrom_mem_into(self.address, self.regAddress, m2[n_act_meas * 6:n_act_meas * 6 + 6]) 
      # grab the time of the measure 2
      time_sensor2[n_act_meas ]= curr_time 
      
      n_act_meas += 1

    # bytes --> lists of integers
    acc_x, acc_y, acc_z = zip(*[ustruct.unpack('<HHH', buf[i:i+6]) for i in range(len(buf)) if i % 6 == 0])
    del buf, m
    gc.collect()
    
    # remove exceeding zeros
    acc_x = acc_x[:n_act_meas]  
    acc_y = acc_y[:n_act_meas]
    acc_z = acc_z[:n_act_meas]
    
    print("measured samples: ", n_act_meas)
    print("expected samples: ", n_exp_meas)
    print("actual sampling rate: ", n_act_meas / acquisition_time)
    
    return acc_x, acc_y, acc_z

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Reading multiples i2c sensor and grab datas regularly

Post by davef » Wed Dec 29, 2021 9:17 pm

3) what timing resolution or allowable offset between the samples for the two devices, ie microseconds?
3a) how accurate does the sampling rate have to be?

zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Re: Reading multiples i2c sensor and grab datas regularly

Post by zaord » Wed Dec 29, 2021 9:48 pm

davef wrote:
Wed Dec 29, 2021 9:17 pm
3) what timing resolution or allowable offset between the samples for the two devices, ie microseconds?
1-2 ms is engouh, It is just for a good human interpretation :)

davef wrote:
Wed Dec 29, 2021 9:17 pm
3a) how accurate does the sampling rate have to be?
It's just I will make transitent analysis, so I just need the two signal in phase as much as I can :)

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Reading multiples i2c sensor and grab datas regularly

Post by davef » Wed Dec 29, 2021 10:18 pm

I re-read this:
viewtopic.php?f=18&t=8443
so I am guessing that reading two I2C devices using softI2C could take 500us.

Writing to them, similar, unless he meant write/read? And that doesn't include the processing times to issue the two commands.

zaord
Posts: 96
Joined: Fri Jan 31, 2020 3:56 pm

Re: Reading multiples i2c sensor and grab datas regularly

Post by zaord » Thu Dec 30, 2021 10:34 am

My question is basically, should I need to take the time stamp of a measurement before this measurement or after ?
Last edited by zaord on Thu Dec 30, 2021 9:13 pm, edited 1 time in total.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Reading multiples i2c sensor and grab datas regularly

Post by davef » Thu Dec 30, 2021 8:34 pm

If the write and read times are the same then taking:
1) one time measurement between the two writes,
2) before both writes or
3) after both reads

is still going to give the same error. I am sure it is probably not that easy.

Sorry, I was probably too focused on the issue of getting the two readings in a timely fashion.

Post Reply