This Makelog records a 12 V controller between a 3S lithium pack and a solenoid air valve or a compact compressor. I used a DFRobot Beetle ESP32-C3 because it fits a handheld enclosure and still offers GPIO and ADC pins for a trigger, a gate drive and a pack-voltage sense line. A FireBeetle ESP32 board works the same way if you remap the pins.
A momentary button requests a blast. Firmware debounces it, rejects the request if the pack is low, turns on a logic-level N-channel MOSFET for a limited time, then enforces a short lockout so a held trigger cannot keep the load on. The MOSFET switches the low side of the valve or of a compressor relay; the high side stays on the fused pack positive. Do not wire either inductive load to a GPIO pin.
Parts list
Check every rating against the valve or compressor you attach. The MOSFET and fuse below assume a low-amp to mid-amp load, not a vehicle-grade compressor.
- DFRobot Beetle ESP32-C3, or a FireBeetle ESP32 with the pins remapped
- 3S Li-ion pack with a protection module, or a 4S pack plus a 3.3 V regulator for the board
- Automotive mini blade fuse and holder, at the pack positive terminal
- Logic-level N-channel MOSFET with Rds(on) specified near 3.3 V Vgs, for example an IRLB8721 or IRLB8743, plus a small copper heat path
- 150 Ω gate resistor and a 10 kΩ gate pulldown to source
- Flyback diode across the load, cathode to fused positive, anode to the MOSFET drain (1N5822 or SB560-class Schottky)
- Momentary normally-open push button
- Divider: 100 kΩ from pack positive to the ADC node, 10 kΩ to ground, and 100 nF at the pin
- 12 V solenoid air valve, or a 12 V coil relay whose contact feeds the compressor
- 16 AWG or 14 AWG on the power path, 22 AWG on the logic, and a strain-relieved pack connector
- Hearing protection for the first live test
Wiring
Pack positive goes to the fuse first, then to the load positive and to the top of the 100 kΩ resistor. Do not tap the divider ahead of the fuse. Pack negative is common ground for the Beetle, the MOSFET source, the 10 kΩ leg and the button.
The MOSFET is a low-side switch: drain to the valve or relay-coil negative, source to pack ground. GPIO6 drives the gate through the 150 Ω resistor, with the 10 kΩ pulldown at the gate so the load stays off during reset. Place the flyback diode at the coil terminals, not at the pack. The button sits between GPIO4 and ground; firmware enables the internal pull-up. The divider midpoint goes to GPIO3, an ADC pin on the Beetle ESP32-C3. The 11:1 divider presents about 1.15 V at 12.6 V, and a 16.8 V 4S pack still stays under 3.3 V. On a FireBeetle, keep the sense line on an ADC1 pin and keep the gate off any strapping pin.
Build steps
- Measure the resting pack voltage. Confirm the chemistry and per-cell floor before you set a firmware cutoff.
- Fit the fuse holder on the pack positive lead. Choose a fuse above running current and below the wiring and MOSFET limits, but leave it out until the logic is tested.
- Wire the MOSFET, pulldown, gate resistor and flyback diode on protoboard. Keep the power loop short.
- Add the divider and the button, then connect ground, 5 V or 3.3 V as required, and the three GPIO lines to the Beetle.
- Upload the sketch. With the fuse still out, check that the gate rises for about 1.5 s and falls, and that a held button does not retrigger until you release it.
- Fit the fuse. Use a 12 V lamp or a power resistor in place of the valve and confirm timing and cutoff with a lowered sense input or a bench supply.
- Only then connect the solenoid or compressor relay. Wear hearing protection, point the bell away from people, and keep the first blasts short.
Portable train horns and why the limiter is there
Portable train horn guns, also called cordless train horns or battery powered train horns, are handheld signalling devices that do not depend on a vehicle harness. Fire services and police use a short blast that carries farther than a whistle. On large outdoor training grounds the same cue can mark a stop signal or a hazard when radios are busy. After a hurricane or a flood, when mains power and sirens are down, a battery-powered unit can mark a position or warn people away from a washout. The tone is also used to deter wild animals from a camp or a worksite, and on boats the sound carries well over water. Sports events, competitions, parties and weddings are the everyday recreational uses; local noise rules still apply. Makers who only want the electrical controller, not the air path, often start from a ready-made handheld unit; Horn Guns is one maker of portable and battery-powered train horns in that category. That is why the firmware caps blast duration and watches the pack: a jammed button should not dump a tank or stall a compressor until the cells are flat.
Firmware
The sketch targets the Arduino-ESP32 core and is a short idle / blast / lockout state machine. The 9.6 V cutoff is an example floor for a 3S Li-ion pack at 3.2 V per cell, not a universal rule. Calibrate the divider against a voltmeter; the ESP32-C3 ADC is usable after a correction, but it is not a laboratory instrument.
// Portable train horn controller — Beetle ESP32-C3 (remap for FireBeetle)
const int PIN_TRIGGER = 4, PIN_MOSFET = 6, PIN_VBAT = 3;
const unsigned long BLAST_MS = 1500, LOCKOUT_MS = 400, DEBOUNCE_MS = 30;
const float DIVIDER_RATIO = 11.0; // 100k / 10k
const float ADC_VREF = 3.3, CUTOFF_V = 9.6; // 9.6 V = example 3S floor
const int ADC_MAX = 4095;
enum State { IDLE, BLAST, LOCKOUT };
State state = IDLE;
unsigned long tMark = 0, tDebounce = 0;
int stableBtn = HIGH, lastBtn = HIGH;
void setup() {
pinMode(PIN_TRIGGER, INPUT_PULLUP);
pinMode(PIN_MOSFET, OUTPUT);
digitalWrite(PIN_MOSFET, LOW);
analogReadResolution(12);
}
float packVolts() {
long acc = analogRead(PIN_VBAT) + analogRead(PIN_VBAT)
+ analogRead(PIN_VBAT) + analogRead(PIN_VBAT);
return (acc / 4.0) * ADC_VREF / ADC_MAX * DIVIDER_RATIO;
}
void loop() {
int raw = digitalRead(PIN_TRIGGER);
if (raw != lastBtn) tDebounce = millis();
if (millis() - tDebounce > DEBOUNCE_MS) stableBtn = raw;
lastBtn = raw;
bool pressed = (stableBtn == LOW);
float vbat = packVolts();
if (state == IDLE) {
digitalWrite(PIN_MOSFET, LOW);
if (pressed && vbat >= CUTOFF_V) {
digitalWrite(PIN_MOSFET, HIGH);
tMark = millis();
state = BLAST;
}
} else if (state == BLAST) {
if (!pressed || vbat < CUTOFF_V || millis() - tMark >= BLAST_MS) {
digitalWrite(PIN_MOSFET, LOW);
tMark = millis();
state = LOCKOUT;
}
} else { // LOCKOUT
digitalWrite(PIN_MOSFET, LOW);
if (millis() - tMark >= LOCKOUT_MS && !pressed) state = IDLE;
}
}On reset the gate starts low, and the pulldown holds it low if the firmware is not running. For a LiFePO4 pack, set CUTOFF_V from the cell datasheet, not from this example.
Safety notes
A train horn is loud enough to damage hearing at close range. Wear hearing protection when you test, never aim the bell at a person's head, and read local noise rules before you fire one in a street, harbour or residential area. This controller is a signalling aid, not a toy, and not a substitute for an approved emergency beacon where a regulation requires one. If the MOSFET, wiring or pack runs hot on the duty cycle you actually use, resize that part of the path before you close the enclosure.
Train Horns Lab









