Advertisement
pleasedontcode

OLED Display rev_02

Aug 25th, 2024
112
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.     - Source Code compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-08-25 14:34:30
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Display "Hello, World!" on the SSD1306 OLED using */
  21.     /* I2C communication with SDA on pin D21 and SCL on */
  22.     /* pin D22, ensuring proper initialization of the */
  23.     /* Adafruit SSD1306 library. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <Adafruit_SSD1306.h>  // https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
  30. #include <U8g2_for_Adafruit_GFX.h>  // https://github.com/olikraus/U8g2_for_Adafruit_GFX
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35.  
  36. /***** DEFINITION OF I2C PINS *****/
  37. const uint8_t display_SSD1306OledDisplay_I2C_PIN_SDA_D21 = 21; // SDA pin
  38. const uint8_t display_SSD1306OledDisplay_I2C_PIN_SCL_D22 = 22; // SCL pin
  39. const uint8_t display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 0x3C; // I2C address (0x3C is common for SSD1306)
  40.  
  41. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  42. // Create an instance of the display object
  43. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  44. #define SCREEN_HEIGHT 64  // OLED display height, in pixels
  45. #define OLED_RESET    -1  // Reset pin # (or -1 if sharing Arduino reset pin)
  46. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); // Initialize display with I2C
  47.  
  48. // Create an instance for U8g2 using the Adafruit display
  49. U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx; // Create an instance for U8g2
  50.  
  51. void setup(void)
  52. {
  53.     // Initialize serial communication for debugging
  54.     Serial.begin(9600);
  55.    
  56.     // Initialize the display with I2C address
  57.     display.begin(SSD1306_SWITCHCAPVCC, display_SSD1306OledDisplay_I2C_SLAVE_ADDRESS);
  58.     display.clearDisplay(); // Clear the display buffer
  59.  
  60.     // Connect u8g2 procedures to Adafruit GFX
  61.     u8g2_for_adafruit_gfx.begin(display);
  62.    
  63.     // Set up the display
  64.     display.setTextSize(1); // Set text size
  65.     display.setTextColor(SSD1306_WHITE); // Set text color
  66.     display.setCursor(0, 0); // Set cursor position
  67.     display.println(F("Hello, World!")); // Print message
  68.     display.display(); // Refresh the display
  69.     delay(2000); // Delay for 2 seconds
  70. }
  71.  
  72. void loop(void)
  73. {
  74.     // Main code to run repeatedly
  75.     display.clearDisplay(); // Clear the display buffer
  76.     display.setCursor(0, 0); // Set cursor position
  77.     display.println(F("ESP32 with OLED")); // Print message
  78.     display.display(); // Refresh the display
  79.     delay(2000); // Delay for 2 seconds
  80. }
  81.  
  82. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement