B4R Question ESP8266 (NodeMcu) and I2C for MPU-6050

peacemaker

Expert
Licensed User
Longtime User
HI, All

Dummy first questions, pardon in advance: searching forum second day and cannot find any way to use the subject.
If no ready lib for MPU-6050 at ESP8266 (this one is only for Arduino PCBs, as i can understand)- how to use generic I2C code to work with MPU-6050 ? Any example ?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Two relevant links:
Code that you need to port: https://www.electronicwings.com/nodemcu/mpu6050-interfacing-with-nodemcu

Another option is to use inline C with the code. It does require some C knowledge.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User

THANKS MUCH ! Ohh, how hard is to search the forum ... - it's my fault to search somehow crooked
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Semi-finally, the working example is assembled:
B4X:
'Used https://www.b4x.com/android/forum/threads/inertial-measurements-mpu-6050-and-magnetometer-hmc5883l.65917/post-417358
'Used https://www.electronicwings.com/nodemcu/mpu6050-interfacing-with-nodemcu
'Used https://www.mschoeffler.de/2017/10/05/tutorial-how-to-use-the-gy-521-module-mpu-6050-breakout-board-with-the-arduino-uno/

'v.0.1

' I2C Bus Esp8266:
' D1 is GPI05 to MPU6050 SCL
' D2 is GPI04 to MPU6050 SDA


Sub Process_Globals
    Public Serial1 As Serial
    Public MPU6050SlaveAddress As Byte = 0x68    'I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69.

    Private wire As WireMaster
    Private raf As RandomAccessFile
    Private AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ As Float
    Private tmr As Timer
  
    Public const MPU6050_REGISTER_SMPLRT_DIV   =  0x19 As Byte
    Public const MPU6050_REGISTER_USER_CTRL    =  0x6A As Byte
    Public const MPU6050_REGISTER_PWR_MGMT_1   =  0x6B As Byte
    Public const MPU6050_REGISTER_PWR_MGMT_2   =  0x6C As Byte
    Public const MPU6050_REGISTER_CONFIG       =  0x1A As Byte
    Public const MPU6050_REGISTER_GYRO_CONFIG  =  0x1B As Byte
    Public const MPU6050_REGISTER_ACCEL_CONFIG =  0x1C As Byte
    Public const MPU6050_REGISTER_FIFO_EN      =  0x23 As Byte
    Public const MPU6050_REGISTER_INT_ENABLE   =  0x38 As Byte
    Public const MPU6050_REGISTER_ACCEL_XOUT_H =  0x3B As Byte
    Public const MPU6050_REGISTER_SIGNAL_PATH_RESET  = 0x68 As Byte
  
    Public const AccelScaleFactor = 16384 As UInt   'sensitivity scale factor respective To full scale setting provided in datasheet
    Public const GyroScaleFactor = 131 As UInt
End Sub

Public Sub AppStart
    Serial1.Initialize(115200)
    wire.Initialize
    Setup_MPU6050
    tmr.Initialize("tmr_tick", 3000)
    tmr.Enabled = True
End Sub

Sub Setup_MPU6050
    wire.WriteTo(MPU6050SlaveAddress, Array As Byte(MPU6050_REGISTER_SMPLRT_DIV, 0x07))
    wire.WriteTo(MPU6050SlaveAddress, Array As Byte(MPU6050_REGISTER_PWR_MGMT_1, 0x01))
    wire.WriteTo(MPU6050SlaveAddress, Array As Byte(MPU6050_REGISTER_PWR_MGMT_2, 0x00))
    wire.WriteTo(MPU6050SlaveAddress, Array As Byte(MPU6050_REGISTER_CONFIG, 0x00))
    wire.WriteTo(MPU6050SlaveAddress, Array As Byte(MPU6050_REGISTER_GYRO_CONFIG, 0x00))    'set +/-250 degree/second full scale
    wire.WriteTo(MPU6050SlaveAddress, Array As Byte(MPU6050_REGISTER_ACCEL_CONFIG, 0x00))    'set +/- 2g full scale
    wire.WriteTo(MPU6050SlaveAddress, Array As Byte(MPU6050_REGISTER_FIFO_EN, 0x00))
    wire.WriteTo(MPU6050SlaveAddress, Array As Byte(MPU6050_REGISTER_INT_ENABLE, 0x01))        'needed ?
    wire.WriteTo(MPU6050SlaveAddress, Array As Byte(MPU6050_REGISTER_SIGNAL_PATH_RESET, 0x00))
    wire.WriteTo(MPU6050SlaveAddress, Array As Byte(MPU6050_REGISTER_USER_CTRL, 0x00))
End Sub

Sub tmr_tick
    wire.WriteTo(MPU6050SlaveAddress, Array As Byte(MPU6050_REGISTER_ACCEL_XOUT_H))
    Dim b() As Byte = wire.RequestFrom(MPU6050SlaveAddress, 14)    '14 bytes data range
    If b.Length = 14 Then
        raf.Initialize(b,False)
        AcX  = raf.ReadInt16(raf.CurrentPosition) / AccelScaleFactor    'reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
        AcY  = raf.ReadInt16(raf.CurrentPosition) / AccelScaleFactor    'reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
        AcZ  = raf.ReadInt16(raf.CurrentPosition) / AccelScaleFactor    'reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
        Tmp  = raf.ReadInt16(raf.CurrentPosition) / 340.00 + 36.53        'reading registers: 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
        GyX  = raf.ReadInt16(raf.CurrentPosition) / GyroScaleFactor        'reading registers: 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
        GyY  = raf.ReadInt16(raf.CurrentPosition) / GyroScaleFactor        'reading registers: 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
        GyZ  = raf.ReadInt16(raf.CurrentPosition) / GyroScaleFactor        'reading registers: 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)
      
        Log("acx: ", AcX , ", acy: ", AcY ," ,acz: ", AcZ,", Tmp: ",NumberFormat(Tmp,1,1) , ", GyX: ", GyX , ", GyY: ", GyY ," ,GyZz: ", GyZ)
    Else
        Log("Missing sensors data...")
    End If
End Sub

It needs to think about Setup registers.
But strange that Y-axle data is not changed (always is the max value of 2g), X, Z and temperature look OK.

Anyone seen such "partially defective" data from MPU6050 ?
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
But strange that Y-axle data is not changed (always is the max value of 2g), X, Z and temperature look OK.

Note: that MPU6050 chipset can be defective: one of my 3 pcs always returns AcY=1.9999 g, but all other parameters are "live", and 2 others NodeMcu PCBs are fully OK.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…