ESPHome and MPU6050 Accelerometer

Hi! Could you please share the connection diagram?

Thank you!

Hi all,

I am having issues with my MPU6050 sensor. I added it to HomeAssistant via ESPHOME with the configuration shown below. The configuration is uploaded without issues, however, the readings are “unknown” - “nan”

Any ideas on what I am doing wrong here?


captive_portal:

i2c:

  scl: GPIO5

  sda: GPIO4

  scan: true

 

sensor:

  - platform: mpu6050

    address: 0x68

    accel_x:

      id: accel_x1

      name: "accel X1"

     

    accel_y:

      id: accel_y1    

      name: "accel Y1"

               

    accel_z:

      id: accel_z1    

      name: "accel Z1"

             

    gyro_x:

      name: "gyro X1"

             

    gyro_y:

      name: "gyro Y1"

               

    gyro_z:

      name: "gyro Z1"

               

    temperature:

      name: "MPU6050 Temperature"

    update_interval: 1.5s

  - platform: template

    id: roll1

    name: pan1roll

    accuracy_decimals: 2

    lambda: |-

      return  (atan( id(accel_y1).state / sqrt( pow( id(accel_x1).state , 2) + pow( id(accel_z1).state , 2) ) ) * 180 / PI) ;

    update_interval: 250ms

  - platform: template

    id: pitch1

    name: pan1pitch

    accuracy_decimals: 2

   

    lambda: |-

      return  (atan(-1 * id(accel_x1).state / sqrt(pow(id(accel_y1).state, 2) + pow(id(accel_z1).state, 2))) * 180 / PI);

    update_interval: 250ms

Connections are sensor → esp8266mod

Vcc->3V
Gnd->G
scl->D1
sda->D2

Make sure you have good connections.
The initial setup of the MPU only happens once at boot and if the connections are bad then you get nan.

Thanks for your reply! It seems my MPU is defective. I connected another one and it worked without problems.

Thanks!!!

@Bohemian would you mind sharing your config / setup? I’m looking at the exact same usecase

@mattclar - keep in mind that I am using the GY-25A sensor as mentioned upthread. That said, here the relevant chunks of code:

esphome:
  name: pergola-sensor
  platform: ESP8266
  board: d1_mini
  
# Pins Used
# A0 - Sensor in

globals:
  - id: angle_offset_voltage
    type: float
    restore_value: no
# Set to dummy value so we can figure out if we've rebooted later
    initial_value: '999.9'

The global is used as a cheat to see if power has been cycled. I did that rather than saving the offset voltage to the ESP since that has a finite number of writes and it’s in an inconvenient place to replace. I store the actual value in an MQTT queue that I read with a small Node-RED flow.

# Set up angle sensor
sensor:
  - platform: adc
    pin: A0
#    name: "Pergola Angle Voltage"
    id: angle_voltage
    update_interval: 10ms
    accuracy_decimals: 3

# Smooth sensor voltage for calibration
  - platform: template
    id: angle_voltage_smoothed
    lambda: return (id (angle_voltage).state);
    update_interval: 100ms
    accuracy_decimals: 3
    filters:
      - sliding_window_moving_average:
          window_size: 50
          send_every: 10

# Expose angle offset from  HA  
  - platform: homeassistant
    name: "Stored Angle Sensor Offset"
    entity_id: sensor.angle_sensor_offset_voltage
    id: stored_angle_sensor_offset
    accuracy_decimals: 2

# Calculate angle from sensor      
  - platform: template
    id: pergola_angle
    name: "Pergola Angle"
    update_interval: 100ms
    accuracy_decimals: 0
# x-axis 0-360 deg is proportional to 0-1 volt, so this is an easy calculation
    lambda: return ((id (angle_voltage).state - id (angle_sensor_offset).state) * 360);
    unit_of_measurement: "°"

# Expose angle offset back to HA      
  - platform: template
    id: angle_sensor_offset
    name: "Angle Sensor Offset"
    update_interval: 1s
    accuracy_decimals: 2
    lambda: |- 
      if (id (angle_offset_voltage) > 5.0) {
      return (id (stored_angle_sensor_offset).state);
      } else {
      return (id (angle_offset_voltage));
      }


# Surface button for sensor offset calibration
button:
  - platform: template
    name: "Calibrate Tilt Sensor"
    icon: "mdi:restart"
    on_press:
      - lambda: |-
          id (angle_offset_voltage) = id (angle_voltage_smoothed).state;

I will get around to cleaning this code up one day, but it works fine for now.

People might be interested in this, too:
https://van-automation.com/adding_van_tilt_sensor.html

1 Like

This is working much better for me, thanks
ESP Code:

esphome:
  name: solarangle1
esp8266:
  board: d1_mini
# Enable logging
logger:
i2c:
  sda: GPIO4
  scl: GPIO5
  scan: true

# Enable Home Assistant API
api:

sensor:
 - platform: mpu6050
   address: 0x68
   update_interval: 200ms
   accel_x:
     name: "MPU6050 Accel X1"
   accel_y:
     name: "MPU6050 Accel Y1"
ota:
  password: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

wifi:
  ssid: !secret iotwifi_ssid
  password: !secret iotwifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Solarangle1 Fallback Hotspot"
    password: !secret wifi_password

web_server:
  port: 80

captive_portal:

Home assistant code:

- platform: template
  sensors:
######################
# Solar Angle 1 Code #
######################      
    solar1x_angle:
      friendly_name: "X Angle1"
      unit_of_measurement: "°"
      value_template: '{{ ((asin((states("sensor.mpu6050_accel_x1") | float) / 9.81) * 180 / pi) - 2.8)  }}'
    solar1y_angle:
      friendly_name: "Y Angle1"
      unit_of_measurement: "°"
      value_template: '{{ ((asin((states("sensor.mpu6050_accel_y1") | float) / 9.81) * 180 / pi) + 0.3)  }}' 
- platform: filter
  name: "Filtered Solar 1 X Angle"
  entity_id: sensor.solar1x_angle
  filters:
    - filter: lowpass
      time_constant: 5
      precision: 1
- platform: filter
  name: "Filtered Solar 1 Y Angle"
  entity_id: sensor.solar1y_angle
  filters:
   - filter: lowpass
     time_constant: 5
     precision: 1