MPU6050

MPU6050 First Newbie Python Program

MPU6050 First Newbie Python Program

Now I have written my first very simple python program which reads the MPU6050’s register 0x75. The program is fully listed below.

tlfong01’s MPU6050 Programming Penzu Diary
https://penzu.com/p/bcc27149

mpu6050_test03_2019mar1401 tlfong01 2019mar14hkt1424

# mpu6050_test03_2019mar1401 tlfong01 2019mar14hkt1424
# Rpi3B+ stretch linux 4.14.34-v7+ arm python3.5.3

# *** Description ***

# 1. Setup I2C channel
# 2. Reset MPU
# 3. Wakeup MPU
# 4. Read and print register default value 0x68 of register 0x75 (WhoAmI)

# *** Imports ***

import smbus
from time import sleep

# *** Device Address ***

mpu6050Addr = 0x68

# *** Register Addresses ***

whoAmIAddr = 0x75
powerMgmt1Addr = 0x6b

# *** Control Bytes ***

resetByte = 0x80
wakeupByte = 0x00

# *** Read Write Print Device/Register Functions ***

def writeDevTwoBytes(i2cPort, devAddrByte, dataByte1, dataByte2):
i2cPort.write_byte_data(devAddrByte, dataByte1, dataByte2)
return

def readDevRegOneByte(i2cPort, devAddrByte, regAddrByte):
readByte = i2cPort.read_byte_data(devAddrByte, regAddrByte)
return readByte

def writeDevRegOneByte(i2cPort, devAddrByte, regAddrByte, writeByte):
writeDevTwoBytes(i2cPort, devAddrByte, regAddrByte, writeByte)
return

def printDevRegOneByte(i2cPort, devAddrByte, regAddrByte, printTitle):
readByte = readDevRegOneByte(i2cPort, devAddrByte, regAddrByte)
print(printTitle, hex(readByte))

# *** MPU6050 Functions ***

def operationDelay():
sleep(0.000020) # 20 micro seconds
return

def resetMpu(i2cCh):
print(‘ Begin resetMpu(), …’)
writeDevRegOneByte(i2cCh, mpu6050Addr, powerMgmt1Addr, resetByte)
operationDelay()
printDevRegOneByte(i2cCh, mpu6050Addr, powerMgmt1Addr, ‘ Reg 0x6b (PowerMgmt1) =’)
print(‘ End resetMpu().’)
return

def wakeupMpu(i2cCh):
print(‘ Begin wakeupMpu(), …’)
writeDevRegOneByte(i2cCh, mpu6050Addr, powerMgmt1Addr, wakeupByte)
operationDelay()
printDevRegOneByte(i2cCh, mpu6050Addr, powerMgmt1Addr, ‘ Reg 0x6b (PowerMgmt1) =’)
print(‘ End wakeupMpu().’)
return

# *** Test Functions ***

def readPrintMpu6050RegWhoAmI():
print(‘ Begin readPrintMpu6050RegWhoAmI(), …’)
printDevRegOneByte(i2cCh, mpu6050Addr, whoAmIAddr, ‘ Reg 0x75 (Who_Am_I) =’)
print(‘ End readPrintMpu6050RegWhoAmI().’)
return

def testMpu01():
readPrintMpu6050RegWhoAmI()
return

# *** Init Functions ***

i2cCh = smbus.SMBus(1)
resetMpu(i2cCh)
wakeupMpu(i2cCh)

# *** Main ***

testMpu01()

# *** End ***

”’
# *** Begin Sample Output 2019mar14hkt1442 ***
Begin resetMpu(), …
Reg 0x6b (PowerMgmt1) = 0x40
End resetMpu().
Begin wakeupMpu(), …
Reg 0x6b (PowerMgmt1) = 0x0
End wakeupMpu().
Begin readPrintMpu6050RegWhoAmI(), …
Reg 0x75 (Who_Am_I) = 0x68
End readPrintMpu6050RegWhoAmI()
# ** End Sample Output ***
”’

.END


Categories: MPU6050

Tagged as:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s