L3GD20 (3DOF) bandwidth with CircuitPython

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Thomas_G_S
 
Posts: 29
Joined: Fri Jan 07, 2022 4:31 pm

L3GD20 (3DOF) bandwidth with CircuitPython

Post by Thomas_G_S »

Hello,

I am trying to increase the bandwidth of the L3GD20 3DOF sensor using a Raspberry Pi Pico running CircuitPython, but I can't find the command. I don't need more updates per second, I am trying to increase the communication bandwidth to decrease the gyro latency. Can anyone help me?

Thanks!

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: L3GD20 (3DOF) bandwidth with CircuitPython

Post by mikeysklar »

Are you connecting to it via SPI or I2C?

User avatar
Thomas_G_S
 
Posts: 29
Joined: Fri Jan 07, 2022 4:31 pm

Re: L3GD20 (3DOF) bandwidth with CircuitPython

Post by Thomas_G_S »

I'm using SPI. Would I be correct in saying that SPI will have a lower latency.

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: L3GD20 (3DOF) bandwidth with CircuitPython

Post by mikeysklar »

SPI is the right choice for speed / latency concerns.

The SPI clock can be adjusted. Maybe start with 24MHz and try up to 62.5MHz with a Pico to see what you can measure.

Code: Select all

>>> spi.configure(baudrate=5000000, phase=0, polarity=0)

User avatar
Thomas_G_S
 
Posts: 29
Joined: Fri Jan 07, 2022 4:31 pm

Re: L3GD20 (3DOF) bandwidth with CircuitPython

Post by Thomas_G_S »

I've tried adding that line into my code and I'm now getting an error: "RuntimeError: Function requires lock". Do you know what this means? Thanks!

User avatar
Thomas_G_S
 
Posts: 29
Joined: Fri Jan 07, 2022 4:31 pm

Re: L3GD20 (3DOF) bandwidth with CircuitPython

Post by Thomas_G_S »

Ok, I've now got rid of the "Function Requires Lock" error. Problem is, the code is now stopping running here, as if in an empty while True loop:

Code: Select all

from time import sleep
import time
import board
import digitalio
import busio
import adafruit_l3gd20

PI = 3.14159

# Initialize the SPI bus
spi = busio.SPI(board.GP18, MOSI=board.GP19, MISO=board.GP16)
# Initialize the CS pin
cs = digitalio.DigitalInOut(board.GP17)  # Replace D5 with the pin you are using for CS
cs.direction = digitalio.Direction.OUTPUT

# Initialize the L3GD20 gyroscope
l3gd20 = adafruit_l3gd20.L3GD20_SPI(spi, cs)


while not spi.try_lock():
    pass

# Setting spi frequency
spi.configure(baudrate=1000000, phase=0, polarity=0)

print("Gyro frequency has been set")
# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
# //////////////////////////////////////////////////////////////////////////////////
# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
"""The code is currently stopping running here"""
# //////////////////////////////////////////////////////////////////////////////////
# \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
# //////////////////////////////////////////////////////////////////////////////////

# Read the gyroscope data (raw data is in radians per second)
while True:
    
    start = time.monotonic()
    
    # Getting raw gyroscope values and converting to degrees per second
    x = l3gd20.gyro[0] * (180/PI)
    y = l3gd20.gyro[1] * (180/PI)
    z = l3gd20.gyro[2] * (180/PI)
    
    print("X: " + str(x) + "Y: " + str(y) + "Z: " + str(z))
    
    end = time.monotonic()
    print(end - start)
    sleep(10)



Also, when I stop the program manually my computer stops being able to detect the Pico and I have to unplug it and reconnect before running the program again.

User avatar
Thomas_G_S
 
Posts: 29
Joined: Fri Jan 07, 2022 4:31 pm

Re: L3GD20 (3DOF) bandwidth with CircuitPython

Post by Thomas_G_S »

I just tried this. Still no luck.

Code: Select all

from time import sleep
import time
import board
import digitalio
import busio
import adafruit_l3gd20

PI = 3.14159


# Initialize the SPI bus
spi = busio.SPI(board.GP18, MOSI=board.GP19, MISO=board.GP16)

while not spi.try_lock():
    pass

# Setting spi frequency
spi.configure(baudrate=1000000, phase=0, polarity=0)


# Initialize the CS pin
cs = digitalio.DigitalInOut(board.GP17)  # Replace D5 with the pin you are using for CS
cs.direction = digitalio.Direction.OUTPUT


while not spi.try_lock():
    pass
    
    try:
        spi.configure(baudrate=5000000, phase=0, polarity=0)
        cs.value = False
        result = bytearray(4)
        spi.readinto(result)
        cs.value = True
        
    finally:
        spi.unlock()


# Initialize the L3GD20 gyroscope
l3gd20 = adafruit_l3gd20.L3GD20_SPI(spi, cs)

print("Gyro frequency has been set")

# Read the gyroscope data (raw data is in radians per second)
while True:
    
    start = time.monotonic()
    
    # Getting raw gyroscope values and converting to degrees per second
    x = l3gd20.gyro[0] * (180/PI)
    y = l3gd20.gyro[1] * (180/PI)
    z = l3gd20.gyro[2] * (180/PI)
    
    print("X: " + str(x) + "Y: " + str(y) + "Z: " + str(z))
    
    end = time.monotonic()
    
    print(end - start)
    
    sleep(10)


User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: L3GD20 (3DOF) bandwidth with CircuitPython

Post by mikeysklar »

Let's try slower with less parameters and make sure you are seeing correct return from spi.frequency:

Code: Select all

spi.configure(baudrate=250000)
spi.frequency
Does the code still stop in the same place?

User avatar
Thomas_G_S
 
Posts: 29
Joined: Fri Jan 07, 2022 4:31 pm

Re: L3GD20 (3DOF) bandwidth with CircuitPython

Post by Thomas_G_S »

Thank you, it's working now!! I'm now just using this code, and it's now working at a frequency if 5 MHz:

Code: Select all

while not spi.try_lock():
    pass

spi.configure(baudrate = 5000000)
spi.frequency

spi.unlock()

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

Return to “Microcontrollers”