Need demo codes for 0.42 OLED

I bought these 3 boards from Taobao.com.
All came with working demos (all displays are OK, showing working demos.)
However, when I try to write my own code, I can’t find any examples that can work with the 0.42 OLED. I don’t know if I should be using I2C or SPI either, or which pins to use.
Any demo codes available ? Many thanks.

https://github.com/01Space/ESP32-S3-0.42OLED

Did you try this link?

Pick an appropriate library and it will have examples. Try searching the library manager on OLED

Yes, I’ve tried the GraphicsTest.ino.

First, I tried to identify the ESP chip model.

I tried running the GraphicsTest but kept getting I2C errors.

Then, I ran an I2C Scanner and found nothing.

So, I am suspecting it’s actually using SPI and not I2C.
There’s no LED on board either, so I guess it’s not one of the popular boards that do have onboard LED.

Try these I2C pins , I found.
First try the scanner to get the adress.

I’ve tried like a hundred times. SPI/IIC, different PIN combinations. Finally, …. it works !!!
Found these 3 chips on sale in AliExpress. One comment says SCL GPIO0, SDA GPIO1 !
Yes! Yes! It works !!
Thanks, everyone !

1 Like

Found the pins for the ESP32-S3-0.42 OLED too.

1 ESP32-C6-0.42 SDA-1, SDL-0 OK
2 ESP32-S3-0.42 SDA-48, SDL-47 OK
3 RP2350-0.42 SDA-?, SDL-? Not yet

Still need to find the pins for the RP2350. It has a working MicroPython installed, which I’ll have to erase before doing any testing.

1 Like

1 ESP32-C6-0.42 SDA-1, SDL-0 OK
2 ESP32-S3-0.42 SDA-48, SDL-47 OK
3 RP2350-0.42 SDA-18, SDL-19 Not tested yet

OK, solved the final mystery.
I’ve used Thonny to connect to the onboard MicroPython, and found a main.py on it. It shows the SDA and SCL pins used.
All done. Thanks, and hope this information is useful to others.

1 Like

If any Uno/Nano users want 0.42" (OLED with I2C) code using u8g2 library, here is a handful of native u8g2 examples...

// https://www.youtube.com/watch?v=caHcaUoQ2kg // arduino with oled 0.42
// https://www.youtube.com/watch?v=icD-hER8YQ8 // esp32 with oled 0.42

#define CASES 8

#define SCREEN_WIDTH 72
#define SCREEN_HEIGHT 40
#define CHAR_HEIGHT 11
#define CHAR_WIDTH 6
#define I2C_ADDRESS 0x3C // 0x3D

#define bmap_width 45
#define bmap_height 20
const unsigned char bmap[] PROGMEM = {  // GIMP export to XBM
  0xf0, 0xff, 0x1f, 0xf0, 0xff, 0x01, 0xf0, 0xff, 0x1f, 0xf8, 0xff, 0x07,
  0x00, 0xfe, 0x1f, 0x7c, 0xe0, 0x0f, 0x78, 0xf0, 0x3f, 0xbc, 0x07, 0x1f,
  0x78, 0xe0, 0x3d, 0xde, 0x07, 0x1f, 0x78, 0xe0, 0x3d, 0xcf, 0x07, 0x1f,
  0x7c, 0xf0, 0xf9, 0xcf, 0x03, 0x1f, 0x3c, 0xf8, 0xf8, 0xc7, 0x83, 0x0f,
  0x3c, 0x7e, 0xf8, 0xc3, 0xe3, 0x07, 0xfc, 0x3f, 0xf0, 0xe1, 0xff, 0x03,
  0xfe, 0x0f, 0xf0, 0xe0, 0xff, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0xfe, 0xff, 0xff, 0x03, 0x00, 0xf8, 0xff, 0xff, 0xff, 0xff, 0x00,
  0xff, 0xff, 0x00, 0xfe, 0xff, 0x03, 0xff, 0xff, 0x00, 0xfe, 0xff, 0x03,
  0xf8, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x01, 0x00
};

#include <U8g2lib.h>                                          // https://github.com/olikraus/u8g2/wiki
U8G2_SSD1306_72X40_ER_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);  // https://github.com/olikraus/u8g2/wiki/u8g2setupcpp
unsigned long timer, timeout = 5000;
int count, x, xMov = 1, y, yMov = 1, dly = 100;
bool titleflag = 0, colorflag;

void setup(void) {
  randomSeed(analogRead(A0));

  u8g2.setDrawColor(1);               // white
  u8g2.setFontPosTop();               // choices: top, bottom, center, baseline
  u8g2.setFont(u8g2_font_luRS08_tr);  // https://github.com/olikraus/u8g2/wiki/fntlistallplain
  u8g2.setFontMode(1);                // transparent
  u8g2.setFontDirection(0);           // horizontal
  u8g2.clear();                       // clear buffer and update display
  u8g2.begin();
}

void loop(void) {
  switch (count) {  // https://github.com/olikraus/u8g2/wiki/u8g2reference
    case 0: corners(); break;
    case 1: pixel(); break;
    case 2: line(); break;
    case 3: box(); break;
    case 4: frame(); break;
    case 5: circle(); break;
    case 6: triangle(); break;
    case 7: text(); break;
    case 8: bitmap(); break;
    default: break;
  }

  if (millis() - timer > timeout) {  // wait for timeout
    timer = millis();
    if (count++ == CASES) {  // next function
      count = 1;             // reset to first function
    }
    newFrane72x40();  // draw new frame
  }
  u8g2.updateDisplay();
  delay(dly);
}

