How to display a smiley face on a 72x40 OLED? | Kastamonu Escortt

How to display a smiley face on a 72x40 OLED?

To display a smiley face on a 72x40 OLED, you need to write a bitmap array of 360 bytes (72x40/8) into the display’s frame buffer via I2C or SPI, then send a display update command. The 0.42 inch 72x40 oled display, typically based on the SSD1306 or SH1106 driver, uses a 72-column by 40-row pixel matrix. Each byte represents 8 vertical pixels in a column, so the smiley face pattern must be manually drawn or generated using a tool like LCD Assistant or a Python script. For example, a simple smiley might occupy columns 12 to 60 and rows 5 to 35, with a 3x3 pixel eye at (20,15) and (50,15), and a 5-pixel wide curved mouth at row 28. The exact byte values depend on how you map the rows; for a 72x40 display, the memory layout is usually column-major with pages of 8 rows. So, page 0 covers rows 0-7, page 1 covers rows 8-15, and so on up to page 4 (rows 32-39). You’ll need to define a 72-byte array for each page, totaling 5 pages. For instance, the eye pixels might be set in page 1 (rows 8-15) and page 2 (rows 16-23), with bits shifted accordingly. A common approach is to use a monochrome bitmap generator that outputs C code; you can then copy the array into your firmware. The display controller’s datasheet specifies that the GDDRAM is 72x40 bits, so you must ensure your data starts at column 0, page 0. After writing the data via I2C (address 0x3C or 0x3D), you send command 0xAF to turn on the display. The smiley face will appear instantly if the contrast is set correctly (command 0x81 followed by a value, typically 0x7F for 128).

Detailed Pixel Mapping for a Smiley Face

Let’s break down the math. A 72x40 OLED has 72 columns (X) and 40 rows (Y). The SSD1306 driver, commonly used in these modules, organizes the GDDRAM into 5 pages (each page = 8 rows). Page 0: rows 0-7, Page 1: rows 8-15, Page 2: rows 16-23, Page 3: rows 24-31, Page 4: rows 32-39. To draw a smiley face, you need to define the head as a circle or ellipse. For a 40-pixel height, a circle with a radius of 15 pixels centered at (36,20) works well. The head spans columns 21 to 51 (center +/- 15) and rows 5 to 35 (center +/- 15). But since rows are grouped in pages, you must calculate which pages contain the head. Rows 5-7 fall in page 0, rows 8-15 in page 1, rows 16-23 in page 2, rows 24-31 in page 3, and rows 32-35 in page 4. For each column, you need to set the appropriate bits in the corresponding page byte. For example, column 21 at page 0 (rows 5-7) might have bits 5,6,7 set (0xE0) if the head starts at row 5. At page 1 (rows 8-15), the same column might have all 8 bits set (0xFF) if the head is solid. At page 4 (rows 32-35), only bits 0-3 might be set (0x0F). The eyes are typically 3x3 pixels. Place the left eye at column 25, row 15 (page 1, rows 8-15, bit 7 for row 15, but careful: bit 0 is row 0, bit 7 is row 7 in a page? Actually, in SSD1306, the LSB corresponds to the lower row number within the page. So for page 1 (rows 8-15), bit 0 = row 8, bit 7 = row 15. So a 3x3 pixel eye at rows 14-16 would span page 1 (rows 14-15, bits 6-7) and page 2 (rows 16-18, bits 0-2). The mouth is a curve. A simple approach is a 5-pixel wide arc at row 28, spanning columns 30 to 42. This falls in page 3 (rows 24-31), so you set bits 4-7 (row 28-31) for columns 30-42, but only for the middle 5 columns? Actually, the mouth might be a 5-pixel high arc, so rows 26-30, which spans page 3 (rows 24-31, bits 2-6). The exact byte values depend on your design. You can use a tool like “OLED bitmap generator” that outputs a 72x40 monochrome bitmap. The tool will generate a 360-byte array. For example, if you draw a smiley face in a 72x40 pixel grid, the tool outputs something like: 0x00, 0x00, 0x3C, 0x42, 0xA5, 0xA5, 0x42, 0x3C, ... (this is for a 8x8 smiley, but scaled). For a 72x40, it’s a massive array. A typical smiley face might have 20% of pixels set (about 576 pixels). The array is sent column by column, page by page. For instance, column 0, page 0 might be 0x00 (no pixels). Column 20, page 0 might be 0xE0 (rows 5-7 set). Column 20, page 1 might be 0xFF (rows 8-15 set). The display controller expects the data in order: first column 0, page 0, then column 1, page 0, up to column 71, page 0, then column 0, page 1, etc. So you need to ensure your array is in that order. If you’re using a library like Adafruit SSD1306, you can use the drawBitmap() function, which takes a 360-byte array. But if you’re writing raw I2C commands, you send the array directly after setting the column and page address. For example, send command 0x21 (set column address) with start column 0, end column 71, then command 0x22 (set page address) with start page 0, end page 4, then send the 360 bytes of data. The display will then show the smiley face.

Hardware and Software Considerations

The 0.42 inch 72x40 oled display module typically operates at 3.3V or 5V, with a current draw of about 20mA when all pixels are on. The I2C interface uses two wires (SDA and SCL) with a pull-up resistor of 4.7kΩ. The I2C address is usually 0x3C (write) or 0x3D (read). Some modules have a jumper to change the address. The display’s resolution is 72x40, which is unusual compared to common 128x64 OLEDs, so you need to ensure your microcontroller’s library supports custom resolutions. For example, the Adafruit SSD1306 library can be modified by changing the SSD1306_LCDWIDTH and SSD1306_LCDHEIGHT defines to 72 and 40. Alternatively, you can write your own driver. The SSD1306 datasheet specifies that the display can be set to a multiplex ratio of 40 (command 0xA8, then 0x27). The segment remap (command 0xA1) and COM scan direction (command 0xC8) might need to be adjusted based on your module’s orientation. For a 72x40 display, the column offset might be 0 (command 0xD3, then 0x00). The display’s charge pump (command 0x8D, then 0x14) must be enabled. The contrast (command 0x81, then value 0x7F) controls brightness. The display’s frame rate is typically 60Hz, but you can adjust it with the clock divide ratio (command 0xD5, then 0x80). The pre-charge period (command 0xD9, then 0x22) and VCOMH deselect level (command 0xDB, then 0x30) affect the display’s quality. For a smiley face, you might want high contrast, so set the contrast to 0xFF for maximum brightness. However, note that the 72x40 OLED has a smaller pixel pitch (0.15mm) compared to larger displays, so the smiley face will appear crisp but small. The viewing angle is 160 degrees, and the response time is 10 microseconds. The display’s lifespan is about 50,000 hours. The module’s dimensions are typically 18mm x 10mm, with a thickness of 1.5mm. It’s ideal for wearable devices, smart cards, or small IoT gadgets. The I2C speed is usually 400kHz, but you can use 100kHz for compatibility. The data transfer time for 360 bytes at 400kHz is about 9ms (360 bytes * 8 bits / 400kHz + overhead). The display update time is under 20ms, so you can animate the smiley face by changing the bitmap array. For example, you can create a sequence of frames: a wink, a big smile, a tongue out. Each frame requires a new 360-byte array. You can store them in flash memory. The microcontroller’s RAM usage is minimal (360 bytes). The power consumption is low, so it’s suitable for battery-powered devices. The display’s driver IC is usually the SSD1306, which has a 1KB GDDRAM (128x64 bits), but only 72x40 bits are used. The unused memory is ignored. Some modules use the SH1106 driver, which has a different command set (e.g., no page addressing, but uses column and row pins). The SH1106 has a 132x64 pixel GDDRAM, so you need to set the column offset to 0. The I2C protocol is the same. The smiley face will look identical on both drivers, but the initialization sequence differs. For example, the SH1106 requires command 0xAF to turn on, and command 0x20 for memory mode. The SSD1306 has more features like horizontal scrolling, which you can use to make the smiley face slide. For instance, you can enable horizontal scroll (command 0x26, then parameters) to move the smiley face left or right. But for a static smiley, just display the bitmap.

Practical Implementation Steps

First, generate the bitmap. Use a tool like “LCD Assistant” (free) or “OLED Image Converter” (online). Set the resolution to 72x40, monochrome, and draw a smiley face. The tool will output a C array. For example, a simple smiley face might look like this (partial):

static const unsigned char smiley [] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0

← Back to Blog