LPG gas leak alarm
A kitchen gas sniffer that beeps and flashes the moment LPG levels climb.
Shop by category
Shop all componentsThe L3G4200D is a 3-axis MEMS gyroscope: it measures how fast it is rotating, in degrees per second, around each axis. Add up (integrate) that rate over time and you get the angle turned, which is how robots know they have turned 90 degrees. This sketch writes the gyro's control registers directly over I2C, calibrates out its offset while still, and shows the Z-axis angle and rate on an OLED. Press the button to zero it.
Tick anything you already have and the total updates.
Follow the colours: red is power, dark grey is ground, blue is a signal.
USB to the Nano.
gyro_angle.inoVerify & upload in Electrolark#include <Wire.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(128, 64, &Wire);
const int ADDR = 0x69;
float offset = 0, angle = 0;
unsigned long last;
void wr(byte reg, byte val) { Wire.beginTransmission(ADDR); Wire.write(reg); Wire.write(val); Wire.endTransmission(); }
int16_t readZ() {
Wire.beginTransmission(ADDR); Wire.write(0x2C | 0x80); Wire.endTransmission(); // OUT_Z_L, auto-increment
Wire.requestFrom(ADDR, 2);
return Wire.read() | (Wire.read() << 8);
}
float rateZ() { return readZ() * 0.00875; } // 250 dps range: 8.75 mdps per count
void setup() {
Wire.begin();
pinMode(4, INPUT_PULLUP);
wr(0x20, 0x0F); // CTRL_REG1: normal mode, X Y Z on, 100 Hz
wr(0x23, 0x00); // CTRL_REG4: 250 dps
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
delay(200);
for (int i = 0; i < 200; i++) { offset += rateZ(); delay(10); }
offset /= 200;
last = micros();
}
void loop() {
unsigned long now = micros();
float r = rateZ() - offset;
if (abs(r) < 0.3) r = 0; // ignore noise when still
angle += r * (now - last) / 1e6;
last = now;
if (digitalRead(4) == LOW) angle = 0;
static unsigned long d = 0;
if (millis() - d > 100) {
d = millis();
oled.clearDisplay(); oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(3); oled.setCursor(0, 0); oled.print(angle, 0); oled.setTextSize(1); oled.print(" deg");
oled.setCursor(0, 44); oled.print("Rate "); oled.print(r, 1); oled.print(" deg/s");
oled.display();
}
delay(5);
}
Recalibrate by resetting while still; gyros always drift a little.
Wrong address; try 0x68.
Fast turns may exceed 250 dps; set CTRL_REG4 to 500 dps and use 0.0175.
Small errors add up over time when you integrate; a compass or accelerometer corrects it.
Only by integrating; an accelerometer measures tilt directly.
The one through the face of the chip, so turning it flat on a table.
6 parts: L3G4200D 3-Axis Gyroscope Module, Generic Nano CH340 Development Board (Soldered), 0.96" OLED Display 128x64, Tactile Push Button Switch, 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-2 hours. The build on this page has 5 steps.
No. This page has the complete, tested code (gyro_angle.ino).
About ₹858 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Gyroscope rate versus angle; Integrating and why it drifts; Reading sensor registers directly.
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.
Lessons are free to read. Maker (₹499/month) and Pro add quizzes, worked problems and certificates, paid with your Geekobot account. See plans
Learning project. Check every circuit and the datasheet of each part before you power it up.