void newFrane72x40() {
  titleflag = 0;                                      // reset title flag
  u8g2.clearBuffer();                                 // clear  buffer
  u8g2.setDrawColor(1);                               // set color to white
  u8g2.drawFrame(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);  // x, y, w, h - u8g2 puts 72x40 origin at oled (28, 0) on 128x64, 128x32
}

void showTitle(const char title[]) {
  if (titleflag == 0) {    // reset flag
    u8g2.setCursor(2, 1);  // x, y position
    u8g2.print(title);     // display
    titleflag = 1;         // one time only
  }
}

void corners() {
  showTitle("72x40");
  u8g2.drawPixel(0, 0);  // corner pixels
  u8g2.drawPixel(71, 0);
  u8g2.drawPixel(71, 39);
  u8g2.drawPixel(0, 39);
  u8g2.drawPixel((SCREEN_WIDTH / 2) - 1, (SCREEN_HEIGHT / 2) - 1);  // center pixel
  // delay(dly);
}

void pixel() {
  showTitle("drawPixel");
  byte x = random(2, SCREEN_WIDTH - 2), y = random(CHAR_HEIGHT, SCREEN_HEIGHT - 2);
  u8g2.drawPixel(x, y);
  // delay(dly);
}

void line() {
  showTitle("drawLine");
  byte x0 = random(2, SCREEN_WIDTH - 2), y0 = random(CHAR_HEIGHT, SCREEN_HEIGHT - 2);  // first point
  byte x1 = random(2, SCREEN_WIDTH - 2), y1 = random(CHAR_HEIGHT, SCREEN_HEIGHT - 2);  // second point
  u8g2.drawLine(x0, y0, x1, y1);
  // delay(dly);
}

void box() {
  showTitle("drawBox");
  colorflag = !colorflag;  // alternate black and white
  u8g2.setDrawColor(colorflag);
  byte w = random(1, SCREEN_WIDTH - 4), h = random(CHAR_HEIGHT, SCREEN_HEIGHT - CHAR_HEIGHT - 4);  // width, height
  byte x = random(2, SCREEN_WIDTH - w), y = random(CHAR_HEIGHT, SCREEN_HEIGHT - h);                // origin
  u8g2.drawBox(x, y, w, h);
  // delay(dly);
}

void frame() {
  showTitle("drawFrame");
  colorflag = !colorflag;  // alternate black and white
  u8g2.setDrawColor(colorflag);
  int w = random(1, SCREEN_WIDTH - 4), h = random(CHAR_HEIGHT, SCREEN_HEIGHT - CHAR_HEIGHT - 4);  // width, height
  int x = random(2, SCREEN_WIDTH - w), y = random(CHAR_HEIGHT, SCREEN_HEIGHT - h);                // origin
  u8g2.drawFrame(x, y, w, h);
  // delay(dly);
}

void circle() {
  showTitle("drawCircle");
  colorflag = !colorflag;  // alternate black and white
  u8g2.setDrawColor(colorflag);
  int r = random(2, (SCREEN_HEIGHT - CHAR_HEIGHT) / 2);                                 // radius
  int x = random(r, SCREEN_WIDTH - r), y = random(CHAR_HEIGHT + r, SCREEN_HEIGHT - r);  // origin
  u8g2.drawCircle(x, y, r);
  // delay(dly);
}

void triangle() {
  showTitle("drawTriangle");
  colorflag = !colorflag;  // alternate black and white
  u8g2.setDrawColor(colorflag);
  int x0 = random(2, SCREEN_WIDTH - 2), y0 = random(CHAR_HEIGHT, SCREEN_HEIGHT - 2);  // point 1
  int x1 = random(2, SCREEN_WIDTH - 2), y1 = random(CHAR_HEIGHT, SCREEN_HEIGHT - 2);  // point 2
  int x2 = random(2, SCREEN_WIDTH - 2), y2 = random(CHAR_HEIGHT, SCREEN_HEIGHT - 2);  // point 3
  u8g2.drawTriangle(x0, y0, x1, y1, x2, y2);
  // delay(dly);
}

void text() {
  // int width - u8g2, getMaxCharWidth*(); // text horizontal placement
  // int height = u8g2.getMaxCharHeight(); // text vertical placement

  u8g2.setFontDirection(0);  // horizontal
  u8g2.setCursor(18, 2);
  u8g2.print("NORTH");

  u8g2.setFontDirection(1);  // top-down
  u8g2.setCursor(69, 8);
  u8g2.print("EAST");

  u8g2.setFontDirection(2);  // upside-down
  u8g2.setCursor(55, 37);
  u8g2.print("SOUTH");

  u8g2.setFontDirection(3);  // button-up
  u8g2.setCursor(2, 33);
  u8g2.print("WEST");

  u8g2.setFontDirection(0); // reset to horizontal
}

void bitmap() {
  if (x < 0 || x >= SCREEN_WIDTH - bmap_width)  // x boundries
    xMov = -xMov;
  if (y < 0 || y >= SCREEN_HEIGHT - bmap_height)  // y boundries
    yMov = -yMov;
  x += xMov;  // update x
  y += yMov;  // update y
  u8g2.clearBuffer();
  u8g2.drawXBMP(x, y, bmap_width, bmap_height, bmap);
  u8g2.sendBuffer();
}
1 Like