#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Define OLED display width and height
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Declare the OLED object (I2C Address: 0x3C)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
// Initialize serial communication for debugging
Serial.begin(115200);
// Initialize the OLED display
if (!display.begin(0x3C, SDA, SCL)) { // 0x3C is the default I2C address for many OLEDs
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Infinite loop on failure
}
// Clear the buffer
display.clearDisplay();
// Set text size and color
display.setTextSize(2); // Text size
display.setTextColor(SSD1306_WHITE); // Text color
display.setCursor(0, 0); // Set initial cursor position
display.display(); // Show the initial display
}
void loop() {
for (int i = 0; i < 100; i++) { // Loop through numbers 00 to 99
// Clear the display
display.clearDisplay();
// Format the number as two digits
char formattedNumber[3];
sprintf(formattedNumber, "%02d", i);
// Display the formatted number
display.setCursor(40, 20); // Adjust position for better alignment
display.println(formattedNumber);
// Update the display with the new content
display.display();
delay(500); // Pause for half a second between numbers
}
}
Loading
grove-oled-sh1107
grove-oled-sh1107