ESP32-S3 firmware to control the opening level of a 12V NC solenoid valve using PWM, for the alambic project.
Drive a 12V normally-closed solenoid valve at partial opening levels instead of simple on/off. The ESP32 generates a 12-bit PWM signal (0–4095) that switches a MOSFET on the 12V power line of the valve. Adjusting the duty cycle changes the average current in the coil, which holds the plunger at intermediate positions.
The first version used the on-board BOOT button to step the duty cycle (+100 per press) and the serial monitor to read the current value. Finding the right frequency/duty combination this way was slow: every frequency change required editing the code and reflashing.
The current version hosts a web server directly on the ESP32. The board creates its own WiFi access point, and a web page with two sliders allows live tuning of both parameters without reflashing:
- Frequency slider: 50 Hz to 15 kHz
- Duty slider: 0 to 4095 (12-bit), with live percentage display
Changes are applied in real time while dragging the sliders.
| Component | Role |
|---|---|
| ESP32-S3 DevKit | PWM generation + WiFi AP + web server |
| IRLZ44N logic-level MOSFET | Switches the 12V valve line from a 3.3V gate signal |
| 1N5819 Schottky diode | Flyback protection across the valve coil |
| 220 Ω resistor | Gate series resistor |
| 10 kΩ resistor | Gate pull-down |
| 12V NC solenoid valve | Controlled load (water) |
| 12V external supply | Valve power (common ground with ESP32) |
Wiring details: see circuit_description.md.
- Flash
valve_pwm_control/valve_pwm_control.inoon the ESP32-S3 (Arduino IDE or arduino-cli, boardesp32:esp32:esp32s3). - Connect to the WiFi network ValveControl (password:
valve1234). - Open http://192.168.4.1 in a browser.
- Adjust frequency and duty with the sliders; the serial monitor (115200 baud) logs every change.
A standard on/off solenoid valve is not a proportional valve: the usable duty-cycle range for partial opening is narrow (typically somewhere between ~60% and ~85%), with hysteresis between opening and closing. This is expected physical behavior, not a firmware issue.