How to display a graph on a 0.95 inch 96x64 OLED?
How to Display a Graph on a 0.95 Inch 96x64 OLED
To display a graph on a 0.95 inch 96x64 OLED, you need to drive the screen with a microcontroller like an ESP32 or STM32, using SPI or I2C communication, and render pixel data into a frame buffer that maps to the 96x64 resolution. The OLED typically uses an SSD1306 or SH1107 driver, but for this size, the SH1107 is more common due to its support for 96x64 matrix. You start by initializing the display over SPI at 4 MHz to 10 MHz, depending on your MCU clock speed. Then, you allocate a 768-byte buffer (96 columns x 64 rows / 8 bits per page) if using monochrome, or 18,432 bytes if using RGB color (96x64x3). For a graph, you calculate x-y coordinates, map data points to pixel positions, and set bits in the buffer. For a line graph, you use Bresenham’s algorithm to draw lines between consecutive points. For a bar chart, you fill rectangles from the bottom of the screen to the y-value. The key is to update the buffer only when data changes, not every frame, to avoid flicker. You can also use DMA (Direct Memory Access) on the ESP32 to send data to the SPI bus without blocking the CPU, allowing real-time graph updates at 30 fps or higher. The graph should be scaled to fit within the 96-pixel width, so if you have 100 data points, you need to downsample or use a sliding window. For example, with a 10-bit ADC reading temperature, you map 0–1023 to 0–63 pixels vertically. You can also add grid lines by setting specific bits in the buffer. Power consumption is about 20 mA at 3.3V, so it’s suitable for battery-powered sensors. If you need color, consider a 0.95 inch 96x64 color oled display that uses an SSD1331 driver, which supports 65K colors and requires a larger buffer but gives more visual impact. For instance, you can plot a red line against a blue background. The SPI speed for color OLEDs can go up to 20 MHz, but you need to ensure your MCU can handle the data rate. The display module usually has a 0.5 mm pitch FPC connector, so you’ll need a breakout board or a custom PCB. The viewing angle is 160 degrees, and the contrast ratio is 2000:1, making it readable in direct sunlight with a polarizer. The pixel pitch is 0.21 mm, so the physical size is about 20.1 mm x 13.4 mm. For graph rendering, you can use libraries like Adafruit_GFX or U8g2, which provide functions for lines, rectangles, and circles. However, for custom graphs, you’ll write low-level pixel manipulation. Here’s a breakdown of the process:
| Step | Action | Details |
|---|---|---|
| 1 | Initialize SPI | Set clock to 8 MHz, mode 0, with CS, DC, and RST pins. Use 4-wire SPI for speed. |
| 2 | Allocate buffer | For monochrome: 96x64/8 = 768 bytes. For color: 96x64x2 = 12,288 bytes (16-bit color). |
| 3 | Map data to pixels | Scale x to 0–95, y to 0–63. Use integer math to avoid floating-point overhead. |
| 4 | Draw graph | Use Bresenham for lines, or fill rectangles for bars. Set bits in buffer. |
| 5 | Send buffer | Use DMA or blocking write. For SH1107, send command 0xAF to turn on display. |
| 6 | Update rate | Keep refresh under 50 ms for smooth animation. Use double buffering if needed. |
For a real-world example, say you’re logging temperature from a DS18B20 sensor on an Arduino Nano. The sensor outputs 12-bit data, but you only need 0–100°C. Map the temperature to vertical pixels: 0°C = 0, 100°C = 63. The x-axis shows time, with each pixel representing 1 second, so the graph scrolls left every second. You implement a circular buffer of 96 values. Every second, you shift the graph left by 1 pixel, then draw the new point at x=95. The buffer is updated by clearing the first column and redrawing all lines. This requires 96 line draws, each taking about 10 microseconds on a 16 MHz Arduino, so total draw time is 960 microseconds, well within the 1-second interval. The OLED’s response time is 10 microseconds per pixel, so the whole screen updates in 7680 microseconds for monochrome. For color, it’s 122,880 microseconds, which is 122 ms, so you might need to reduce the update rate to 8 fps. The SPI bus can handle 1 MHz to 10 MHz, but the limiting factor is the MCU’s memory bandwidth. On an ESP32, you can use the I2S peripheral to drive the SPI bus at 40 MHz, achieving 30 fps for color graphs. The graph quality depends on the pixel density: 96 pixels across means you can show about 48 data points with 2-pixel spacing, or 96 points with 1-pixel spacing. For a smooth curve, you need interpolation. Use linear interpolation between points, or cubic spline for smoother curves, but that increases computation time. For example, with 10 data points, you can interpolate to 96 points by calculating the slope between each pair. This adds about 200 microseconds for 96 points on an ESP32. The OLED’s contrast is adjustable via software command 0x81, with values from 0 to 255. Set it to 128 for typical use. The display’s temperature range is -40°C to 85°C, so it works in harsh environments. The power consumption is 12 mA for monochrome and 20 mA for color at full brightness. You can reduce it to 1 mA by using sleep mode (command 0xAE). For a graph, you can also add a cursor or data labels by drawing text using a 5x7 font. Each character takes 5x7 pixels, so you can fit about 13 characters per line. The font data is stored in flash memory, about 1 KB per font. The graph can be combined with a user interface using buttons or a rotary encoder. For example, you can press a button to switch between line and bar graph, or zoom in on a range. The zoom function requires recalculating the scaling factor. If you zoom in 2x, you only show 48 x-values, and you need to pan the graph. The OLED’s memory is organized in pages, so you can update only a portion of the screen by setting the page address and column address. This is useful for partial updates. For instance, if only the last 10 pixels change, you send only 10 bytes instead of 768. This reduces SPI traffic and power. The typical SPI command sequence for a partial update is: set column range (0x21, start, end), set page range (0x22, start, end), then send data. The SH1107 supports up to 132 columns, but the visible area is 96 columns, so you need to set the display offset (command 0xD3) to center the image. The offset value is typically 0 for 96x64. The display driver also supports horizontal scrolling (command 0x26), but for graphs, you implement software scrolling. The graph’s background can be set to black or white. For a monochrome OLED, black is the default (pixels off), and white is on. For a color OLED, you set the background to any RGB color. The color depth is 16-bit (5-6-5 format), so you have 32 red, 64 green, 32 blue levels. This gives 65,536 colors. For a graph, you can use a gradient background to show temperature zones. For example, blue for cold, red for hot. The gradient is drawn by filling rectangles with increasing color values. This requires 64 rectangles for a vertical gradient, each with a different color. The computation for this is trivial on a 32-bit MCU. The graph can also be interactive. For example, if you have a touch sensor, you can detect touch coordinates and display the value at that point. The touch sensor is not integrated into the OLED, so you need a separate touch panel, like a resistive touch overlay. The overlay adds about 1 mm thickness and 10 mA power consumption. The total system cost for a graph display is around $10 for the OLED, $5 for the MCU, and $2 for the sensor. The graph’s accuracy is limited by the pixel resolution. For a 0–100°C range, each pixel represents 1.56°C. To improve accuracy, you can use a logarithmic scale or a smaller range. For example, for 20–30°C, each pixel is 0.156°C. The graph’s update rate is also limited by the sensor’s sampling rate. The DS18B20 takes 750 ms for 12-bit conversion, so you can update the graph every second. For faster sensors like the MAX31855, you can update at 10 Hz. The OLED’s lifespan is 50,000 hours for continuous operation, so it’s suitable for long-term monitoring. The graph can be stored in flash memory as a bitmap for a static image, but for dynamic data, you need real-time rendering. The rendering algorithm can be optimized by using lookup tables for trigonometric functions if you’re plotting sine waves. For example, you can precompute 96 sine values and store them in an array. This takes 384 bytes for 16-bit values. The graph’s appearance can be enhanced with anti-aliasing, but this is computationally expensive. For a 96x64 display, anti-aliasing might not be noticeable due to the small pixel size. The pixel pitch is 0.21 mm, so the human eye can’t distinguish individual pixels at a distance of 30 cm. The display’s refresh rate is 100 Hz, but the human eye perceives 30 fps as smooth. For a graph, 10 fps is sufficient for most applications. The graph’s data can be logged to an SD card via SPI, and then replayed on the OLED. The SD card uses the same SPI bus, but you need a separate chip select line. The graph’s y-axis can be labeled with values using a 5x7 font. For example, you can display the min and max values on the left side. The font rendering takes about 1 ms per character. The graph’s x-axis can be labeled with time stamps. The OLED’s driver IC supports hardware rotation, but you can also rotate the graph in software by swapping x and y coordinates. The graph’s data can be filtered using a moving average to reduce noise. For example, a 5-point moving average smooths the graph. The filter requires 5 bytes of memory per data point. The graph’s color can be used to indicate different data series. For example, red for temperature, blue for humidity. The color OLED can display 16 colors simultaneously if you use a palette. The palette is stored in the driver IC’s RAM. The graph’s background can be a grid pattern. The grid is drawn by setting pixels at regular intervals. For example, a 10x10 pixel grid. The grid lines can be dashed to reduce visual clutter. The graph’s data points can be marked with circles or squares. The circle drawing algorithm uses the midpoint circle algorithm, which takes about 100 microseconds per circle. The graph’s title can be displayed at the top using a larger font. The font size can be 8x8 or 10x16. The larger font takes more memory but is more readable. The graph’s overall layout should be designed to maximize data visibility. The graph area should be at least 80x48 pixels, leaving room for labels. The graph’s scaling can be automatic or manual. Automatic scaling calculates the min and max of the data and adjusts the y-axis. This requires sorting the data, which takes O(n) time. The graph’s data can be stored in a circular buffer of 96 values. The buffer is updated in real time. The graph’s rendering can be done in the background using a timer interrupt. The interrupt rate should be 10 Hz to 30 Hz. The interrupt service routine should be short to avoid missing interrupts. The graph’s data can be transmitted over WiFi or Bluetooth to a smartphone. The ESP32 can send the graph data via BLE, and the smartphone can display it. The OLED is just for local monitoring. The graph’s power consumption can be reduced by turning off the display when not in use. The sleep mode draws 1 uA. The graph’s wake-up time is 10 ms. The graph’s data can be encrypted for security, but this is not necessary for most applications. The graph’s accuracy is also affected by the OLED’s gamma correction. The gamma can be adjusted via command 0xB8 for the SH1107. The default gamma is linear. The graph’s color can be calibrated using a colorimeter, but this is overkill for most projects. The graph’s viewing angle is 160 degrees, so it’s readable from any direction. The graph’s contrast ratio is 2000:1, so it’s visible in bright light. The graph’s response time is 10 microseconds, so there’s no motion blur. The graph’s pixel failure rate is less than 1% over 50,000 hours. The graph’s electrostatic discharge protection is rated at 2 kV. The graph’s operating voltage is 3.3V, but it can tolerate 5V logic levels if you use a level shifter. The graph’s SPI bus can be shared with other devices, like a sensor or an SD card. The graph’s chip select pin must be unique. The graph’s data can be displayed in real time or from a file. The graph’s file format can be a bitmap or a raw data file. The graph’s conversion from raw data to pixels is done in software. The graph’s algorithm can be optimized for speed by using pointer arithmetic. The graph’s buffer can be stored in external SRAM for larger data sets. The graph’s display can be updated in a single SPI transaction using a burst write. The burst write sends all 768 bytes at once. The SPI transaction time is 768 bytes * 8 bits / 8 MHz = 768 microseconds. The graph’s update rate is limited by the SPI transaction time. For a color OLED, the transaction time is 12,288 bytes * 8 bits / 8 MHz = 12.288 ms. This allows 81 fps theoretically, but the MCU overhead reduces it to 30 fps. The graph’s DMA can reduce the CPU load to near zero. The DMA transfers the buffer to the SPI peripheral without CPU intervention. The DMA setup takes 10 microseconds. The graph’s double buffering can prevent tearing. The double buffer uses two buffers: one for drawing, one for displaying. The buffers are swapped after each update. The swap takes 1 microsecond. The graph’s memory usage is 1536 bytes for monochrome double buffer, or 24,576 bytes for color double buffer. The graph’s color depth can be reduced to 8-bit (256 colors) to save memory. The 8-bit color uses a palette. The palette is stored in the driver IC’s RAM. The graph’s palette can be customized for the data. For example, a temperature gradient from blue to red. The graph’s palette size is 256 entries, each 16 bits. The graph’s color can be dithered to simulate more colors. The dithering algorithm uses a 2x2 pattern. The dithering adds 10% to the rendering time. The graph’s rendering can be accelerated by using the MCU’s hardware graphics engine, if available. The ESP32 has a hardware JPEG decoder, but not a graphics engine. The STM32F4 has a hardware graphics accelerator. The graph’s rendering can be done in parallel using multiple cores. The ESP32 has two cores, so you can run the graph rendering on one core and the sensor reading on the other. The graph’s data can be synchronized between cores using a mutex. The graph’s performance can be measured in frames per second. For a monochrome graph, you can achieve 100 fps on an ESP32 at 240 MHz. For a color graph, you can achieve 30 fps. The graph’s power consumption is proportional to the update rate. At 30 fps, the power is 20 mA. At 1 fps, the power is 1 mA. The graph’s data can be displayed in a variety of styles, including line, bar, scatter, and area. The area graph fills the area under the line. The area fill uses a flood fill algorithm. The flood fill takes 1 ms for a 96x64 area. The graph’s data can be animated by adding a trailing effect. The trailing effect fades the previous data points. The fading is done by reducing the brightness of the pixels. The brightness is controlled by the contrast register. The contrast can be changed per pixel only in color OLEDs. The graph’s trailing effect requires storing the previous frame in a buffer. The graph’s animation can be used to show trends over time. The graph’s data can be exported to a CSV file on the SD card. The CSV file can be analyzed on a PC. The graph’s display can be mirrored to a second OLED. The second OLED uses the same SPI bus with a different chip select. The graph’s data can be broadcast over a network using MQTT. The MQTT message contains the graph data as JSON. The graph’s display can be controlled remotely via a web interface. The web interface uses a web server on the ESP32. The graph’s data can be stored in a database on the cloud. The graph’s display can be used in a weather station, a heart rate monitor, or a spectrum analyzer. The graph’s resolution is sufficient for most visualization tasks. The
Shop 12,400+ vet-authorized medicines.
From flea & tick preventives to compounded cat thyroid chews — reviewed by a DVM, priced 31% below clinic retail on average.
This article is for educational purposes only and does not replace veterinary care. Always consult your licensed veterinarian before starting, changing, or stopping any medication or supplement for your pet.