Adding a Jaycar MMA8452Q Tri-Axis Tilt Sensor

Because I am new to HA (from OA) and couldn’t find and reference to a MMA8452 sensor, and as a way of documentation for my future re-use, I thought others might find this interesting on how I connected this up.

I was looking for a way to integrate a MMA8452Q sensor onto ESPHome on a Nodemcu 1.0 device, predominantly as a way I can monitor natural gas usage in my house (where I live in Australia, we have EDMI-U8 natural gas meters installed, and I wanted to monitor our natural gas usage.
Others have stated monitoring devices like this using OCR or via Magnetosensor)
I’m going to try the magneto way to avoid blocking the meter and facing questions on what it is and what its doing there by the utilities company.

Searching the component lists of ESPHome, I could only find “other” devices similar but none would work, and because I’d already bought one - and this device is cheap and easily accessible in Australia through Jaycar XC3732 - I thought I’d put some effort to work out how to do it - and hopefully this might be useful for someone else here.

here is my journey:

  1. testing it on arduino - this was easy, I just downloaded the MMA8452Q example library from arduino libraries. Plugged that device into the unit and uploaded with serial monitor showing it was working when I moved it! easy!
    to get it connected to the nodemcu is easier as an i2C via pins GND/3.3 & SDA(to D2)/SCL(to D1).

  2. Uploaded ESPhome onto the NodeMCU with the web interface - again easy!

  3. it appears that someone (“s-marian”) else has made drivers for ESPEasy but I couldnt find any documentation for it… so I trolled through the .h and .ccp and found the correct lines.
    turns out: add to the .yaml you need to add the following lines to put up their code

external_components:
  - source:
      type: git
      url: https://github.com/s-marian/esphome-components-mma8452q
  1. As this requires I2C, you need to add it to the .yaml too
    You need to add I2C to your ESPHome config too:
i2c:
  sda: GPIO4 #D2 pin on NodeMCU
  scl: GPIO5 #D1 pin on NodeMCU
  scan: true 
  1. to make the device readable as a sensor add to the .yaml:
sensor:
  - platform: mma8452q
    address: 0x1D
    accel_x:
      name: "Field Strength X"
    accel_y:
      name: "Field Strength Y"
    accel_z:
      name: "Field Strength Z"
    update_interval: 1s
  1. On upload, watch the logfiles and the I2C will scan the bus and tell you what the device address is… change that in the .yaml
    'address: 0xD0

on reboot you should get something like:

...
15:15:59|[D]|[mma8452q:432]|Got accel={x=-0.038 m/s², y=-0.801 m/s², z=-0.549 m/s²},|
|15:15:59|[D]|[sensor:093]|'Field Strength X': Sending state -0.03809 m/s² with 2 decimals of accuracy|
|15:15:59|[D]|[sensor:093]|'Field Strength Y': Sending state -0.80078 m/s² with 2 decimals of accuracy|
|15:15:59|[D]|[sensor:093]|'Field Strength Z': Sending state -0.54883 m/s² with 2 decimals of accuracy|
...

… and that’s where Im at right now… now to work on getting the code to read the gas meter diaphragm.

Hope this helps someone else.