Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: OLED_DISPLAY
- - Version: 002
- - Source Code compiled for: ESP32 DevKit V1
- - Source Code created on: 2026-03-07 04:58:59
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Display "Hello World" on OLED at startup, then */
- /* increment a counter every second and display it on */
- /* the screen */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <Adafruit_SSD1306.h> //https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
- #include <U8g2_for_Adafruit_GFX.h> //https://github.com/olikraus/U8g2_for_Adafruit_GFX
- #include <Adafruit_SSD1306.h> //https://github.com/adafruit/Adafruit_SSD1306
- #include <Adafruit_GFX.h> //https://github.com/adafruit/Adafruit-GFX-Library
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF I2C PINS *****/
- const uint8_t OledDisplay_SSD1306OledDisplay_I2C_PIN_SDA_D21 = 21;
- const uint8_t OledDisplay_SSD1306OledDisplay_I2C_PIN_SCL_D22 = 22;
- const uint8_t OledDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 60;
- /****** DEFINITION OF OLED DISPLAY CONSTANTS *****/
- #define SCREEN_WIDTH 128 // OLED display width in pixels
- #define SCREEN_HEIGHT 64 // OLED display height in pixels
- #define OLED_RESET -1 // Reset pin (not used, set to -1)
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // Create Adafruit SSD1306 display object using I2C
- // The address is 0x3C (60 in decimal) as specified in the constant
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- // Create U8g2_for_Adafruit_GFX object for enhanced font rendering
- U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
- /****** GLOBAL VARIABLES *****/
- unsigned long lastUpdateTime = 0; // Stores the last time counter was updated
- uint32_t counter = 0; // Counter that increments every second
- void setup(void)
- {
- // Initialize Serial communication for debugging
- Serial.begin(115200);
- delay(100);
- Serial.println("\n\nESP32 OLED Display Test Starting...");
- // Initialize I2C communication with custom pins
- Wire.begin(OledDisplay_SSD1306OledDisplay_I2C_PIN_SDA_D21,
- OledDisplay_SSD1306OledDisplay_I2C_PIN_SCL_D22);
- delay(100);
- // Initialize the OLED display with the I2C address 0x3C
- if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
- Serial.println(F("SSD1306 allocation failed"));
- // Hang in an infinite loop if display fails to initialize
- while (1) {
- delay(1000);
- }
- }
- // Clear the display buffer
- display.clearDisplay();
- // Initialize U8g2 font rendering
- u8g2_for_adafruit_gfx.begin(display);
- // Display "Hello World" at startup
- display.clearDisplay();
- display.setTextSize(1);
- display.setTextColor(SSD1306_WHITE);
- display.setCursor(0, 0);
- display.println(F("Hello World"));
- display.display();
- // Initialize the last update time
- lastUpdateTime = millis();
- Serial.println("Setup complete. Display initialized.");
- }
- void loop(void)
- {
- // Get the current time in milliseconds
- unsigned long currentTime = millis();
- // Check if one second (1000 milliseconds) has passed since the last update
- if (currentTime - lastUpdateTime >= 1000) {
- // Update the last update time
- lastUpdateTime = currentTime;
- // Increment the counter
- counter++;
- // Clear the display buffer
- display.clearDisplay();
- // Display "Hello World" on the first line
- display.setTextSize(1);
- display.setTextColor(SSD1306_WHITE);
- display.setCursor(0, 0);
- display.println(F("Hello World"));
- // Display the counter value on the second line
- display.setCursor(0, 16);
- display.print(F("Counter: "));
- display.println(counter);
- // Update the display
- display.display();
- // Print counter value to Serial for debugging
- Serial.print("Counter: ");
- Serial.println(counter);
- }
- // Small delay to prevent overwhelming the processor
- delay(10);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment