128x64 SSD1306 OLED Display Module + Arduino


To use a 64x48 OLED display module with an Arduino and the U8glib library, you'll need to ensure the connections are correct, the U8glib library is installed, and you understand the basic programming concepts for displaying content on the OLED. The key is to understand the display's resolution (64x48) and how to interact with it using U8glib functions. 

1. Wiring:

Connect the OLED's GND pin to the Arduino's GND. 

Connect the OLED's VCC pin to the Arduino's 5V. 

Connect the OLED's SDA pin to the Arduino's A4 pin. 

Connect the OLED's SCL pin to the Arduino's A5 pin. 

Some OLEDs might have a RESET or DC pin, connect them to appropriate Arduino pins if needed, otherwise, leave them unconnected. 

2. U8g2 Library:

Install the U8glib library in the Arduino IDE. You can find it in the "Sketch > Include Library > Manage Libraries" menu.

The U8g2 library supports various OLED display controllers, including those commonly used in 128x64 OLED modules. 

3. Programming:

Include the library:

#include <U8g2lib.h>. 

Instantiate the U8g2lib object:

Use a constructor like U8G_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G_R0, 8, 8, 8, U8X8_PIN_NONE); (adjust the parameters according to your display type and wiring). Note that if you are using a 64x48 OLED, you may need to adjust the constructor to match the display dimensions (e.g., U8G_SSD1306_64X48_NONAME_F_SW_I2C u8g(U8G2_R0, 8, 8, 8, U8X8_PIN_NONE);). 

Display content:

Use U8g2lib functions like u8g.setFont(someFont);, u8g2.setRotation(0); (for portrait orientation) or u8g2.setRotation(1); (for landscape orientation), u8g2.drawStr(x, y, "Text to display");, u8g2.sendBuffer();. 

Consider display size:

When drawing graphics or text, make sure the coordinates and dimensions are within the 64x48 resolution. 


#include <Arduino.h>

#include <U8g2lib.h>


#ifdef U8X8_HAVE_HW_SPI

#include <SPI.h>

#endif

#ifdef U8X8_HAVE_HW_I2C

#include <Wire.h>

#endif


U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display


void setup(void) {

  u8g2.begin();  

}


uint8_t m = 24;


void loop(void) {

  char m_str[3];

  strcpy(m_str, u8x8_u8toa(m, 2)); /* convert m to a string with two digits */

  u8g2.firstPage();

  do {

    u8g2.setFont(u8g2_font_logisoso62_tn);

    u8g2.drawStr(0,63,"9");

    u8g2.drawStr(33,63,":");

    u8g2.drawStr(50,63,m_str);

  } while ( u8g2.nextPage() );

  delay(1000);

  m++;

  if ( m == 60 )

    m = 0;

}


Post a Comment