Printing Decimal Numbers on SSD1306 OLED Display Using the SSD1306xLED Library

Tinusaur SSD1306XLED SSD1306 OLED Llibary

After playing for awhile with that SSD1306 OLED display I decided to add few more things to the SSD1306xLED library and the ability to print numbers seamed to be an important one.

Tinusaur SSD1306xLED ATtiny85 SSD1306 OLDEThere is already a function in the library that outputs strings so I needed only the conversion from int to decimal string. So I used another function usint2decascii that I previously wrote for another project OWOWOD which code in turn I borrowed from a third project LCDDDD – a LCD Direct Drawing Driver for PCD8544 based displays such as Nokia 3310 LCD. The weird LCDDDD name comes from the fact that it outputs the data directly to the LCD instead of storing it into a buffer first and then periodically outputting it to the LCD – this is unlike most of the popular LCD drivers.

Here is the main function definition …

uint8_t usint2decascii(uint16_t num, char* buffer)

The function requires a small buffer to store the result. Since the largest number is 65535 – that is 0xFFFF in hex, 5+1 bytes are needed for that buffer.

For convenience there are 2 functions for direct printing of numbers. Below is their implementation – it’s very simple:


char ssd1306_numdec_buffer[USINT2DECASCII_MAX_DIGITS + 1];

void ssd1306_numdec_font6x8(uint16_t num) {
  ssd1306_numdec_buffer[USINT2DECASCII_MAX_DIGITS] = '\0';
  uint8_t digits = usint2decascii(num, ssd1306_numdec_buffer);
  ssd1306_string_font6x8(ssd1306_numdec_buffer + digits);

void ssd1306_numdecp_font6x8(uint16_t num) {
  ssd1306_numdec_buffer[USINT2DECASCII_MAX_DIGITS] = '\0';
  usint2decascii(num, ssd1306_numdec_buffer);

The ssd1306_numdec_font6x8 only prints the number while ssd1306_numdecp_font6x8 prints numbers the same way but right-aligned and 5-digit padded.

Printing numbers is as simple as this …

  ssd1306_setpos(20, 4);

Here is a little more complicated example …

ssd1306_setpos(40, 3);
ssd1306_numdecp_font6x8(0xFA32); // dec: 64050
ssd1306_setpos(40, 4);
ssd1306_numdecp_font6x8(0x05CD); // dec: 1485

ssd1306xled sample screen

It prints “a=”, “b=” and then their values. Both numbers are right-aligned and left-padded with up to 4 spaces.

The latest test program in SSD1306xLED includes examples of how to use the ssd1306_numdec_font6x8 and the ssd1306_numdecp_font6x8 functions.

The SSD1306xLED library is at SSD1306xLED page.

Source code of the SSD1306xLED is available at

Source code of the TinyAVRLib is available at