Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*By: Randy N. Cañada, 29/11/2025
- Simple Arduino, INA219 voltage/current sensor + OLED +arduino UNO
- Connections:
- INA219- refer to image file, SDA=A4 , SCL= A5
- SSD1306 SDA = A4, SCL = A5, GND(ARDUINO)-GROUND(SSD1306),3.3VDC/5VDC(ARDUINO)-SSD1306 VCC
- MY YOUTUBE CHANNEL: ElectroCrafters:
- https://www.youtube.com/channel/UCu1aqzUdTqSnTN5BHNlbVgA
- MY FB CHANNEL: FB PAGE:
- Arduino:
- https://www.facebook.com/rhacan2?__tn__=R
- PLS ALSO SUPPORT MY OTHER PAGE:FB PAGES:
- Playlist.ph:
- https://www.facebook.com/profile.php?id=61573177858722
- SUPPORT MY OTHER YOUTUBE CHANNEL:
- PlaylistPH
- https://www.youtube.com/@PlaylistPh-h2v
- */
- #include <Wire.h>
- #include <Adafruit_INA219.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- // INA219 object
- Adafruit_INA219 ina219;
- // OLED setup
- #define SCREEN_WIDTH 128 // OLED display width, in pixels
- #define SCREEN_HEIGHT 64 // OLED display height, in pixels
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
- // --- Battery thresholds (adjust for your test supply) ---
- const float VBAT_MIN = 1.42; // Example: cutoff
- const float VBAT_MAX = 1.50; // Example: full charge
- int buzzer = A2;
- int greenLED = 7;
- int redLED = 6;
- void setup() {
- Serial.begin(9600);
- pinMode(buzzer,OUTPUT);
- pinMode(greenLED,OUTPUT);
- pinMode(redLED,OUTPUT);
- // INA219 init
- if (!ina219.begin()) {
- Serial.println("Failed to find INA219 chip");
- while (1);
- }
- ina219.setCalibration_32V_2A();
- Serial.println("INA219 ready!");
- // OLED init
- if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // 0x3C is default I2C addr
- Serial.println("SSD1306 allocation failed");
- while (1);
- }
- display.clearDisplay();
- display.setTextSize(2); // Normal 1:1 pixel scale
- display.setTextColor(SSD1306_WHITE);
- display.setCursor(0,0);
- display.println("OLED Ready");
- display.display();
- delay(1000);
- }
- void loop() {
- float busVoltage = ina219.getBusVoltage_V(); // Voltage across load
- float current_mA = ina219.getCurrent_mA(); // Current draw
- float power_mW = ina219.getPower_mW(); // Power consumption
- int pct = batteryPercent(busVoltage);
- // --- Serial output (3 decimal places) ---
- Serial.print("Bus Voltage: "); Serial.print(busVoltage, 3); Serial.println(" V");
- Serial.print("Current: "); Serial.print(current_mA, 3); Serial.println(" mA");
- Serial.print("Power: "); Serial.print(power_mW/1000.0, 3); Serial.println(" W");
- Serial.print("Battery %: "); Serial.print(pct); Serial.println("%");
- if (pct <= 5) {
- Serial.println("⚠️ WARNING: Battery low!");
- digitalWrite(redLED, HIGH);
- digitalWrite(greenLED, LOW);
- digitalWrite(buzzer,!digitalRead(buzzer));
- }else{
- digitalWrite(greenLED, HIGH);
- digitalWrite(redLED, LOW);
- digitalWrite(buzzer, LOW);
- }
- Serial.println("-----------------------------");
- // --- OLED output ---
- display.clearDisplay();
- display.setCursor(0,0);
- display.setTextSize(2);
- display.print("V: "); display.print(busVoltage, 2); display.println(" V");
- display.print("I: "); display.print(current_mA, 1); display.println(" mA");
- display.print("P: "); display.print(power_mW, 1); display.println(" mW");
- // ✅ Battery line
- display.fillRect(0, 48, 128, 16, SSD1306_BLACK);
- display.setCursor(0,48);
- char buffer[12];
- sprintf(buffer, "Batt:%3d%%", pct);
- display.print(buffer);
- // ✅ Trigger low battery by percentage threshold
- if (pct <= 5) {
- display.clearDisplay();
- display.setTextSize(2);
- display.setCursor(52,1);
- display.print("LOW");
- display.setCursor(28,32);
- display.print("BATTERY!");
- }
- display.display();
- delay(1000);
- }
- int batteryPercent(float vbat) {
- if (vbat <= VBAT_MIN) return 0;
- if (vbat >= VBAT_MAX) return 100;
- return (int)((vbat - VBAT_MIN) * 100.0 / (VBAT_MAX - VBAT_MIN));
- }
Advertisement
Add Comment
Please, Sign In to add comment