Skip to content
Le Journal · Carnet technique

How to draw shapes on a 1.54 inch 128x64 OLED display?

Auteur

To draw shapes on a 1.54 inch 128x64 OLED display, you need to use a microcontroller like an Arduino or ESP32, communicate via SPI or I2C, and leverage a graphics library such as Adafruit_GFX or U8g2. The display itself is a monochrome OLED with a resolution of 128 pixels horizontally and 64 pixels vertically, driven by a controller like the SSD1306 or SH1106. The key is to initialize the display over SPI, set pixel coordinates, and call functions that draw lines, rectangles, circles, triangles, or custom bitmaps. For example, with Adafruit_GFX, you call display.drawLine(x0, y0, x1, y1, WHITE) to draw a straight line, or display.drawCircle(x, y, radius, WHITE) for a circle. The display’s 128x64 pixel grid means each shape must fit within those bounds—x from 0 to 127, y from 0 to 63. The 1.54 inch 128x64 oled display typically uses SPI for faster data transfer, with a maximum clock speed of around 10 MHz, allowing for smooth shape updates at 30-60 frames per second depending on complexity. The display’s contrast is adjustable via the SSD1306_command(0x81) followed by a value from 0 to 255, with 128 being a common default. Power consumption is low—around 20 mA with all pixels lit, dropping to 0.1 mA in sleep mode—making it ideal for battery-powered projects. The shape-drawing process relies on the frame buffer: the library writes pixel data to a 1KB buffer (128x64 bits = 1024 bytes), then sends it to the display via SPI. Each pixel is either on or off, so shapes are binary, but you can simulate grayscale using dithering patterns, though that requires custom code. The display’s refresh rate is typically 100 Hz, but the actual update speed depends on the microcontroller’s SPI bus speed and the library’s efficiency. For instance, drawing a filled rectangle of 100x50 pixels takes about 500 microseconds on an Arduino Uno at 16 MHz, while a complex circle outline might take 2 milliseconds. The pixel response time is under 10 microseconds, so there’s no ghosting. The display’s viewing angle is >160 degrees, and the brightness is around 100 cd/m², which is sufficient for indoor use. The operating temperature range is -40°C to +85°C, so it works in harsh environments. The SPI interface uses four pins: CS (chip select), DC (data/command), MOSI (master out slave in), and SCK (serial clock), plus a reset pin. The typical wiring on an Arduino Uno is: CS to pin 10, DC to pin 9, MOSI to pin 11, SCK to pin 13, and VCC to 3.3V or 5V (the display has a built-in regulator for 3.3V logic). The I2C variant uses SDA and SCL, but SPI is preferred for shape drawing due to higher throughput. The display’s driver IC, SSD1306, supports both horizontal and vertical addressing modes, which affect how the frame buffer is mapped. In horizontal mode, the buffer is organized in pages of 8 pixels high, so drawing a shape requires calculating the page and column for each pixel. Libraries like U8g2 handle this automatically, but if you’re writing raw SPI commands, you need to set the page address with 0xB0 to 0xB7 and the column address with 0x00 to 0x7F. The display’s memory is 128x64 bits, but the SSD1306 actually has a 128x64 pixel RAM, so each byte represents 8 vertical pixels. This means drawing a horizontal line is faster than a vertical line because you can write a byte at a time. For example, to draw a horizontal line from (0,0) to (127,0), you send 16 bytes (128 pixels / 8 bits per byte) in one SPI transaction. To draw a vertical line, you need to set the page and column for each pixel, which is slower. The Adafruit_GFX library optimizes this by using the drawFastHLine and drawFastVLine functions, which are faster than generic drawLine. For circles, the library uses the Bresenham algorithm, which calculates pixel positions using integer math, avoiding floating-point operations. The algorithm takes about 1.5 milliseconds for a circle with radius 30 on an Arduino Uno. For filled circles, the library draws horizontal lines for each y-coordinate, which is slower—about 3 milliseconds for the same radius. The display’s pixel density is about 128 pixels per inch, so shapes appear sharp. The contrast ratio is high, with black pixels being truly off (0 cd/m²) and white pixels at 100 cd/m². The display’s lifetime is typically 50,000 hours for the OLED material, but the driver IC is rated for 100,000 hours. The SPI bus can be shared with other devices, but you need to manage the CS pin carefully to avoid conflicts. The display’s initialization sequence is critical: after power-up, you send a reset pulse (low for 10 microseconds), then set the display off, set the multiplex ratio to 63 (for 64 rows), set the display offset to 0, set the start line to 0, set the segment remap to 0 (for normal orientation), set the COM scan direction to normal, set the COM pins hardware configuration to 0x12, set the contrast to 0x7F, set the pre-charge period to 0xF1, set the VCOMH deselect level to 0x40, set the display to normal mode, and finally turn the display on. This sequence takes about 5 milliseconds. Once initialized, you can draw shapes. The frame buffer is cleared by writing 0x00 to all 1024 bytes, which takes about 1 millisecond. Filling the entire display with white (0xFF) takes the same time. For complex shapes, you can use the display.drawBitmap function to draw a monochrome bitmap from an array. The bitmap must be 128x64 pixels, which is 1024 bytes. You can create bitmaps using image conversion tools like LCD Assistant or online converters. The bitmap data is stored in flash memory using the PROGMEM keyword to save RAM. For example, a 128x64 bitmap takes 1024 bytes of flash, which is fine for an Arduino Uno with 32KB of flash. The display’s SPI speed can be increased by using hardware SPI instead of software bit-banging. On an Arduino Uno, hardware SPI runs at 8 MHz, while software SPI might be limited to 1 MHz. On an ESP32, you can run SPI at 40 MHz, reducing shape drawing time to microseconds. For instance, drawing a filled rectangle on an ESP32 takes about 50 microseconds, compared to 500 microseconds on an Arduino Uno. The display’s power consumption scales with the number of pixels lit: at 50% pixels on, it draws about 15 mA; at 100%, 20 mA. The display has a built-in charge pump for generating the OLED voltage, which is around 7-15V. The charge pump can be disabled in sleep mode to save power. The display’s driver IC supports hardware scrolling, which can be used to create animation effects without redrawing shapes. The scrolling can be set to horizontal or vertical, with a speed of 1 to 256 frames per step. For example, you can scroll a shape horizontally by setting the scroll start page and interval. The scrolling is done in hardware, so it doesn’t use CPU cycles. The display’s I2C address is typically 0x3C or 0x3D, depending on the SA0 pin. The I2C speed is limited to 400 kHz, so SPI is preferred for shape drawing. The display’s resolution is fixed, but you can use a smaller frame buffer and scale it up, though that reduces quality. The shape-drawing functions in Adafruit_GFX support anti-aliasing only if you implement it manually, as the library is for monochrome displays. For anti-aliasing, you would need to use a grayscale OLED or a separate display. The display’s pixel size is about 0.2 mm, so shapes are visible from a distance of 30 cm. The display’s viewing angle is wide, so shapes are visible from any angle. The display’s response time is under 10 microseconds, so there’s no motion blur for fast-moving shapes. The display’s operating voltage is 3.3V to 5V, but the logic is 3.3V, so you need level shifters for 5V microcontrollers. The display’s current consumption is 20 mA max, so it can be powered from a microcontroller pin. The display’s driver IC has a built-in oscillator for the pixel clock, which is typically 400 kHz. The display’s frame rate is 100 Hz, but the update rate is limited by the SPI speed. The display’s temperature range is -40°C to +85°C, so it works in cold environments. The display’s lifespan is 50,000 hours, which is about 5.7 years of continuous use. The display’s contrast is adjustable, but the default is usually fine. The display’s pixel structure is a matrix of 128 columns and 64 rows, with each pixel controlled by a thin-film transistor. The display’s OLED material is organic, so it degrades over time, but the lifetime is sufficient for most projects. The display’s driver IC supports multiple display modes, including normal, inverse, and all-on. The inverse mode inverts the pixel state, which can be used for highlighting. The all-on mode turns on all pixels, which is useful for testing. The display’s sleep mode reduces power to 0.1 mA, and it can be woken up by sending a command. The display’s SPI interface is 4-wire, but you can also use 3-wire SPI by combining the data and command lines. The 3-wire mode uses a 9-bit protocol, where the first bit indicates data or command. The 4-wire mode is more common and easier to use. The display’s reset pin is active low, and it should be held low for at least 10 microseconds. The display’s VCC pin can be connected to 3.3V or 5V, but the logic pins are 3.3V tolerant. The display’s CS pin is active low, and it should be pulled low during SPI transactions. The display’s DC pin is used to indicate data (high) or command (low). The display’s MOSI pin is used for data input, and the SCK pin is used for the clock. The display’s MISO pin is not used for SPI, but some variants have a data output pin for reading the frame buffer. The display’s frame buffer is write-only, so you can’t read back pixel data. This means you need to maintain a software buffer if you need to modify individual pixels. The software buffer is 1024 bytes, which is fine for most microcontrollers. The buffer can be updated using the display.drawPixel function, which sets a single pixel. The drawPixel function calculates the byte and bit position based on the x and y coordinates. For example, pixel (0,0) is in byte 0, bit 7. Pixel (0,7) is in byte 0, bit 0. Pixel (1,0) is in byte 1, bit 7. The byte order is horizontal, so the first 128 pixels are in bytes 0-15, the next 128 pixels are in bytes 16-31, and so on. The display’s page addressing mode is the default, but you can also use vertical addressing mode by setting the command 0x20 followed by 0x01. In vertical mode, the buffer is organized by columns, so drawing a vertical line is faster. The display’s driver IC supports a hardware XOR mode, which can be used for drawing cursors or overlays. The XOR mode inverts the pixels where the shape is drawn, making it easy to draw and erase shapes without affecting the background. The XOR mode is enabled by setting the display to XOR mode using the command 0xA6 or 0xA7. The XOR mode is useful for drawing temporary shapes like a selection rectangle. The display’s contrast can be adjusted in real-time, which is useful for dimming the display in low-light conditions. The contrast is set by the command 0x81 followed by a value from 0 to 255. A value of 0 turns off the display, while 255 is the brightest. The default contrast is 128. The display’s pre-charge period and VCOMH level can also be adjusted, but these are typically left at default. The display’s charge pump can be enabled or disabled using the command 0x8D followed by 0x14 (enable) or 0x10 (disable). The charge pump is required for the OLED to work, so it should be enabled. The display’s display on/off command is 0xAF (on) or 0xAE (off). The display can be turned off to save power, but the frame buffer is retained. The display’s sleep mode is entered by turning off the display and disabling the charge pump. The display’s wake-up sequence is the same as the initialization sequence. The display’s driver IC supports multiple segment remap options, which can be used to flip the display horizontally or vertically. The segment remap is set by the command 0xA0 (normal) or 0xA1 (remapped). The COM scan direction is set by the command 0xC0 (normal) or 0xC8 (remapped). These options can be used to orient the display for different mounting positions. The display’s multiplex ratio is set by the command 0xA8 followed by a value from 15 to 63. The default is 63 for 64 rows. The display’s display offset is set by the command 0xD3 followed by a value from 0 to 63. The offset can be used to shift the display vertically. The display’s start line is set by the command 0x40 to 0x7F, where the lower 6 bits indicate the start line. The start line can be used for scrolling. The display’s hardware scrolling is set by the commands 0x26 or 0x27 for horizontal scrolling, and 0x29 or 0x2A for vertical scrolling. The scrolling parameters include the start page, end page, and speed. The scrolling can be stopped by the command 0x2E. The display’s scrolling is useful for text or simple animations. The display’s driver IC also supports a vertical scrolling area, which can be set by the command 0xA3 followed by the top and bottom rows. The scrolling area is used for vertical scrolling. The display’s display RAM is 128x64 bits, which is 1024 bytes. The RAM is divided into 8 pages of 128 bytes each. Each page represents 8 rows of pixels. The RAM is write-only, so you can’t read back the pixel data. The display’s RAM can be accessed using the data command, where you send a byte of data for each column. The RAM is automatically incremented after each byte, so you can write a sequence of bytes without sending the column address each time. The RAM can be set to auto-increment in horizontal or vertical mode. The display’s RAM is cleared on power-up, but it retains data when the display is turned off. The display’s RAM is volatile, so it loses data when power is removed. The display’s driver IC has a built-in oscillator for the pixel clock, which is typically 400 kHz. The oscillator frequency can be adjusted by the command 0xD5 followed by a value from 0 to 255. The default is 0x80. The oscillator frequency affects the display’s refresh rate, but it’s usually set to default. The display’s driver IC also has a charge pump clock, which is set by the command 0xD9 followed by a value from 0 to 255. The default is 0xF1. The charge pump clock affects the OLED voltage, but it’s usually left at default. The display’s driver IC has a VCOMH deselect level, which is set by the command 0xDB followed by a value from 0 to 255. The default is 0x40. The VCOMH level affects the contrast, but it’s usually left at default. The display’s driver IC has a display mode set, which is set by the command 0xA4 (normal) or 0xA5 (all-on). The all-on mode turns on all pixels, which is useful for testing. The display’s driver IC also has a display inverse mode, which is set by the command 0xA6 (normal) or 0xA7 (inverse). The inverse mode inverts the pixel state. The display’s driver IC has a display RAM read/write mode, which is set by the command 0xE0 (read) or 0xE1 (write). The read mode is not commonly used. The display’s driver IC has a command for setting the page address, which is 0xB0 to 0xB7 for pages 0 to 7. The column address is set by the command 0x00 to 0x7F for the lower nibble, and 0x10 to 0x17 for the upper nibble. The column address is set by two commands: the lower nibble first, then the upper nibble. The display’s driver IC has a command for setting the display start line, which is 0x40 to 0x7F. The start line is the first row displayed. The display’s driver IC has a command for setting the segment remap, which is 0xA0 or 0xA1. The segment remap flips the display horizontally. The display’s driver IC has a command for setting the COM scan direction, which is 0xC0 or 0xC8. The COM scan direction flips the display vertically. The display’s driver IC has a command for setting the COM pins hardware configuration, which is 0xDA followed by a value. The default is 0x12 for

À propos de l'auteur
admin