30 Days Lost in Space → Help Center

Coding Concepts

Working with Strings on Displays

Getting numbers and text to show up correctly on your OLED or 7-segment display. How to convert variables to text, add leading zeros, format sensor readings, and avoid the gotchas that make your display show garbage.

The Problem

You have a variable with a temperature reading, a countdown, or a score. You want it on the display. But the display does not speak the same language your variable does. The TM1637 wants integers. The OLED wants C-style strings or Arduino Strings. This article is about getting from "I have a number" to "it looks right on the screen."

Two Kinds of Strings

String (capital S)

Arduino's built-in String object. You can glue pieces together with +, and it grows automatically.

String message = "Temp: ";
message = message + 72;      // "Temp: 72"
message = message + " F";    // "Temp: 72 F"

For most projects in this course, String works fine.

char arrays (C-style strings)

Text stored as an array of characters ending with a null character '\0'. You manage the memory yourself.

char message[20] = "Temp: ";

You will see char arrays when working with u8g2.drawStr(), which requires a const char*, not a String.

Converting Between Them

// String to char array (for drawStr)
String myString = "Hello";
u8g2.drawStr(0, 20, myString.c_str());

// char array to String
char myChars[] = "Hello";
String myString = String(myChars);

Converting Numbers to Strings

Using String()

int score = 42;
String text = String(score);          // "42"

float voltage = 3.14;
String text2 = String(voltage, 2);    // "3.14" (2 decimal places)
String text3 = String(voltage, 1);    // "3.1"

Using dtostrf() for char arrays

When you need a char array (for drawStr()):

float temp = 23.7;
char buffer[10];
dtostrf(temp, 5, 1, buffer);  // " 23.7" (5 chars wide, 1 decimal)

Leading Zeros

Clocks and timers need leading zeros. The number 5 should display as "05."

The Simple Check

String formatTime(int hours, int minutes) {
  String h = (hours < 10)   ? "0" + String(hours)   : String(hours);
  String m = (minutes < 10) ? "0" + String(minutes) : String(minutes);
  return h + ":" + m;
}
// formatTime(9, 5) returns "09:05"

Using snprintf()

snprintf() is the C way. More powerful, one line:

char buffer[6];
snprintf(buffer, sizeof(buffer), "%02d:%02d", hours, minutes);
// buffer: "09:05"

Breaking down %02d: pad with zeros, minimum 2 characters wide, integer.

| Format | Input | Result | |--------|-------|--------| | %d | 42 | "42" | | %3d | 5 | " 5" | | %03d | 5 | "005" | | %02d:%02d | 9, 5 | "09:05" |

Important: snprintf() on Arduino does NOT support %f for floats. Use dtostrf() first, then insert the result.

TM1637 is Different

The TM1637 4-digit display does NOT use strings. It takes integers.

display.showNumberDec(42);         // Shows "  42"
display.showNumberDec(42, true);   // Shows "0042" (leading zeros)

For clock display, combine hours and minutes into one integer:

int hours = 9;
int minutes = 30;
int timeValue = hours * 100 + minutes;  // 930
display.showNumberDecEx(timeValue, 0b01000000, true);  // "09:30"

Do not try to pass a String to the TM1637. It does not have a print() method.

OLED Alignment Tips

On the OLED, text position is in pixels, not characters. A number jumping between 9 and 10 will shift everything after it.

Fixed-Width with snprintf()

char line[20];
snprintf(line, sizeof(line), "Score: %4d", score);
// "Score:    7" (always 4 characters for the number)

Remember: y is the baseline

The y-coordinate in drawStr() is the baseline of the text, not the top. If your font is 10 pixels tall, you need y >= 10 to see the first line.

u8g2.drawStr(0, 0, "Hello");   // Probably invisible! y=0 is above baseline
u8g2.drawStr(0, 12, "Hello");  // This shows up

Common Mistakes

| Mistake | What Happens | Fix | |---------|-------------|-----| | Passing String to drawStr() | Compiler error | Use .c_str() | | Concatenating number without converting | Random character instead of number | Use String(score) | | Buffer too small for snprintf | Text gets cut off | Use char buffer[32] as a safe default | | Using %f with snprintf on Arduino | Prints "?" or blank | Use dtostrf() for floats | | Forgetting null terminator in char arrays | Garbage after your text | Always size arrays with room for '\0' |