LPG gas leak alarm
A kitchen gas sniffer that beeps and flashes the moment LPG levels climb.
Shop by category
Shop all componentsThe LSM303DLHC combines an accelerometer and a magnetometer. The magnetometer senses the Earth's magnetic field; the direction of its horizontal part gives the heading. Nearby steel and speakers bend the field, so the sketch first records the minimum and maximum on each axis while you turn the compass round (hard-iron calibration) and then uses the centre of each range. In India the magnetic north is within about a degree of true north, so no big correction is needed.
Mount it where it does not block your view or an airbag.
Tick anything you already have and the total updates.
LSM303DLHC accelerometer and magnetometer module
Follow the colours: red is power, dark grey is ground, blue is a signal.
USB from the car's charger.
compass.inoVerify & upload in Electrolark#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
#include <Adafruit_LSM303DLH_Mag.h>
Adafruit_LSM303DLH_Mag_Unified mag(12345);
float mnX = 1000, mxX = -1000, mnY = 1000, mxY = -1000;
const char* POINTS[] = {"N", "NE", "E", "SE", "S", "SW", "W", "NW"};
void setup() {
pinMode(3, INPUT_PULLUP);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); oled.setTextColor(WHITE);
mag.begin();
}
void loop() {
sensors_event_t e; mag.getEvent(&e);
float x = e.magnetic.x, y = e.magnetic.y;
if (digitalRead(3) == LOW) {
mnX = min(mnX, x); mxX = max(mxX, x); mnY = min(mnY, y); mxY = max(mxY, y);
}
float cx = (mnX < mxX) ? (mnX + mxX) / 2 : 0, cy = (mnY < mxY) ? (mnY + mxY) / 2 : 0;
float h = atan2(y - cy, x - cx) * 57.2958;
if (h < 0) h += 360;
oled.clearDisplay(); oled.setTextSize(1); oled.setCursor(0, 0);
oled.print(digitalRead(3) == LOW ? "Calibrating: turn me" : "Heading");
oled.setTextSize(3); oled.setCursor(0, 20); oled.print(POINTS[(int)((h + 22.5) / 45) % 8]);
oled.setTextSize(2); oled.setCursor(70, 26); oled.print((int)h);
oled.display();
delay(100);
}
It is near a speaker or steel bracket; move it, then calibrate again.
The board's X arrow is not pointing forward; rotate it or add an offset.
This simple version needs to be flat; add tilt compensation with the accelerometer.
No, it uses the Earth's magnetic field only.
The car's steel shifts the field; calibrating where it is mounted cancels most of that.
Yes, with a GPS for your position you can compute the direction to any place.
6 parts: LSM303DLHC High-Precision 3-Axis Accelerometer & Magnetometer Module – I2C Interface, 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 2 hours. The build on this page has 5 steps.
No. This page has the complete, tested code (compass.ino).
About ₹637 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
How a magnetometer compass works; Hard-iron calibration; Turning a heading into compass points.
Mount it where it does not block your view or an airbag.
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.