Slow reads of bmp388 and lis3dh on rp2040

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
jeecrombie
 
Posts: 19
Joined: Sat Mar 23, 2019 6:00 pm

Slow reads of bmp388 and lis3dh on rp2040

Post by jeecrombie »

Hello,
I am working on a data logger, reading data from bmp388 and lis3dh, both boards from Adafruit. The processor board is a Pimoroni Tiny 2040.
In a while loop I am reading the xyz and temp values. I took out the sleep statement from the example code, but the program still seems to only be logging the data about once per second. I need it to read much more frequently. When I ran similar arduino code on the M0 adalogger, it captured data much more frequently.
Is there a delay in the library code somewhere? Or can circuit python only read the sensors once a second? For my application, I need to capture data in millisecond time.
Thanks in advance.
George

User avatar
danhalbert
 
Posts: 4613
Joined: Tue Aug 08, 2017 12:37 pm

Re: Slow reads of bmp388 and lis3dh on rp2040

Post by danhalbert »

Could you post the code you are using? Copy it from your board.

User avatar
jeecrombie
 
Posts: 19
Joined: Sat Mar 23, 2019 6:00 pm

Re: Slow reads of bmp388 and lis3dh on rp2040

Post by jeecrombie »

Code: Select all

import time
import board
import busio
import adafruit_lis3dh
import adafruit_bmp3xx
import digitalio
import microcontroller
import supervisor

i2c = busio.I2C(board.GP1, board.GP0)

bmp = adafruit_bmp3xx.BMP3XX_I2C(i2c)
# address seems to change from 18 to 19 randomly
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19)

rled = digitalio.DigitalInOut(board.LED_R)
rled.switch_to_output()
rled.value=1
gled = digitalio.DigitalInOut(board.LED_G)
gled.switch_to_output()
gled.value=1
bled = digitalio.DigitalInOut(board.LED_B)
bled.switch_to_output()
bled.value=1

# Set range of accelerometer (can be RANGE_2_G, RANGE_4_G, RANGE_8_G or RANGE_16_G).
lis3dh.range = adafruit_lis3dh.RANGE_2_G

# Write headings to the output data file
# blink the led if there was a problem
try:
    with open("/temperature.txt", "w") as datalog:
        datalog.write("T,X,Y,Z,Pressure,Temperature \n")
        datalog.flush()
        datalog.close()
        start_time=supervisor.ticks_ms()
except OSError as e:  # Typically when the filesystem isn't writeable...
    delay = 0.5  # ...blink the LED every half second.
    if e.args[0] == 28:  # If the filesystem is full...
        delay = 0.25  # ...blink the LED faster!
    while True:
        rled.value = not rled.value
        time.sleep(delay)        
        
# Loop forever printing accelerometer and barometer values
try:
    with open("/temperature.txt", "a") as datalog:
        while True:
    # Read accelerometer values (in m / s ^ 2).  Returns a 3-tuple of x, y,
    # z axis values.  Divide them by 9.806 to convert to Gs.
            x, y, z = [
                value / adafruit_lis3dh.STANDARD_GRAVITY for value in lis3dh.acceleration
            ]
 #           print("x = %0.3f G, y = %0.3f G, z = %0.3f G" % (x, y, z))
    # Small delay to keep things responsive but give time for interrupt processing.
#            print(
#                "Pressure: {:6.4f}  Temperature: {:5.2f}".format(bmp.pressure, bmp.temperature)
#            )
            current_time = supervisor.ticks_ms() - start_time
            datalog.write("%0i,%0.3f,%0.3f,%0.3f," % (current_time, x, y, z))
#            datalog.write("\n")
            datalog.flush()
            datalog.write("{:6.2f},{:5.2f}".format(bmp.pressure, bmp.temperature))
            datalog.flush()
            datalog.write("\n")
            datalog.flush()
#               time.sleep(1)
except OSError as e:  # Typically when the filesystem isn't writeable...
    delay = 0.5  # ...blink the LED every half second.
    if e.args[0] == 28:  # If the filesystem is full...
        delay = 0.25  # ...blink the LED faster!
    while True:
        rled.value = not rled.value
        time.sleep(delay)

User avatar
jeecrombie
 
Posts: 19
Joined: Sat Mar 23, 2019 6:00 pm

Re: Slow reads of bmp388 and lis3dh on rp2040

Post by jeecrombie »

Just a couple of notes:
the pimoroni board has a three color led, which works backwards from normal LEDs. So you see I broke it out. I use the blinking red to indicate that there is an error.
The only sleep is when I am blinking the led.
I do flushes because they were in the original code example.
Time seems to be pretty consistent for sampling interval.

User avatar
jeecrombie
 
Posts: 19
Joined: Sat Mar 23, 2019 6:00 pm

Re: Slow reads of bmp388 and lis3dh on rp2040

Post by jeecrombie »

Sample output:

T,X,Y,Z,Pressure,Temperature
6,0.051,-0.106,1.014,1017.82,18.51
769,0.048,-0.113,0.992,1017.85,18.52
1530,0.044,-0.113,0.996,1017.90,18.52
2286,0.048,-0.108,1.011,1017.84,18.53
3047,0.047,-0.110,0.974,1017.76,18.52

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”