pleasedontcode

# OLED Display rev_02

Mar 6th, 2026 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: OLED_DISPLAY
  13.     - Version: 002
  14.     - Source Code compiled for: ESP32 DevKit V1
  15.     - Source Code created on: 2026-03-07 04:58:59
  16.  
  17. ********* Pleasedontcode.com **********/
  18.  
  19. /****** SYSTEM REQUIREMENTS *****/
  20. /****** SYSTEM REQUIREMENT 1 *****/
  21.     /* Display "Hello World" on OLED at startup, then */
  22.     /* increment a counter every second and display it on */
  23.     /* the screen */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26.  
  27.  
  28. /* START CODE */
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <Wire.h>
  32. #include <Adafruit_SSD1306.h>   //https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
  33. #include <U8g2_for_Adafruit_GFX.h>  //https://github.com/olikraus/U8g2_for_Adafruit_GFX
  34. #include <Adafruit_SSD1306.h>   //https://github.com/adafruit/Adafruit_SSD1306
  35. #include <Adafruit_GFX.h>   //https://github.com/adafruit/Adafruit-GFX-Library
  36.  
  37. /****** FUNCTION PROTOTYPES *****/
  38. void setup(void);
  39. void loop(void);
  40.  
  41. /***** DEFINITION OF I2C PINS *****/
  42. const uint8_t OledDisplay_SSD1306OledDisplay_I2C_PIN_SDA_D21        = 21;
  43. const uint8_t OledDisplay_SSD1306OledDisplay_I2C_PIN_SCL_D22        = 22;
  44. const uint8_t OledDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS      = 60;
  45.  
  46. /****** DEFINITION OF OLED DISPLAY CONSTANTS *****/
  47. #define SCREEN_WIDTH 128        // OLED display width in pixels
  48. #define SCREEN_HEIGHT 64        // OLED display height in pixels
  49. #define OLED_RESET -1           // Reset pin (not used, set to -1)
  50.  
  51. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  52. // Create Adafruit SSD1306 display object using I2C
  53. // The address is 0x3C (60 in decimal) as specified in the constant
  54. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  55.  
  56. // Create U8g2_for_Adafruit_GFX object for enhanced font rendering
  57. U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
  58.  
  59. /****** GLOBAL VARIABLES *****/
  60. unsigned long lastUpdateTime = 0;  // Stores the last time counter was updated
  61. uint32_t counter = 0;               // Counter that increments every second
  62.  
  63. void setup(void)
  64. {
  65.     // Initialize Serial communication for debugging
  66.     Serial.begin(115200);
  67.     delay(100);
  68.     Serial.println("\n\nESP32 OLED Display Test Starting...");
  69.  
  70.     // Initialize I2C communication with custom pins
  71.     Wire.begin(OledDisplay_SSD1306OledDisplay_I2C_PIN_SDA_D21,
  72.                OledDisplay_SSD1306OledDisplay_I2C_PIN_SCL_D22);
  73.     delay(100);
  74.  
  75.     // Initialize the OLED display with the I2C address 0x3C
  76.     if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
  77.         Serial.println(F("SSD1306 allocation failed"));
  78.         // Hang in an infinite loop if display fails to initialize
  79.         while (1) {
  80.             delay(1000);
  81.         }
  82.     }
  83.  
  84.     // Clear the display buffer
  85.     display.clearDisplay();
  86.  
  87.     // Initialize U8g2 font rendering
  88.     u8g2_for_adafruit_gfx.begin(display);
  89.  
  90.     // Display "Hello World" at startup
  91.     display.clearDisplay();
  92.     display.setTextSize(1);
  93.     display.setTextColor(SSD1306_WHITE);
  94.     display.setCursor(0, 0);
  95.     display.println(F("Hello World"));
  96.     display.display();
  97.  
  98.     // Initialize the last update time
  99.     lastUpdateTime = millis();
  100.  
  101.     Serial.println("Setup complete. Display initialized.");
  102. }
  103.  
  104. void loop(void)
  105. {
  106.     // Get the current time in milliseconds
  107.     unsigned long currentTime = millis();
  108.  
  109.     // Check if one second (1000 milliseconds) has passed since the last update
  110.     if (currentTime - lastUpdateTime >= 1000) {
  111.         // Update the last update time
  112.         lastUpdateTime = currentTime;
  113.  
  114.         // Increment the counter
  115.         counter++;
  116.  
  117.         // Clear the display buffer
  118.         display.clearDisplay();
  119.  
  120.         // Display "Hello World" on the first line
  121.         display.setTextSize(1);
  122.         display.setTextColor(SSD1306_WHITE);
  123.         display.setCursor(0, 0);
  124.         display.println(F("Hello World"));
  125.  
  126.         // Display the counter value on the second line
  127.         display.setCursor(0, 16);
  128.         display.print(F("Counter: "));
  129.         display.println(counter);
  130.  
  131.         // Update the display
  132.         display.display();
  133.  
  134.         // Print counter value to Serial for debugging
  135.         Serial.print("Counter: ");
  136.         Serial.println(counter);
  137.     }
  138.  
  139.     // Small delay to prevent overwhelming the processor
  140.     delay(10);
  141. }
  142.  
  143. /* END CODE */
  144.  
Advertisement
Add Comment
Please, Sign In to add comment