Advertisement
pleasedontcode

OLED Display rev_01

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