So I am working on a step counter using a raspberry pi pico and a MPU6050 when last night I had the code working fine so I unplugged the pico, then I went to plug the pico back in this morning and now it's displaying zeros. I configured the code accordingly to these hookups:
VCC to 3v3
GND to GND
SCL to GP1
SDA to GP0
Here is the code:
#import PIN and I2C from machine library
from machine import Pin, I2C
import time
#Define I2C bus
i2c = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1))
#Device address on the I2C bus
MPU6050_ADDR = 0x68
#PWR_MGMT_1 memory address
MPU6050_PWR_MGMT_1 = 0x6B
#Accelerometer's high and low register for each axis
MPU6050_ACCEL_XOUT_H = 0x3B
MPU6050_ACCEL_XOUT_L = 0x3C
MPU6050_ACCEL_YOUT_H = 0x3D
MPU6050_ACCEL_YOUT_L = 0x3E
MPU6050_ACCEL_ZOUT_H = 0x3F
MPU6050_ACCEL_ZOUT_L = 0x40
#Accelerometer's LSB/g (least significant bits per gravitational force) sensitivity
MPU6050_LSBG = 16384.0
#Set all bits in the PWR_MGMT_1 register to 0
def mpu6050_init(i2c):
i2c.writeto_mem(MPU6050_ADDR, MPU6050_PWR_MGMT_1, bytes([0])) #needs to be 0 to have device in 'awake' mode change to 1 for 'sleep' mode
#define x, y, and z values
def accel_x_value(h, l):
if not h[0] & 0x80:
return h[0] << 8 | l[0]
return -((h[0] ^ 255) << 8) | (l[0] ^ 255) + 1
def accel_y_value(h, l):
if not h[0] & 0x80:
return h[0] << 8 | l[0]
return -((h[0] ^ 255) << 8) | (l[0] ^ 255) + 1
def accel_z_value(h, l):
if not h[0] & 0x80:
return h[0] << 8 | l[0]
return -((h[0] ^ 255) << 8) | (l[0] ^ 255) + 1
#Get Accelerometer values
def get_accel_x(i2c):
accel_x_h = i2c.readfrom_mem(MPU6050_ADDR, MPU6050_ACCEL_XOUT_H, 1)
accel_x_l = i2c.readfrom_mem(MPU6050_ADDR, MPU6050_ACCEL_XOUT_L, 1)
return accel_x_value(accel_x_h, accel_x_l) / MPU6050_LSBG
def get_accel_y(i2c):
accel_y_h = i2c.readfrom_mem(MPU6050_ADDR, MPU6050_ACCEL_YOUT_H, 1)
accel_y_l = i2c.readfrom_mem(MPU6050_ADDR, MPU6050_ACCEL_YOUT_L, 1)
return accel_y_value(accel_y_h, accel_y_l) / MPU6050_LSBG
def get_accel_z(i2c):
accel_z_h = i2c.readfrom_mem(MPU6050_ADDR, MPU6050_ACCEL_ZOUT_H, 1)
accel_z_l = i2c.readfrom_mem(MPU6050_ADDR, MPU6050_ACCEL_ZOUT_L, 1)
return accel_z_value(accel_z_h, accel_z_l) / MPU6050_LSBG
steps = 0 #step counter
while True:
if get_accel_x(i2c) > 0.6: #minimum x value for counter
steps += 1
elif get_accel_y(i2c) > 3: #minimum y value for counter
steps += 1
elif get_accel_z(i2c) > 3: #minimum z value for counter
steps += 1
print("\nsteps:", steps)
print("Accelerometer:\t", get_accel_x(i2c), get_accel_y(i2c), get_accel_z(i2c), "g")
#Print Accelerometer values (X,Y,Z)
time.sleep(0.75) #Delay between values in seconds
First I replaced the hardware, I tried a different MPU6050 and new wires (I only have one pico so I can't try a different one) yet I still get the same zero error. Next I tried testing the MPU6050 with some test code I found (from this website: https://www.hackster.io/mr-alam/how-to-use-i2c-pins-in-raspberry-pi-pico-i2c-scanner-code-8f489f) and it came back working on both MPU6050s, yet still the same zero error. Next I changed the pins and the code to reflect that, yet still the same zero error. Lastly I looked online for help and couldn't find anything useful except for someone mentioning 'tie the SLEEP bit low in the PWR_MGMT_1 register' (from: MPU6050 only outputs 0x00 on I2C with MicroPython) however I have no clue what that means and I don't have enough mentions to comment on that question, so I can't ask.