// -------------------------------------------------------------
// AMPERÍMETRO + VOLTÍMETRO + WATTÍMETRO
// Shunt: 0.300Ω
// OLED SSD1306 128x32
// -------------------------------------------------------------
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// ---------------- OLED -----------------
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// --------------- PINOS ------------------
const int pinCurrent = A0; // Shunt
const int pinVoltage = A2; // Divisor da bateria
// --------------- CONSTANTES --------------
const float Rshunt = 0.300; // ohms
const float voltageMultiplier = 6.0; // R1=100k / R2=20k
const int samples = 30;
// ================= SETUP =================
void setup() {
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
while (true); // trava se o OLED não inicializar
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Medidor Iniciado");
display.display();
delay(800);
}
// ================= LOOP ==================
void loop() {
// ----------- Leitura Corrente -----------
float sumA0 = 0;
for (int i = 0; i < samples; i++)
sumA0 += analogRead(pinCurrent);
float vShunt = ((sumA0 / samples) * 5.0) / 1023.0;
float current = vShunt / Rshunt; // A
// ----------- Leitura Tensao -------------
float sumA2 = 0;
for (int i = 0; i < samples; i++)
sumA2 += analogRead(pinVoltage);
float vA2 = ((sumA2 / samples) * 5.0) / 1023.0;
float batteryVoltage = vA2 * voltageMultiplier;
// ----------- Calculo Potencia -----------
float power = batteryVoltage * current; // W
// ------------- OLED DISPLAY -------------
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.print("V: ");
display.print(batteryVoltage, 2);
display.println(" V");
display.print("I: ");
display.print(current, 2);
display.println(" A");
display.print("P: ");
display.print(power, 2);
display.println(" W");
display.display();
// ---------- Debug Serial (opcional) -----
Serial.print("V = ");
Serial.print(batteryVoltage, 2);
Serial.print(" V | I = ");
Serial.print(current, 2);
Serial.print(" A | P = ");
Serial.print(power, 2);
Serial.println(" W");
delay(150);
}