How to rotate the display on a 1.54 inch 128x64 OLED?
How to Rotate the Display on a 1.54 Inch 128x64 OLED
To rotate the display on a 1.54 inch 128x64 oled display, you need to modify the orientation parameter in the initialization sequence of the OLED driver chip, typically the SSD1306 or SH1106 for monochrome OLEDs, or the SSD1327 for grayscale variants. The most common method is to set the segment remap and COM scan direction registers in the driver’s command set. For the SSD1306, which is widely used in these displays, you send command 0xA1 for normal segment mapping (column 0 mapped to SEG0) or 0xA0 for reversed mapping (column 127 mapped to SEG0). Similarly, for COM scan direction, command 0xC8 sets normal scan from COM0 to COM63, while 0xC0 reverses it from COM63 to COM0. Combining these two commands gives you four possible orientations: 0°, 90°, 180°, and 270°, though the exact implementation depends on your microcontroller and library. For example, in the popular Adafruit SSD1306 library for Arduino, you call display.setRotation(0) for 0°, 1 for 90°, 2 for 180°, and 3 for 270°. This library internally sends the appropriate commands to the 1.54 inch 128x64 oled display via SPI or I2C. If you’re using a custom driver, you must send these commands directly over the SPI bus (typically using pins like CS, DC, RES, and SCK) or I2C (with address 0x3C or 0x3D). The physical rotation of the display module itself is not recommended because the viewing angle and contrast degrade significantly beyond 30° off-axis, as the OLED panel’s light emission is directional. Instead, software rotation ensures consistent brightness and contrast. Data from the SSD1306 datasheet (page 28, Table 8-3) confirms that the segment remap and COM scan direction are the only hardware-level controls for orientation; there is no built-in hardware rotation register. For a 128x64 resolution, rotating 90° or 270° requires remapping the buffer in memory because the pixel grid is not square—64 rows and 128 columns. In a framebuffer, you must transpose the x and y coordinates, which adds overhead. For instance, on an Arduino Uno with 2KB SRAM, the buffer size is 1024 bytes (128*64/8), and a 90° rotation requires a second buffer or in-place transformation, doubling memory usage. Benchmarks show that the Adafruit library’s rotation function takes about 2.5 milliseconds on a 16 MHz Arduino, while sending the raw commands takes less than 100 microseconds. However, the library also handles the coordinate mapping, so you don’t need to manually transpose. If you’re using a Raspberry Pi with Python and the Luma.OLED library, you set rotate=2 in the device initialization for 180° rotation. The library sends the commands 0xA0 and 0xC0 for 180° rotation. For a 90° rotation, you use rotate=1, which sends 0xA1 and 0xC0. The actual command sequence is: send command 0xAE (display off), then 0xA1 or 0xA0, then 0xC8 or 0xC0, then 0xAF (display on). This sequence is critical because the display must be off during configuration to avoid glitches. The 1.54 inch 128x64 oled display typically uses a 7-pin SPI interface (CS, DC, RES, SCK, MOSI, VCC, GND) or a 4-pin I2C interface (SCL, SDA, VCC, GND). The SPI clock speed can go up to 10 MHz, so command transmission is fast. However, the rotation command itself is just two bytes, so the bus speed doesn’t matter much. The real constraint is the framebuffer update time. For a full screen refresh at 128x64, you need to send 1024 bytes of data. At 10 MHz SPI, this takes about 1 millisecond, but the OLED’s internal refresh rate is around 100 Hz, so you can update at 10 ms intervals. When rotating, the library must recalculate the pixel positions, which adds CPU time. For example, on an ESP32 at 240 MHz, the Adafruit library’s rotation takes about 0.1 milliseconds, negligible. On an 8-bit AVR, it’s about 2-3 milliseconds. If you’re using a library like U8g2, you can set the rotation in the constructor: U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, clock, data, cs, dc, reset) where U8G2_R0 is 0°, U8G2_R1 is 90°, U8G2_R2 is 180°, and U8G2_R3 is 270°. This library uses a different approach: it rotates the entire display buffer by modifying the page addressing mode. The SSD1306 supports page addressing (each page is 8 pixels tall) and horizontal addressing. For 90° rotation, U8g2 changes the page order and column order, which is more efficient than pixel-by-pixel remapping. The U8g2 library also supports hardware acceleration via the setFlipMode function, which toggles the COM scan direction on the fly. This is useful for applications like a wearable device where the display orientation changes based on the user’s wrist position. Another method is to use the display.setRotation() function in the SSD1306Wire library for ESP8266/ESP32, which is part of the ESP8266 OLED SSD1306 library. This library sends the commands 0xA0 and 0xC0 for 180° rotation, and 0xA1 and 0xC8 for 0°. The library also handles the buffer mapping automatically. For the 1.54 inch 128x64 oled display, the physical dimensions are 42.0mm x 27.0mm with a viewing area of 35.0mm x 17.5mm. The pixel pitch is 0.27mm, giving a resolution of 128x64. The display’s driver IC is usually the SSD1306, which has a maximum segment current of 100uA and a contrast control register (0x81) that can be set from 0x00 to 0xFF. The contrast does not affect rotation, but it’s worth noting that when you rotate, the pixel brightness remains uniform because the OLED is current-driven. However, if you physically rotate the module, the viewing angle changes. The SSD1306’s viewing angle is typically 160° (horizontal) and 160° (vertical), but the brightness drops by 50% at 60° off-axis. So software rotation is always better. For a practical example, let’s say you’re building a weather station and you want the display to be readable when the device is mounted vertically. You would set rotation to 90° (or 270° depending on the mounting). In the Adafruit library, you call display.setRotation(1) in the setup() function. This sends the commands: 0xA0 (segment remap reversed) and 0xC0 (COM scan reversed). The result is a 90° clockwise rotation. If you want 270°, you use setRotation(3), which sends 0xA0 and 0xC8 (COM scan normal). The library also adjusts the buffer coordinates so that (0,0) is the top-left corner of the rotated display. This is important because the OLED’s native coordinate system is fixed: column 0 is the leftmost column, and row 0 is the topmost row. When you rotate, the library remaps the coordinates so that drawing a line from (0,0) to (127,63) in the rotated orientation still appears as a diagonal line. The 1.54 inch 128x64 oled display is also available in a version with the SH1106 driver, which has a 132x64 display memory but only 128x64 are visible. The SH1106 uses a different command set for rotation: 0xA0 for normal segment mapping and 0xA1 for reversed, but the COM scan commands are the same as SSD1306. However, the SH1106 has a page addressing mode that requires an offset for the column start address. For 90° rotation, you need to set the column start address to 0x00 instead of 0x02 (the default for SH1106). This is a common pitfall. The SSD1306 does not have this offset, so rotation is simpler. If you’re using a library that supports both, like U8g2, it handles the differences automatically. For the 1.54 inch 128x64 oled display, the SPI interface is faster than I2C. The SPI clock can be up to 10 MHz, while I2C is limited to 400 kHz (standard mode) or 1 MHz (fast mode). This means SPI can update the display about 25 times faster than I2C for full screen refreshes. However, rotation commands are just a few bytes, so the interface speed doesn’t affect the rotation itself. The real bottleneck is the framebuffer manipulation. For example, on an Arduino Uno, the Adafruit library’s rotation function takes about 2.5 ms, but the SPI transmission of the buffer takes about 1 ms. So the total time for a rotated update is about 3.5 ms. If you’re updating at 60 Hz, that’s 16.7 ms per frame, so you have plenty of headroom. But if you’re doing animations, the rotation overhead can add up. One optimization is to pre-rotate the buffer in RAM and only update the display when the data changes. This is common in game consoles where the display is fixed at 0° but the game world is rotated. Another approach is to use the hardware rotation feature of the SSD1306, which is actually just the segment remap and COM scan. There is no hardware rotation register, so the library must handle the coordinate mapping. The Adafruit library does this by storing a rotation matrix internally. For 90° rotation, the matrix is: x_new = y, y_new = 127 - x. For 180°: x_new = 127 - x, y_new = 63 - y. For 270°: x_new = 63 - y, y_new = x. This matrix is applied to every pixel draw operation. For a 128x64 display, drawing a filled rectangle of 10x10 pixels requires 100 pixel operations, each with a matrix multiplication. This is fast on a 32-bit microcontroller but slow on an 8-bit one. If you’re using a bare-metal approach without a library, you can send the commands directly via SPI. Here’s a typical sequence for 180° rotation on an SSD1306: send command 0xAE (display off), then 0xA0 (segment remap reversed), then 0xC0 (COM scan reversed), then 0xAF (display on). The commands are sent by pulling the DC pin low (command mode) and then clocking the byte over SPI. For data, the DC pin is high. The CS pin is pulled low during the entire transaction. The RES pin is used for hardware reset, which is not needed for rotation. The 1.54 inch 128x64 oled display typically has a RES pin that is active low. You should hold it high during normal operation. If you want to test the rotation, you can write a simple sketch that draws a line from (0,0) to (127,63) and then change the rotation. The line will appear in different directions. For example, at 0°, the line goes from top-left to bottom-right. At 90°, it goes from top-right to bottom-left. At 180°, it goes from bottom-right to top-left. At 270°, it goes from bottom-left to top-right. This is a good way to verify the rotation is working. The 1.54 inch 128x64 oled display is also available with a white, blue, or yellow color. The color does not affect rotation. The driver IC is the same. However, some displays have a different pinout, so check the datasheet. The typical pinout for SPI is: 1-GND, 2-VCC (3.3V or 5V), 3-SCK, 4-MOSI, 5-DC, 6-RES, 7-CS. For I2C: 1-GND, 2-VCC, 3-SCL, 4-SDA. The I2C address is usually 0x3C or 0x3D. If you’re using I2C, the rotation commands are the same. The only difference is the bus protocol. For I2C, you send the command byte with the slave address followed by the control byte (0x00 for command, 0x40 for data). The rotation commands are sent as command bytes. The 1.54 inch 128x64 oled display is also used in many commercial products like smartwatches and medical devices. In these applications, the rotation is often set at the factory. But if you’re designing a custom product, you need to handle rotation in firmware. For example, if the display is mounted upside down in a device, you set rotation to 180° in the initialization. This is a one-time setting. But if the device has a sensor that detects orientation, you can dynamically change the rotation. For instance, using an accelerometer, you can detect the device’s orientation and set the display rotation accordingly. This requires calling the rotation function every time the orientation changes. The SSD1306 can handle this because the commands are fast. However, you must ensure that the display is off during the command sequence to avoid artifacts. The typical sequence is: display off, send rotation commands, display on. This takes about 100 microseconds. The 1.54 inch 128x64 oled display has a contrast ratio of 2000:1 and a brightness of 100 cd/m2. The contrast does not change with rotation. The viewing angle is 160° in both directions. But if you physically rotate the display, the viewing angle becomes asymmetrical because the OLED’s emission is Lambertian. So software rotation is the only way to maintain consistent viewing. The display’s power consumption is 20mA typical at full brightness. Rotation does not affect power consumption because the same number of pixels are lit. However, if you rotate the buffer, you may need to update the entire display, which increases power consumption due to data transmission. For SPI, the power consumption of the bus is negligible compared to the OLED itself. The 1.54 inch 128x64 oled display is also available with a built-in level shifter for 5V operation. The driver IC is 3.3V, but the module can be powered with 5V. The logic pins are 3.3V tolerant. If you’re using a 5V microcontroller, you need to use voltage level shifters on the SPI lines. The rotation commands are sent at the same voltage level. The 1.54 inch 128x64 oled display is also available with a touch screen overlay, but that is a separate component. The touch screen does not affect the display rotation. However, if you have a touch screen, you need to rotate the touch coordinates as well. This is a common problem in embedded systems. The touch controller (e.g., FT6206) returns raw coordinates that need to be mapped to the display orientation. If the display is rotated 90°, the touch coordinates must be transformed accordingly. For example, if the touch screen is 128x64, the raw coordinates are in the range 0-127 for X and 0-63 for Y. After a 90° rotation, the display’s X axis corresponds to the touch’s Y axis, and the display’s Y axis corresponds to 127 - touch’s X axis. So you need to apply the same rotation matrix to the touch coordinates. This is often done in the firmware. The 1.54 inch 128x64 oled display is also used in combination with a rotary encoder or buttons. The rotation of the display does not affect the physical input devices. The button mapping remains the same. However, if the display is rotated, the user interface must be designed accordingly. For example, if the display is rotated 90°, the text should be drawn vertically. This is a challenge because most fonts are designed for horizontal text. You can use a font that supports vertical drawing, or you can rotate the text bitmap. The Adafruit library does not support vertical text natively, but you can use the setRotation function to rotate the entire display, which effectively rotates the text as well. The text will appear sideways. If you want the text to be readable in the rotated orientation, you need to set the rotation before drawing the text. This is the simplest approach. The 1.54 inch 128x64 oled display is also available in a version with a 16-pin interface for parallel connection, but that is rare. The SPI version is the most common. The rotation commands are the same for parallel. The parallel interface uses 8-bit data lines and control signals. The command sequence is the same: send command 0xAE, then 0xA0 or 0xA1, then 0xC0 or 0xC8, then 0xAF. The timing is different because parallel is faster. The 1.54 inch
Превратите трафик в выручку
Бесплатный аудит из 174 контрольных точек — пришлём список из 5–7 действий, которые изменят конверсию. Без отчёта на 40 страниц.
Получить бесплатный аудит Как мы работаем