A hobby gas sensor is not a certified alarm and does not replace a real gas or smoke detector.
Gas / smoke alarm (hobby)
MQ-2 sensor with a buzzer and a web page. A hobby add-on, not a certified detector.
A waterproof ultrasonic JSN-SR04T measures the level in a tank or well and reports percent.
Safety Board: ESP32 DevKit V1 (WROOM-32) Level: ●●○ ⏱ 1–2 h
The JSN-SR04T is the waterproof cousin of the HC-SR04. The transducer is on a cable, so the electronics stay dry. It has a blind zone of around 20 to 25 cm in front of the head, so mount it higher than the maximum water level by at least that much.
Take several readings and use the median, which removes the occasional echo from a pipe or a wall. Convert distance to a percentage with the height of your tank, and the speed of sound if you want to be exact about temperature. The echo line is 5 V, so divide it down for the ESP32.
| Module | Module pin | ESP32 pin | Note |
|---|---|---|---|
| JSN-SR04T VCC / GND | 5V / GND | ||
| JSN-SR04T TRIG | out | GPIO5 | |
| JSN-SR04T ECHO | in | GPIO18 via 1k/2k divider | Echo is 5 V |
#include <WiFi.h>
const char* WIFI_SSID = "YOUR_WIFI";
const char* WIFI_PASS = "YOUR_PASSWORD";
bool connectWiFi(uint32_t timeoutMs = 20000) {
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
uint32_t t0 = millis();
while (WiFi.status() != WL_CONNECTED && millis() - t0 < timeoutMs) {
delay(250);
Serial.print(".");
}
Serial.println();
if (WiFi.status() == WL_CONNECTED) {
Serial.print("IP: ");
Serial.println(WiFi.localIP());
return true;
}
return false;
}
#include <WebServer.h>
WebServer server(80);
String buildJson(); // implemented in the project below
String num(float v, int d = 1) { return isnan(v) ? String("null") : String(v, d); }
const char PAGE_HTML[] PROGMEM = R"rawliteral(
<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>ESP32</title>
<style>body{font-family:system-ui;background:#0b0f14;color:#e6edf3;margin:20px}table{border-collapse:collapse}td{padding:6px 14px;border-bottom:1px solid #223}button,input{font-size:16px;padding:8px 14px;margin:4px;border-radius:6px;border:1px solid #345;background:#16202b;color:#e6edf3}</style>
</head><body><h2>ESP32</h2><table id="t"></table>
<script>
function load(){fetch('/json').then(r=>r.json()).then(d=>{let h='';for(const k in d){h+='<tr><td>'+k+'</td><td>'+d[k]+'</td></tr>';}document.getElementById('t').innerHTML=h;});}
load();setInterval(load,2000);
</script></body></html>
)rawliteral";
void startWeb() {
server.on("/", []() { server.send_P(200, "text/html", PAGE_HTML); });
server.on("/json", []() { server.send(200, "application/json", buildJson()); });
server.begin();
}
const int TRIG = 5, ECHO = 18;
float TANK_EMPTY_CM = 150, TANK_FULL_CM = 20; // distance from the sensor to the bottom / to the full level
float distCm = NAN; int pct = -1;
float measure() {
float s[5]; int n = 0;
for (int i = 0; i < 5; i++) {
digitalWrite(TRIG, LOW); delayMicroseconds(4); digitalWrite(TRIG, HIGH); delayMicroseconds(12); digitalWrite(TRIG, LOW);
long us = pulseIn(ECHO, HIGH, 40000);
if (us > 0) s[n++] = us * 0.0343 / 2.0;
delay(60);
}
if (!n) return NAN;
for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++) if (s[j] < s[i]) { float x = s[i]; s[i] = s[j]; s[j] = x; }
return s[n / 2]; // median
}
String buildJson() { return String("{"distance_cm":") + num(distCm) + ","level_%":" + pct + "}"; }
void setup() {
pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT);
Serial.begin(115200);
connectWiFi();
startWeb();
}
void loop() {
server.handleClient();
static uint32_t t = 0;
if (millis() - t > 5000) { t = millis(); distCm = measure(); pct = isnan(distCm) ? -1 : constrain((int)((TANK_EMPTY_CM - distCm) * 100.0 / (TANK_EMPTY_CM - TANK_FULL_CM)), 0, 100); }
}
Buy the parts kit · Estimated total: $10.50
Ukraine: we buy and sell these parts ourselves, order them on this site.
Some links are affiliate links: we may earn a commission at no extra cost to you.
A hobby gas sensor is not a certified alarm and does not replace a real gas or smoke detector.
MQ-2 sensor with a buzzer and a web page. A hobby add-on, not a certified detector.
A DS18B20 probe inside the freezer; a buzzer and a message if the temperature rises or the door stays open.
A hobby gas sensor is not a certified alarm and does not replace a real gas or smoke detector.
An MQ-7 sensor with a buzzer and display for a garage, boiler room or generator shed. A hobby add-on, not a certified CO alarm.
Comments
No comments yet. Share your experience or ask a question.