GY-521 Accelerometer/Gyroscope: migrate from custom component

For years, I have used a GY-521 sensor on an ESPHome device to monitor my laundry. The code has used a custom component which I copied the code from somewhere…but it’s identical to the one posted here:

I just updated my ESPHome instance and now in order to update this device I understand I have to switch to an external component. I have no idea how to do that, nor franky, no interest. But I see there is a built in component here:

I have spent hours today trying to use that component in an equivalent manner, yet I get no data from the sensor, and my derivative sensors all return nan. Is it compatible with the GY-521, specifically, this one?

If so, how can I get this to work?

i2c:
  sda: 21
  scl: 22
  scan: true
  id: bus_a

sensor:
  - platform: mpu6050
    address: 0x68
    update_interval: 1s
    i2c_id: bus_a
    accel_x:
      internal: true
      id: dryer_accel_x
    accel_y:
      internal: true
      id: dryer_accel_y
    accel_z:
      internal: true
      id: dryer_accel_z
    gyro_x:
      internal: true
      id: dryer_gyro_x
    gyro_y:
      internal: true
      id: dryer_gyro_y
    gyro_z:
      internal: true
      id: dryer_gyro_z
  - platform: template
    internal: true
    id: dryer_accel  
    unit_of_measurement: "G"
    update_interval: 1s  
    accuracy_decimals: 4
    lambda: |-
      return (sqrt((float)pow(id(dryer_accel_x).state, 2) + (float)pow(id(dryer_accel_y).state, 2) + (float)pow(id(dryer_accel_z).state, 2)) / 32768) * 2;
    filters:
      - lambda: |-
          id(dryer_delta_accel) = fabsf(id(dryer_accel).state - x);
          return(x);       
  - platform: template
    internal: true
    id: dryer_gyroscope
    unit_of_measurement: "deg/s"    
    update_interval: 1s      
    accuracy_decimals: 4
    lambda: |-
      return (sqrt((float)pow(id(dryer_gyro_x).state, 2) + (float)pow(id(dryer_gyro_y).state, 2) + (float)pow(id(dryer_gyro_z).state, 2)) / 32768) * 250;
    filters:
      - lambda: |-
          id(dryer_delta_gyroscope) = fabsf(id(dryer_gyroscope).state - x);
          return(x);

Sometimes I get errors similar to what is in that original github issue, but it seems that was resolved years ago.

Any help?

What are you really trying to do?

I had an idea to use the accelerometer in my m5 atom device as a vibration sensor so I could tell if my generator was running or not.

Since you have this on a dryer, I am guessing that you are probably doing something similar. If so, you probably don’t really care about the gyro. You really probably don’t need to know where in 3-space your dryer is. You just need to know if it is moving or not.

The calculations you are doing are probably excessive and probably should be done more frequently to get a better answer.

Mine gets the raw values from the accelerometers more frequently, but uses the raw numbers and compares to the last value. If it exceeds a settable threshold it increments a counter. Every settable period for the vibration sensor, it returns the counter and the resets it.

This seems to work pretty well for both my generator and my HPWH, but with different thresholds. In my generator it can sense when I open the cover, but not when I just walk by it.

If you want to take the easy way out, you can bring back custom component support: GitHub - robertklep/esphome-custom-component: Brings back support for custom ESPHome components

1 Like

Update ESPHome on a sensor whose code has worked fine for years.

This is the way. Worked perfect for the GY-521 as well as another device I hadn’t begun to mess with yet. Thank you sir.

1 Like