Using the u8x8 library with Arduino Nano

Thread Starter

TonyAm

Joined Oct 12, 2022
83
Trying to figure out how to update text to a 128x64 OLED using the u8x8 library for Arduino. The text changes in real time using a potentiometer.


C-like:
void setup() {

  u8x8.begin();

  u8x8.setPowerSave(0);

}


//* Writing to the LCD*//

void LCDWrite()

{

  //setFont(u8x8_font_chroma48medium8_r);

  u8x8.setFont(u8x8_font_px437wyse700a_2x2_r);

  u8x8.drawString(0,1,"PHASE3");

  u8x8.setInverseFont(1);

  //   // *Display results of the keySelect() to the Oled

  //   // There are 12 integers returned from keySelect(), numbered 0-11.

  //   // Convert the 12 integer value of keySelect() to correspond to each key in music notation

  char *keys[] = {"Am/C", "A#m/C#", "Bm/D", "Cm/D#", "C#m/E", "Dm/F", "D#m/F#", "Em/G", "Fm/G#", "F#m/A", "Gm/A#", "G#m/B"};


  u8x8.drawString(0,3,keys[keyPrint]);

  delay(20);

  u8x8.drawString(0,3,"      "); //this causes too much blinking
The above solution causes too much blinking, thought maybe there was a better way to update values to the OLED in real time without using this method.

Sincere thanks for any help,
TonyAm
 
Last edited by a moderator:

crugorocks

Joined May 1, 2025
31
Only update the display when the content actually changes. Edit your code like this:
Code:
#include <U8x8lib.h>

U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);

int lastKey = -1;

void setup() {
  u8x8.begin();
  u8x8.setPowerSave(0);
  u8x8.setFont(u8x8_font_px437wyse700a_2x2_r);
  u8x8.drawString(0, 1, "PHASE3");
}

void loop() {
  int potValue = analogRead(A0);
  int keyPrint = map(potValue, 0, 1023, 0, 11);

  char *keys[] = {"Am/C", "A#m/C#", "Bm/D", "Cm/D#", "C#m/E", "Dm/F",
                  "D#m/F#", "Em/G", "Fm/G#", "F#m/A", "Gm/A#", "G#m/B"};

  // Only update the display if the value changed
  if (keyPrint != lastKey) {
    lastKey = keyPrint;

    // Clear only the characters we expect to change (e.g., 9 max chars wide)
    u8x8.drawString(0, 3, "         "); // 9 spaces = wide enough to clear old string
    u8x8.drawString(0, 3, keys[keyPrint]);
  }

  delay(50); // You can adjust this for faster/slower response
}
 

Thread Starter

TonyAm

Joined Oct 12, 2022
83
Only update the display when the content actually changes. Edit your code like this:
Code:
#include <U8x8lib.h>

U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);

int lastKey = -1;

void setup() {
  u8x8.begin();
  u8x8.setPowerSave(0);
  u8x8.setFont(u8x8_font_px437wyse700a_2x2_r);
  u8x8.drawString(0, 1, "PHASE3");
}

void loop() {
  int potValue = analogRead(A0);
  int keyPrint = map(potValue, 0, 1023, 0, 11);

  char *keys[] = {"Am/C", "A#m/C#", "Bm/D", "Cm/D#", "C#m/E", "Dm/F",
                  "D#m/F#", "Em/G", "Fm/G#", "F#m/A", "Gm/A#", "G#m/B"};

  // Only update the display if the value changed
  if (keyPrint != lastKey) {
    lastKey = keyPrint;

    // Clear only the characters we expect to change (e.g., 9 max chars wide)
    u8x8.drawString(0, 3, "         "); // 9 spaces = wide enough to clear old string
    u8x8.drawString(0, 3, keys[keyPrint]);
  }

  delay(50); // You can adjust this for faster/slower response
}
Excellent, sincere thanks.

TonyAm
 

Thread Starter

TonyAm

Joined Oct 12, 2022
83
Update: Tried the code, which I really appreciate since it showed me a new way of going about using "edge detection", or updating values only when they've changed, as it applies to an OLED display.

Code:
    // *Display results of the keySelect() to the Oled
    // There are 12 integers returned from keySelect(), numbered 0-11.
    // Convert the 12 integer value of keySelect() to correspond to each key in music notation
  char *keys[] = {"Am/C", "A#m/C#", "Bm/D", "Cm/D#", "C#m/E", "Dm/F", "D#m/F#", "Em/G", "Fm/G#", "F#m/A", "Gm/A#", "G#m/B"};
    //since keySelect() returns in int from 0-11
  //we can use it to select a char that corresponds to the musical key in the array.
  uint8_t keyPrint = keySelect(); //keySelect() is a function that just returns the value of a potentiometer
  int lastKeyVal = -1;

  // Only update the display if the value changed
  if (keyPrint != lastKeyVal) {
    lastKeyVal = keyPrint;

    // Clear only the characters we expect to change (e.g., 9 max chars wide)
    u8x8.drawString(0, 3, "         "); // 9 spaces = wide enough to clear old string
    u8x8.drawString(0, 3, keys[keyPrint]);
  }

  delay(10); // You can adjust this for faster/slower response
//*I started at delay(50), then down to delay(10) Still a lot of flickering of the text.

I think I'm just going to switch to using an OLED with the 1306 driver chip, since there are many straight forward (easy) examples around such as the Adafruit examples with their library.

I believe there must be an easier way to do this with the u8x8 library, a built-in function that has eluded me.
Thanks again for the code example.
 
Top