Apa Projek Ni
Monitor bateri 12V (macam bateri kereta atau solar setup) guna ESP12E. Baca voltage, display kat OLED, hantar data ke server. Macam fuel gauge tapi untuk bateri.
Components
- ESP12E / NodeMCU
- Voltage divider (25V max → 3.3V for ADC)
- OLED SSD1306 128×64
- 12V battery
Voltage Divider
ESP8266 ADC max 3.3V (actually 1V internal, NodeMCU ada built-in divider to 3.3V). Untuk baca 12V battery, kena scale down lagi.
Guna resistor divider: R1 = 100kΩ, R2 = 33kΩ
Vout = Vin × R2 / (R1 + R2) Vout = 12V × 33k / (100k + 33k) = 2.98V ← safe for ADC Max measurable: 3.3V × (133k / 33k) = 13.3V
Covers full 12V battery range (10.5V empty → 12.8V full).
Wiring
| From | To |
|---|---|
| Battery + | R1 (100kΩ) → junction → R2 (33kΩ) → GND |
| Junction (between R1 & R2) | A0 |
| Battery – | ESP GND (common ground!) |
| OLED SDA | D2 |
| OLED SCL | D1 |
⚠️ Common ground penting — battery GND dan ESP GND kena sama. Kalau tak, reading jadi nonsense.
Code
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#define ADC_PIN A0
#define R1 100000.0
#define R2 33000.0
#define VREF 3.3
Adafruit_SSD1306 display(128, 64, &Wire, -1);
void setup() {
Serial.begin(115200);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
}
void loop() {
int raw = analogRead(ADC_PIN);
float vADC = raw * (VREF / 1023.0);
float vBattery = vADC * ((R1 + R2) / R2);
// Battery percentage (rough estimate for lead-acid)
// 10.5V = 0%, 12.8V = 100%
float pct = (vBattery - 10.5) / (12.8 - 10.5) * 100.0;
pct = constrain(pct, 0, 100);
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 0);
display.printf("%.1fV", vBattery);
display.setTextSize(1);
display.setCursor(0, 30);
display.printf("%.0f%%", pct);
display.display();
Serial.printf("Battery: %.2fV (%.0f%%)\n", vBattery, pct);
delay(5000);
}
Calibration
Resistor tolerance affect accuracy. Untuk calibrate:
- Ukur battery voltage dengan multimeter
- Compare dengan reading dari ESP
- Adjust ratio dalam code:
float calibration = actual_voltage / esp_reading;
Battery Voltage Reference (12V Lead-Acid)
| Voltage | State |
|---|---|
| 12.7V+ | 100% (full) |
| 12.4V | 75% |
| 12.2V | 50% |
| 12.0V | 25% |
| 11.8V | ~0% (discharge soon) |
| 10.5V | Dead — stop discharging! |
Notes
- Add 100nF capacitor between A0 and GND untuk stabilize reading
- Take average of 10 readings untuk reduce noise
- Kalau nak monitor over time, push data ke InfluxDB/MQTT macam projek sensor udara
- For LiPo batteries, voltage range different — adjust accordingly