Last active
December 26, 2025 07:56
-
-
Save notthetup/b0314a3dd817291fc8df8e25c9aeabf0 to your computer and use it in GitHub Desktop.
INA228 Display Xiao
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <Arduino.h> | |
| #include <Wire.h> | |
| #include <U8g2lib.h> | |
| #include <Adafruit_INA228.h> | |
| // ===================== | |
| // OLED (128x64 SSD1306) | |
| // ===================== | |
| U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2( | |
| U8G2_R0, | |
| /* reset = */ U8X8_PIN_NONE | |
| ); | |
| // ===================== | |
| // Config | |
| // ===================== | |
| #define UPDATE_INTERVAL_MS 1000 // ms | |
| // ===================== | |
| // INA228 | |
| // ===================== | |
| Adafruit_INA228 ina228; | |
| uint32_t lastUpdate = 0; | |
| bool sensorOK = false; | |
| // ===================== | |
| // Helpers | |
| // ===================== | |
| void drawError(const char *msg) { | |
| u8g2.clearBuffer(); | |
| u8g2.setFont(u8g2_font_7x14_tf); | |
| u8g2.drawStr(0, 24, "ERROR:"); | |
| u8g2.drawStr(0, 44, msg); | |
| u8g2.sendBuffer(); | |
| } | |
| void drawMeasurements(float voltage, float current_mA) { | |
| char buf[20]; | |
| u8g2.clearBuffer(); | |
| u8g2.setFlipMode(1); | |
| u8g2.setFont(u8g2_font_logisoso18_tf); | |
| // Voltage (top) | |
| snprintf(buf, sizeof(buf), "%.2f V", voltage); | |
| u8g2.drawStr(0, 26, buf); | |
| // Current (bottom) | |
| if (current_mA >= 1000.0f) { | |
| snprintf(buf, sizeof(buf), "%.2f A", current_mA / 1000.0f); | |
| } else { | |
| snprintf(buf, sizeof(buf), "%.0f mA", current_mA); | |
| } | |
| u8g2.drawStr(0, 56, buf); | |
| u8g2.sendBuffer(); | |
| } | |
| // ===================== | |
| // Setup | |
| // ===================== | |
| void setup() { | |
| Serial.begin(115200); | |
| delay(2000); | |
| // I2C (required on ESP32-C3) | |
| // Wire.begin(SDA, SCL); | |
| // OLED | |
| u8g2.begin(); | |
| u8g2.clearBuffer(); | |
| u8g2.setFont(u8g2_font_7x14_tf); | |
| u8g2.drawStr(0, 24, "Initializing..."); | |
| u8g2.sendBuffer(); | |
| // INA228 | |
| if (!ina228.begin()) { | |
| Serial.println("INA228 not found"); | |
| drawError("INA228 missing"); | |
| sensorOK = false; | |
| return; | |
| } | |
| ina228.setAveragingCount(INA228_COUNT_1024); | |
| ina228.setShunt(0.015, 10.0); | |
| sensorOK = true; | |
| Serial.println("INA228 initialized"); | |
| u8g2.clearBuffer(); | |
| u8g2.drawStr(0, 24, "INA228 Ready"); | |
| u8g2.sendBuffer(); | |
| } | |
| // ===================== | |
| // Loop | |
| // ===================== | |
| void loop() { | |
| if (millis() - lastUpdate < UPDATE_INTERVAL_MS) { | |
| return; | |
| } | |
| lastUpdate = millis(); | |
| if (!sensorOK) { | |
| return; | |
| } | |
| float voltage = ina228.readBusVoltage(); // volts | |
| float current_mA = ina228.readCurrent(); // milliAmps | |
| Serial.printf("V=%.3f V I=%.1f mA\n", voltage, current_mA); | |
| drawMeasurements(voltage, current_mA); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment