rhandycan1

INA219 voltage/current sensor + OLED

Nov 29th, 2025 (edited)
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | Source Code | 0 0
  1. /*By: Randy N. Cañada, 29/11/2025
  2. Simple Arduino, INA219 voltage/current sensor + OLED +arduino UNO
  3.  
  4. Connections:
  5. INA219- refer to image file, SDA=A4 , SCL= A5
  6. SSD1306  SDA = A4, SCL = A5, GND(ARDUINO)-GROUND(SSD1306),3.3VDC/5VDC(ARDUINO)-SSD1306 VCC
  7.  
  8.  
  9. MY YOUTUBE CHANNEL: ElectroCrafters:
  10. https://www.youtube.com/channel/UCu1aqzUdTqSnTN5BHNlbVgA
  11.  
  12. MY FB CHANNEL: FB PAGE:
  13. Arduino:
  14. https://www.facebook.com/rhacan2?__tn__=R
  15.  
  16. PLS ALSO SUPPORT MY OTHER PAGE:FB PAGES:
  17. Playlist.ph:
  18. https://www.facebook.com/profile.php?id=61573177858722
  19.  
  20. SUPPORT MY OTHER YOUTUBE CHANNEL:
  21. PlaylistPH
  22. https://www.youtube.com/@PlaylistPh-h2v
  23.  
  24. */
  25.  
  26. #include <Wire.h>
  27. #include <Adafruit_INA219.h>
  28. #include <Adafruit_GFX.h>
  29. #include <Adafruit_SSD1306.h>
  30.  
  31. // INA219 object
  32. Adafruit_INA219 ina219;
  33.  
  34. // OLED setup
  35. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  36. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  37. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
  38.  
  39. // --- Battery thresholds (adjust for your test supply) ---
  40. const float VBAT_MIN = 1.42;   // Example: cutoff
  41. const float VBAT_MAX = 1.50;   // Example: full charge
  42.  
  43. int buzzer = A2;
  44. int greenLED = 7;
  45. int redLED = 6;
  46.  
  47. void setup() {
  48.   Serial.begin(9600);
  49.   pinMode(buzzer,OUTPUT);
  50.   pinMode(greenLED,OUTPUT);
  51.   pinMode(redLED,OUTPUT);
  52.  
  53.   // INA219 init
  54.   if (!ina219.begin()) {
  55.     Serial.println("Failed to find INA219 chip");
  56.     while (1);
  57.   }
  58.   ina219.setCalibration_32V_2A();
  59.   Serial.println("INA219 ready!");
  60.  
  61.   // OLED init
  62.   if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // 0x3C is default I2C addr
  63.     Serial.println("SSD1306 allocation failed");
  64.     while (1);
  65.   }
  66.   display.clearDisplay();
  67.   display.setTextSize(2);      // Normal 1:1 pixel scale
  68.   display.setTextColor(SSD1306_WHITE);
  69.   display.setCursor(0,0);
  70.   display.println("OLED Ready");
  71.   display.display();
  72.   delay(1000);
  73. }
  74.  
  75. void loop() {
  76.   float busVoltage = ina219.getBusVoltage_V();      // Voltage across load
  77.   float current_mA = ina219.getCurrent_mA();        // Current draw
  78.   float power_mW = ina219.getPower_mW();            // Power consumption
  79.  
  80.   int pct = batteryPercent(busVoltage);
  81.  
  82.   // --- Serial output (3 decimal places) ---
  83.   Serial.print("Bus Voltage: "); Serial.print(busVoltage, 3); Serial.println(" V");
  84.   Serial.print("Current: "); Serial.print(current_mA, 3); Serial.println(" mA");
  85.   Serial.print("Power: "); Serial.print(power_mW/1000.0, 3); Serial.println(" W");
  86.   Serial.print("Battery %: "); Serial.print(pct); Serial.println("%");
  87.   if (pct <= 5) {
  88.     Serial.println("⚠️ WARNING: Battery low!");
  89.     digitalWrite(redLED, HIGH);
  90.     digitalWrite(greenLED, LOW);
  91.     digitalWrite(buzzer,!digitalRead(buzzer));
  92.  
  93.   }else{
  94.     digitalWrite(greenLED, HIGH);
  95.     digitalWrite(redLED, LOW);
  96.     digitalWrite(buzzer, LOW);
  97.  
  98.   }
  99.   Serial.println("-----------------------------");
  100.  
  101.   // --- OLED output ---
  102.   display.clearDisplay();
  103.   display.setCursor(0,0);
  104.   display.setTextSize(2);
  105.   display.print("V: "); display.print(busVoltage, 2); display.println(" V");
  106.   display.print("I: "); display.print(current_mA, 1); display.println(" mA");
  107.   display.print("P: "); display.print(power_mW, 1); display.println(" mW");
  108.  
  109.   // ✅ Battery line
  110.   display.fillRect(0, 48, 128, 16, SSD1306_BLACK);
  111.   display.setCursor(0,48);
  112.   char buffer[12];
  113.   sprintf(buffer, "Batt:%3d%%", pct);
  114.   display.print(buffer);
  115.  
  116.   // ✅ Trigger low battery by percentage threshold
  117.   if (pct <= 5) {
  118.     display.clearDisplay();
  119.     display.setTextSize(2);
  120.     display.setCursor(52,1);
  121.     display.print("LOW");
  122.     display.setCursor(28,32);
  123.     display.print("BATTERY!");
  124.   }
  125.   display.display();
  126.  
  127.   delay(1000);
  128. }
  129.  
  130. int batteryPercent(float vbat) {
  131.   if (vbat <= VBAT_MIN) return 0;
  132.   if (vbat >= VBAT_MAX) return 100;
  133.   return (int)((vbat - VBAT_MIN) * 100.0 / (VBAT_MAX - VBAT_MIN));
  134. }
  135.  
Tags: ina219
Advertisement
Add Comment
Please, Sign In to add comment