LPG gas leak alarm
A kitchen gas sniffer that beeps and flashes the moment LPG levels climb.
Shop by category
Shop all componentsA Sharp GP2Y0A21 infrared sensor gives an analog voltage that drops as an object moves away, but not in a straight line. The Nano reads the voltage and converts it with a power-law fit that works for the 10 to 80 cm range. Closer than about 10 cm the reading folds back and looks like a far object, so the sensor must be mounted at least 10 cm from anything it should see.
Infrared is invisible but harmless at this power; do not stare at it from very close.
Tick anything you already have and the total updates.
Sharp GP2Y0A21YK0F IR distance sensor, 10 to 80 cm
Follow the colours: red is power, dark grey is ground, blue is a signal.
USB power is fine. The sensor draws about 30 mA in short bursts, so keep the capacitor close to its connector.
sharp_ir.inoVerify & upload in Electrolark#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
float distanceCm() {
long sum = 0;
for (int i = 0; i < 20; i++) sum += analogRead(A0); // average to cut noise
float volts = (sum / 20.0) * 5.0 / 1023.0;
if (volts < 0.4) return 80; // beyond range
return 27.86 * pow(volts, -1.15); // fit for the GP2Y0A21: about 10-80 cm
}
void setup() { oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); }
void loop() {
float d = distanceCm();
oled.clearDisplay(); oled.setTextColor(WHITE);
oled.setTextSize(3); oled.setCursor(0, 10); oled.print(d, 0);
oled.setTextSize(1); oled.print(" cm");
oled.display();
delay(100);
}
The sensor cannot see closer than about 10 cm. Move it back.
Add the capacitor, average more samples, and avoid bright sunlight on the sensor.
Dark or shiny objects reflect differently. Calibrate with the surface you will really use.
It has a narrower beam and reacts faster, but a shorter range and it is sensitive to colour.
Yes, with a different fit constant. The datasheet graph gives the values.
The sensor needs 4.5 to 5.5 V, and its output can reach about 3 V, so check your board's ADC range.
6 parts: Generic Nano CH340 Development Board (Soldered), Sharp GP2Y0A21YK0F IR Distance Sensor, 10-80 cm, 0.96" OLED Display 128x64, Capacitor Assortment Kit, 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 beginner project and takes about 1 hour. The build on this page has 5 steps.
No. This page has the complete, tested code (sharp_ir.ino).
About ₹1,183 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Why an analog sensor's output is non-linear; Curve fitting a datasheet graph; Smoothing a noisy ADC reading.
Infrared is invisible but harmless at this power; do not stare at it from very close.
Ideas that reuse this build: Ultrasonic distance meter. 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.