For an index to all my stories click this text.
When I was working at my project for the Caravan/Mobile-home leveler (previous story on this weblog) I had an idea. What if we could use the data from the MPU6050 to detect vibration or mechanical movement.
The breadboard setup.
This is the same setup as I used in my stories about the caravan / mobile-home leveling aid.
This should not pose any problems and you can easily build this on a breadboard.
Just make sure that the SCL pin from the MPU6050 is attached to pin 5 (D1) of the ESP8266 and the SDA pin is attached to pin 4 (D2).
As this is so simple you can use any other microcontroller, like an ESP32 or Raspberry Pi Pico, to build this project. You even do not need to make alterations in the program except for the pin numbers.
MicroPython program
from machine import Pin, I2C import time import math from lucsmpulib import init_mpu, get_data i2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000) # esp8266 init_mpu(i2c) totz = 0 i = 0 # Calculate the average value of z for i in range (20): x, y, z = get_data(i2c) totz = totz + z average = totz / 20 print(average) # use a margin great = average + 0.017 less = average - 0.017 while True: x, y, z = get_data(i2c) if z > great or z< less: print("vibration detected ",z) time.sleep(.5) time.sleep(.2)
The idea is simple.
First a measurement is done 20 times and from that the average value for z is calculated.
Then we add a margin to the average value and call that great and we subtract the same value from average and call that less.
I used a margin of 0.017 but you will need to do some tests and adjust it to your own circumstances.
Inside the while loop we constant read the value of z and test if it is greater then the value of great or smaller as the value of less and if that is the case: vibration is detected.
Each time after vibration is detected we wait for .5 seconds to make sure everything is settled down before we take another reading.
This program is just a framework. You need to write additional code to make this useful like adding a led that lights up when vibration is detected. Or send a notification to your phone. I am sure you can find some purpose for this.
The MPU6050 library
To use this program you will need my own MPU6050 library. I discussed that in the story about the caravan / mobile-home leveling aid. But for your convenience I add it here:
''' Luc Volders library for MPU6050 ''' from machine import Pin, I2C import time def init_mpu(i2c, address=0x68): i2c.writeto_mem(address, 0x6B, b'\x00') time.sleep_ms(100) i2c.writeto_mem(address, 0x19, b'\x07') i2c.writeto_mem(address, 0x1B, b'\x00') i2c.writeto_mem(address, 0x1B, b'\x00') i2c.writeto_mem(address, 0x1C, b'\x00') def read_data(i2c, addr, address=0x68): high = i2c.readfrom_mem(address, addr, 1)[0] low = i2c.readfrom_mem(address, addr + 1, 1)[0] value = high << 8 | low if value > 32768: value = value - 65536 return value def get_data(i2c): readx = read_data(i2c, 0x3B) / 16384.0 ready = read_data(i2c, 0x3B + 2) / 16384.0 readz = read_data(i2c, 0x3B + 4) / 16384.0 return readx, ready, readz
That's all for now.
Till next time
Luc Volders