#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// OLED display configuration
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define SCREEN_I2C_ADDRESS 0x3C
#define OLED_RESET_PIN -1

Adafruit_SSD1306 screen(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET_PIN);

// GPIO configuration
#define CH 4 // Define GPIO 4

// Declare the bitmap globally
static const unsigned char PROGMEM image_battery_25_bits[] = {0x0f,0xff,0xfe,0x10,0x00,0x01,0x10,0x00,0x3d,0x70,0x00,0x3d,0x80,0x00,0x3d,0x80,0x00,0x3d,0x80,0x00,0x3d,0x80,0x00,0x3d,0x80,0x00,0x3d,0x70,0x00,0x3d,0x10,0x00,0x3d,0x10,0x00,0x01,0x0f,0xff,0xfe};

void setup() {
  Serial.begin(115200);

  // Initialize the OLED display
  if (!screen.begin(SSD1306_SWITCHCAPVCC, SCREEN_I2C_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Stop here if initialization fails
  }

  screen.clearDisplay();
  screen.display(); // Ensure the screen starts blank

  // Configure GPIO 7 as input with internal pull-down resistor
  pinMode(CH, INPUT_PULLDOWN);

  Serial.println(F("OLED and GPIO initialized successfully with pull-down resistor"));
}

void loop() {
  // Read the state of GPIO 7
  int gpioState = digitalRead(CH);

  // Clear the display
  screen.clearDisplay();

  // Display the bitmap if GPIO 7 is high
  if (gpioState == HIGH) {
    screen.drawBitmap(103, 0, image_battery_25_bits, 24, 16, 1);
  }

  // Update the display
  screen.display();

  delay(100); // Short delay for responsiveness
}
Loading
xiao-esp32-c3
Loading
ssd1306
r1:1
r1:2
sw1:1
sw1:2
sw1:3