Marcio Cunha

How to build a smart DIY UPS with ESP32 and inverter

Learn how to design an uninterruptible power system for your homelab using a deep-cycle battery, pure sine wave inverter, and ESP32 telemetry.

Marcio Cunha•3 min
Also available in:PortuguêsEspañol
Summary
  • Deep-cycle batteries provide safe and reliable backup for demanding homelab environments.
  • Pure sine wave inverters prevent damage to sensitive server power supply units.
  • The ESP32 microcontroller enables real-time Wi-Fi monitoring of voltage and temperature.
  • Smart automations prevent hardware damage during prolonged electricity blackouts.
  • Monitoring hardware bottlenecks helps maintain overall server stability under load.

Why build a DIY UPS for your homelab environment

Keeping a home server or homelab running without interruptions requires reliable electrical redundancy. However, traditional consumer uninterruptible power supplies often feature small batteries and zero room for physical expansion. By designing a custom solution using deep-cycle stationary batteries, you gain real autonomy to keep routers, storage arrays, and mini PCs powered for hours. In practice, this means a sudden power outage will not corrupt your files or take down your critical containers. To ensure everything runs smoothly and securely, it is worth checking out the infrastructure details in how to build a home Proxmox server for network testing running essential services.

Choosing the right components for your electrical project

The heart of the system consists of a 12-volt deep-cycle battery, a voltage inverter, and an intelligent charging circuit. Stationary batteries differ from standard car batteries because they withstand repeated deep discharges without losing capacity quickly. The inverter transforms direct current from the battery into clean alternating current, preferably pure sine wave to protect sensitive server components. Additionally, proper wire gauge sizing is vital to prevent overheating during high-current draws. Alongside electrical stability, monitoring hard drive performance is critical, as discussed in slow Linux disk? How to spot IOPS bottlenecks and latency to avoid systemic failures.

Integrating telemetry with ESP32 and sensors

To turn a mechanical setup into an intelligent UPS, we use an ESP32 microcontroller equipped with Wi-Fi and Bluetooth capabilities. We connect resistive voltage dividers to the analog pins of the ESP32 to monitor battery health without damaging the microcontroller. DS18B20 temperature sensors attached to the battery casing provide early warnings if thermal runaway or overheating occurs. This telemetry data is transmitted via the MQTT protocol to your central dashboard, allowing you to visualize grid status instantly. If power fails and the battery drops to a critical threshold, automated scripts can gracefully shut down VMs and containers before total power loss.

Assembly scheme and ESP32 reading code

Physical assembly requires extreme care regarding electrical insulation due to high currents involved in the 12-volt setup. Below, we present a functional C++ snippet using the Arduino IDE to safely read battery voltage through a calibrated resistive voltage divider.

const int analogPin = 34; // ESP32 ADC pin for voltage divider reading
const float resistenciaR1 = 30000.0; // Higher resistor connected to positive
const float resistenciaR2 = 7500.0; // Resistor connected to ground

void setup() {
  Serial.begin(115200);
}

void loop() {
  int rawValue = analogRead(analogPin);
  float voltage = (rawValue / 4095.0) * 3.3; // Convert ADC reading to volts
  float batteryVoltage = voltage * ((resistenciaR1 + resistenciaR2) / resistenciaR2);
  
  Serial.print("Battery Voltage: ");
  Serial.print(batteryVoltage);
  Serial.println(" V");
  
  delay(5000);
}

This code continuously reads the analog voltage, applies the mathematical voltage divider formula, and outputs the result on the serial port, ready for wireless transmission. With these metrics integrated, your homelab gains complete operational intelligence and reliability.

Final considerations on redundancy and safety

Building a smart DIY UPS with an ESP32 and deep-cycle battery turns your homelab resilience into a fully customizable and transparent project. You step away from commercial black boxes and gain absolute control over runtime and operating metrics. Although it demands careful handling of electricity and microcontroller programming, the payoff in reliability rewards every assembly step. Keep connections tight, check sensors periodically, and ensure your data remains safe even when the grid fails.