python - Raspberry Pi Pico with KX134 accelerometer not being recognised despite being detected - Stack Overflow

I'm using the code below to communicate between a Raspberry Pi Pico and a KX134 accelerometer conn

I'm using the code below to communicate between a Raspberry Pi Pico and a KX134 accelerometer connected over I2C. The device is being detected by a ping, however the program itself does not register it as being connected. I'm using the qwiic_kx13x library, which in itself depends on the qwiic_i2c library (links pasted below). Thank you in advance :)

import qwiic_i2c
import qwiic_kx13x
import time
import sys

# Initialize I2C driver (assuming qwiic_i2c doesn't support custom SDA/SCL)
my_bus = qwiic_i2c.get_i2c_driver(sda=4, scl=5, freq=100000)

ping_result = my_bus.ping(0x1F)  # Check for device presence at the correct address 0x1F
print("Device is connected:", ping_result)

def runExample():
    print("\nSparkFun KX13X Accelerometer Example 1\n")

    # Initialize the KX13X device at I2C address 0x1F
    myKx = qwiic_kx13x.QwiicKX13XCore(address=0x1F)  # Corrected address

    if not myKx.connected:
        print("The Qwiic KX13X Accelerometer device isn't connected to the system. Please check your connection", \
              file=sys.stderr)
        return

    if myKx.begin():
        print("Ready.")
    else:
        print("Make sure you're using the correct KX13X device")

    # Set the accelerometer range if needed, uncomment if range is required
    # myKx.set_range(myKx.KX132_RANGE8G)

    # Initialize with basic settings
    myKx.initialize(myKx.BASIC_SETTINGS)

    while True:
        # Get accelerometer data
        myKx.get_accel_data()
        print("X: {0}g Y: {1}g Z: {2}g".format(
            myKx.kx134_accel.x,
            myKx.kx134_accel.y,
            myKx.kx134_accel.z))
        
        time.sleep(0.02)  # Delay for the Output Data Rate, default 50Hz (1/50 = 0.02)

if __name__ == '__main__':
    try:
        runExample()
    except (KeyboardInterrupt, SystemExit):
        print("\nEnding Example 1")
        sys.exit(0)


I've tried pinging the device (shows as connected) and specifying an exact I2C address (0x1F) instead of allowing it to choose the default. None of these worked.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745648199a4638097.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信