LPG gas leak alarm
A kitchen gas sniffer that beeps and flashes the moment LPG levels climb.
Shop by category
Shop all componentsThe MPU6050 measures gravity along three axes, and from that you can compute pitch and roll. This sketch averages readings to reduce jitter, subtracts an offset you set with a button-less calibration at start-up, and draws a bubble that moves as you tilt the board. Lay it flat on a surface and press reset to zero it. It is accurate to about a degree, which is plenty for hanging shelves.
Tick anything you already have and the total updates.
GY-521 MPU6050 accelerometer and gyro
Follow the colours: red is power, dark grey is ground, blue is a signal.
USB power bank for a portable level.
spirit_level.inoVerify & upload in Electrolark#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_SSD1306.h>
Adafruit_MPU6050 mpu;
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
float offX = 0, offY = 0, fx = 0, fy = 0;
void readAngles(float &roll, float &pitch) {
sensors_event_t a, g, t;
mpu.getEvent(&a, &g, &t);
roll = atan2(a.acceleration.y, a.acceleration.z) * 180 / PI;
pitch = atan2(-a.acceleration.x, sqrt(a.acceleration.y * a.acceleration.y + a.acceleration.z * a.acceleration.z)) * 180 / PI;
}
void setup() {
Wire.begin(21, 22);
mpu.begin();
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.setTextColor(WHITE);
delay(500);
float r, p;
for (int i = 0; i < 50; i++) { readAngles(r, p); offX += r; offY += p; delay(10); }
offX /= 50; offY /= 50;
}
void loop() {
float r, p;
readAngles(r, p);
fx = 0.8 * fx + 0.2 * (r - offX); // smooth the jitter
fy = 0.8 * fy + 0.2 * (p - offY);
int bx = constrain(96 + (int)(fx * 2), 70, 122);
int by = constrain(32 + (int)(fy * 2), 6, 58);
oled.clearDisplay();
oled.setCursor(0, 0); oled.printf("Roll %+5.1f", fx);
oled.setCursor(0, 12); oled.printf("Pitch %+5.1f", fy);
oled.drawCircle(96, 32, 26, WHITE);
oled.drawLine(96, 6, 96, 58, WHITE); oled.drawLine(70, 32, 122, 32, WHITE);
oled.fillCircle(bx, by, 4, WHITE);
oled.display();
delay(30);
}
Check both I2C addresses (OLED 0x3C, MPU6050 0x68) with an I2C scanner.
Keep the board still during the first second after reset, and let it warm up for a minute.
Swap the sign in the roll or pitch line to match how you mounted the board.
About one degree after zeroing, enough for shelves and frames but not for surveying.
Yes. Add a buzzer and sound it when both angles are under half a degree.
The start-up zeroing is usually enough for a spirit level.
5 parts: ESP32-WROOM-32 38-Pin Development Board, GY-521 MPU6050 6-Axis Gyro and Accelerometer Module, 0.96" OLED Display 128x64, Solderless Breadboard and Dupont Jumper Wires (40 pcs). On this page you can tick the parts you already have and add only the rest to your cart.
It is a intermediate project and takes about 1.5 hours. The build on this page has 6 steps.
No. This page has the complete, tested code (spirit_level.ino).
About ₹764 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Reading an accelerometer with the Adafruit MPU6050 library; Converting acceleration to angle; Drawing a live bubble on an OLED.
Ideas that reuse this build: Digital spirit level. They are listed under Take it further on this page.
A kitchen gas sniffer that beeps and flashes the moment LPG levels climb.
See the smoke level live on a crisp screen and get a loud warning when it spikes.
An infrared eye that spots an open flame and raises the alarm.
Walk into the room and it sounds the alarm: the classic first security build.
Learning project. Check every circuit and the datasheet of each part before you power it up.