0

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.

  • You can use my [MPU6050 driver](https://github.com/OneMadGypsy/upy-motion). It definitely works. I made it for the RPi Pico. – OneMadGypsy Nov 21 '22 at 13:46
  • Sorry, I am still a bit of a noob when it comes to micropython and Thonny, so I might need a bit of hand holding. I downloaded and ran the MPU6050.py and uploaded it to the pico, I know that goes a bit against the instructions on the GitHub page, but I couldn't figure out how to freeze the code or how to run the .mpy file as Thonny said it didn't recognize the .mpy file. When I ran the .py code it just created a blank line in the shell, so I think I am running the code incorrectly, but I don't know how to get it working. So the error persists and I am still getting zeros for everything – Wyatt Shepherd Nov 22 '22 at 00:56
  • Thanks for your help, I tried looking up how to access the 6th bit for the past few days with nothing, as I already made 0x6B equal to 0 which I thought would keep it awake. as mentioned above I couldn't get the interface to open, so I am confident I am doing something wrong, thanks again – Wyatt Shepherd Nov 22 '22 at 01:04
  • So I followed your instructions and I am getting an error, also when it worked before I didn't need a driver until it just stopped working when I unplugged and then plugged everything back in. – Wyatt Shepherd Nov 22 '22 at 04:32
  • Also here are the errors I am getting from running the calibration code: – Wyatt Shepherd Nov 22 '22 at 04:33
  • Traceback (most recent call last): File "", line 3, in File "mpu6050.py", line 292, in __init__ File "mpu6050.py", line 470, in __enable_interrupts File "mpu6050.py", line 93, in __writeByte File "mpu6050.py", line 75, in __writeBytes OSError: [Errno 5] EIO – Wyatt Shepherd Nov 22 '22 at 04:34
  • Never mind I just fix it, I don't need a driver, I accidentally deleted the function to initialize the MPU6050 when trying to organize my code for my group to be more legible and added this code at the end stopped the error: mpu6050_init(i2c). Thanks again for your help – Wyatt Shepherd Nov 22 '22 at 04:57

1 Answers1

0

I added this code to initialize the MPU6050: mpu6050_init(i2c) to the end of my code (right before the 'steps = 0 #step counter' bit). This is calling a function near the top of the code to initialize the board to get out of sleep mode, which is what causes 'sleep mode' on the device.