MPU6050

MPU6050 Test writing to registers

After reading the WhoAmI register OK, I moved on to writing and then reading back of other registers.  The following program write to gyro and accel resisters and tested OK.  A sample output is given at the very bottom of the listing.

# mpu6050_test11_2019mar1701 tlfong01 2019mar17hkt1507
# 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 default value 0x68 in register WhoImI 0x75
# 5. Write something to registers gyro/accelConfig and read back

# *** Imports ***

import datetime
from time import sleep
import inspect
import smbus

# *** Device Address ***

mpu6050Addr = 0x68

# *** Register Addresses ***

whoAmIAddr = 0x75
powerMgmt1Addr = 0x6b
gyroConfigAddr = 0x1b
accelConfigAddr = 0x1c

# *** Control Bytes ***

resetByte = 0x80
wakeupByte = 0x00
gyroConfigByte = 0xe8 # trigger x, y, z, with full scale 0x01
accelConfigByte = 0xec # trigger x, y, z, with full scale 0x11

# *** Print Functions ***

def printDateTime():
timeNow = datetime.datetime.now()
timeNowStr = str(timeNow)[0:16]
print(‘ Date Time now =’, timeNowStr)
return

def convertByteToFourCharPadString(num):
s = hex(num)
s = (s[2:])
if (len(s) != 2):
s = ‘0’ + s
s = ‘0x’ + s
return s

# *** General I2C Device 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, convertByteToFourCharPadString(readByte))

# *** MPU6050 Specific Register Write, Read, Print Functions ***

def operationDelay():
sleep(0.001) # 1mS
return

def readPrintReg(regAddrByte, printTitle):
print(‘ regAddr =’, hex(regAddrByte))
readByte = readDevRegOneByte(i2cCh, mpu6050Addr, regAddrByte)
print(‘ ‘, printTitle, ‘ =’, convertByteToFourCharPadString(readByte))
return

def writeReadPrintReg(regAddrByte, writeByte, printTitle):
print(‘ regAddr =’, hex(regAddrByte))
print(‘ writeByte =’, convertByteToFourCharPadString(writeByte))
writeDevRegOneByte(i2cCh, mpu6050Addr, regAddrByte, writeByte)
operationDelay()
readPrintReg(regAddrByte, printTitle)
return

# *** MPU6050 System Functions ***

def resetMpu(i2cCh):
print(‘ Begin’, inspect.stack()[0][3])
writeDevRegOneByte(i2cCh, mpu6050Addr, powerMgmt1Addr, resetByte)
operationDelay()
readPrintReg(powerMgmt1Addr, ‘ powerMgmt1 ‘)
print(‘ End ‘, inspect.stack()[0][3])
return

def wakeupMpu(i2cCh):
print(‘ Begin’, inspect.stack()[0][3])
writeDevRegOneByte(i2cCh, mpu6050Addr, powerMgmt1Addr, wakeupByte)
operationDelay()
readPrintReg(powerMgmt1Addr, ‘ powerMgmt1 ‘)
print(‘ End ‘, inspect.stack()[0][3])
return

# *** MPU6050 Test Functions ***

def readPrintRegWhoAmI():
print(‘ Begin’, inspect.stack()[0][3])
readPrintReg(whoAmIAddr, ‘ ahoAmI (0x75) =’)
print(‘ End ‘, inspect.stack()[0][3])
return

def writeReadPrintRegGyroConfig():
print(‘ Begin’, inspect.stack()[0][3])
writeReadPrintReg(gyroConfigAddr, gyroConfigByte, ‘ gyroConfig (0x1b) ‘)
print(‘ End ‘, inspect.stack()[0][3])
return

def writeReadPrintRegAccelConfig():
print(‘ Begin’, inspect.stack()[0][3])
writeReadPrintReg(accelConfigAddr, accelConfigByte, ‘ accleConfig (0x1c) ‘)
print(‘ End ‘, inspect.stack()[0][3])
return

def testMpu01():
readPrintRegWhoAmI()
writeReadPrintRegGyroConfig()
writeReadPrintRegAccelConfig()
return

# *** Init Function Execution ***

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

# *** Main Function Execution ***

testMpu01()

# *** End ***

”’
>>>
== RESTART: /home/pi/Python_Programs/test1189/mpu6050_test11_2019mar1701.py ==
Date Time now = 2019-03-17 15:08
Begin resetMpu
regAddr = 0x6b
powerMgmt1 = 0x40
End resetMpu
Begin wakeupMpu
regAddr = 0x6b
powerMgmt1 = 0x00
End wakeupMpu
Begin readPrintRegWhoAmI
regAddr = 0x75
ahoAmI (0x75) = = 0x68
End readPrintRegWhoAmI
Begin writeReadPrintRegGyroConfig
regAddr = 0x1b
writeByte = 0xe8
regAddr = 0x1b
gyroConfig (0x1b) = 0xe8
End writeReadPrintRegGyroConfig
Begin writeReadPrintRegAccelConfig
regAddr = 0x1c
writeByte = 0xec
regAddr = 0x1c
accleConfig (0x1c) = 0xec
End writeReadPrintRegAccelConfig
>>>
”’

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