Skip to content
Est.
2014
Tuesday Edition

How to use a 2.08 inch 256x64 OLED display with a temperature sensor?

aBy admin About the author available below

How to Use a 2.08 Inch 256x64 OLED Display with a Temperature Sensor

You hook up a 2.08 inch 256x64 oled display to a temperature sensor by wiring the sensor’s data line to a microcontroller’s analog or digital pin, then writing code to read the sensor’s output and send the temperature value to the display over SPI. The display itself runs on a 3.3V supply, draws about 20mA typical with all pixels on, and uses a SSD1306-like controller (often the SH1106 variant for 256x64 resolution). Most temperature sensors, like the DS18B20 or the DHT22, output data in degrees Celsius with a resolution of 0.0625°C for the DS18B20, and the DHT22 offers ±0.5°C accuracy from -40°C to 80°C. You’ll need to initialize the display with a 256x64 buffer, which is 2048 bytes total (256 columns × 64 rows / 8 bits per byte), and update it at least once per second to reflect new sensor readings. The SPI clock speed can go up to 10 MHz, but a safe 4 MHz works fine for most Arduino boards, ensuring no data corruption over longer wires up to 30 cm. For the wiring, the display’s CS pin connects to a digital output (e.g., pin 10 on Arduino Uno), DC to pin 9, RES to pin 8, and MOSI/SCK to pins 11 and 13 respectively. The temperature sensor, if using a DS18B20, needs a 4.7kΩ pull-up resistor on the data line, and it communicates over OneWire protocol, which uses a single pin (e.g., pin 2). The entire setup consumes under 50mA total, making it viable for battery-powered projects with a 3.7V LiPo cell and a step-up regulator to 3.3V. You can find the exact display pinout and specs from the 2.08 inch 256x64 oled display datasheet, which lists a 1.6mm thickness and a 0.95mm pixel pitch, giving a 52.5mm × 13.1mm active area. The display’s contrast is adjustable via software command 0x81, with values from 0 to 255, and a typical setting of 0x7F works well for indoor use. The temperature sensor’s conversion time is 750ms for 12-bit resolution, so you can read it every second without lag. Below is a wiring table for clarity:

ComponentPinArduino Uno PinNotes
OLED DisplayVCC3.3VDo not use 5V, risk of damage
OLED DisplayGNDGNDCommon ground
OLED DisplayCS10Chip select, active low; must be pulled low to initiate SPI communication
OLED DisplayDC9Data/Command select; high for data, low for commands
OLED DisplayRES8Reset pin, active low; a brief low pulse resets the display controller
OLED DisplayMOSI11Master Out Slave In; data line from Arduino to display
OLED DisplaySCK13Serial Clock; synchronizes data transfer
DS18B20 SensorVDD3.3VPower supply; can also be powered in parasitic mode
DS18B20 SensorGNDGNDCommon ground with display and Arduino
DS18B20 SensorDQ2Data line; requires 4.7kΩ pull-up resistor to 3.3V

Now, let us delve deeper into each aspect of this integration to ensure a thorough understanding and successful implementation. The 2.08 inch 256x64 OLED display is a monochrome graphic display that offers a crisp, high-contrast viewing experience thanks to its organic light-emitting diode technology. Unlike traditional LCDs, OLEDs do not require a backlight, which means each pixel emits its own light, resulting in deeper blacks, faster response times, and lower power consumption when displaying dark content. The display’s resolution of 256 columns by 64 rows provides ample space for presenting temperature readings alongside additional information such as time, date, or graphical elements like bar charts or trend lines. The active area measures 52.5mm by 13.1mm, making it compact enough to fit into small enclosures while still being readable from a comfortable distance. The pixel pitch of 0.95mm ensures that individual pixels are discernible, yet the overall image appears smooth and well-defined. The display’s thickness of just 1.6mm adds to its versatility, allowing it to be integrated into slim devices or mounted on surfaces without adding bulk.

When connecting the display to a microcontroller, it is crucial to adhere to the 3.3V supply voltage requirement. Applying 5V to the VCC pin can permanently damage the display, as the internal driver IC is not rated for higher voltages. The typical current draw of 20mA with all pixels illuminated is a worst-case scenario; in practice, when displaying only text or partial graphics, the current consumption is often lower, which is beneficial for battery-powered projects. The display controller, often an SH1106 variant for 256x64 resolution, is similar to the more common SSD1306 but with additional memory to support the extra columns. The SH1106 has a built-in 128x64 display RAM, but for 256x64, it uses a page addressing mode where each page corresponds to 8 rows, and the RAM is organized into 128 columns per page. To address the full 256 columns, the controller uses a segment mapping that allows horizontal scrolling across two 128-column banks. This means that your initialization code must include commands to set the segment remap and COM scan direction correctly to ensure the display renders from left to right without mirroring. The buffer size of 2048 bytes is calculated as (256 columns * 64 rows) / 8 bits per byte, which is the amount of RAM needed to hold the entire frame. You can allocate this buffer in the microcontroller’s memory, but on devices with limited SRAM, such as the Arduino Uno with only 2KB, this may leave little room for other variables. In such cases, you can optimize by updating only the regions of the display that change, or by using a microcontroller with more RAM, like the Arduino Mega or ESP32.

The SPI communication protocol used by the display is straightforward but requires careful timing. The CS (Chip Select) pin must be pulled low before sending any data or commands, and then pulled high after the transaction is complete. The DC (Data/Command) pin tells the display whether the incoming byte is a command (low) or data (high). The RES (Reset) pin should be held low for at least 10 microseconds during initialization to ensure the controller starts in a known state. The SPI clock speed of 4 MHz is a safe choice for most Arduino boards because it balances speed with signal integrity. At 10 MHz, the maximum rated speed, you may encounter issues with longer wires or breadboard connections due to capacitance and crosstalk. Keeping wires under 30 cm and using shielded cables can mitigate these problems. The MOSI pin carries data from the Arduino to the display, while the SCK pin provides the clock signal that synchronizes the data transfer. The display does not send data back to the Arduino over SPI, so the MISO pin is not used and can be left unconnected.

Turning to the temperature sensor, the DS18B20 is a popular choice due to its digital output, high accuracy, and simple wiring. It communicates using the OneWire protocol, which allows multiple sensors to share a single data pin, making it scalable for projects that require monitoring multiple locations. The sensor can be powered in two modes: external power, where VDD is connected to 3.3V, or parasitic power, where the sensor draws power from the data line. In parasitic mode, the data line must be held high during temperature conversions, which can complicate timing. For simplicity, external power is recommended, requiring only a 4.7kΩ pull-up resistor between the data line and VDD. The sensor’s resolution is configurable from 9 to 12 bits, with 12 bits providing the finest granularity of 0.0625°C per step. The conversion time increases with resolution: 93.75ms for 9 bits, 187.5ms for 10 bits, 375ms for 11 bits, and 750ms for 12 bits. For most applications, 12-bit resolution is adequate, and the 750ms conversion time means you can read the sensor once per second with a comfortable margin. The sensor’s accuracy is ±0.5°C over the range of -10°C to +85°C, which covers typical indoor and outdoor environments. For extreme temperatures, the accuracy degrades to ±2°C from -55°C to +125°C, but this is still suitable for many industrial applications.

Alternatively, the DHT22 sensor offers a wider temperature range of -40°C to 80°C with the same ±0.5°C accuracy, but it also measures humidity, which can be useful for environmental monitoring. The DHT22 uses a proprietary single-wire protocol that is different from OneWire and requires precise timing in the code. Its sampling rate is limited to 2 seconds (0.5 Hz), so you cannot read it faster than that. The DHT22’s power consumption is higher than the DS18B20, drawing up to 1.5mA during conversion, but it still falls within the 50mA total budget of the system. When choosing between the two, consider whether you need humidity data and whether the slower sampling rate is acceptable. For pure temperature monitoring, the DS18B20 is often preferred for its simplicity and lower power consumption.

The software aspect of the project involves writing code that initializes the display, configures the sensor, and then continuously reads the temperature and updates the display. The display initialization sequence typically includes commands to turn off the display, set the multiplex ratio to 63 (for 64 rows), set the display offset to 0, set the start line to 0, enable the charge pump for internal voltage generation, set the segment remap to column 127 (for correct orientation), set the COM scan direction to remapped mode, set the display clock divide ratio and oscillator frequency, set the pre-charge period, set the VCOMH deselect level, enable the display, and clear the buffer. For the SH1106, additional commands are needed to set the page address and column address ranges to cover all 256 columns. The sensor reading code for the DS18B20 involves sending a Skip ROM command to address all sensors on the bus, then a Convert T command to start a temperature conversion, waiting 750ms, then sending a Read Scratchpad command to retrieve the temperature data as two bytes. These bytes are combined and converted to a floating-point Celsius value by multiplying by 0.0625. For the DHT22, the reading process involves pulling the data line low for at least 1ms to start the sensor, then reading the 40-bit data packet that includes temperature and humidity values with a checksum for error checking.

Once the temperature value is obtained, it must be formatted as a string for display. You can use the dtostrf() function in Arduino to convert a float to a character array with a specified number of decimal places. For example, dtostrf(temperature, 6, 2, buffer) will create a string like " 23.45" with a width of 6 characters and 2 decimal places. You can then use a graphics library like Adafruit_SSD1306 or U8g2 to draw the text on the display. The U8g2 library is particularly versatile because it supports a wide range of display controllers and provides functions for drawing text in various fonts, as well as primitive shapes like lines, circles, and rectangles. To display the temperature, you would set the font, cursor position, and call u8g2.print() with the buffer. After drawing, you must call u8g2.sendBuffer() to transfer the buffer to the display over SPI. This update should be done at least once per second to keep the reading current, but you can increase the frequency if the sensor conversion time allows. For the DS18B20 at 12-bit resolution, the maximum update rate is about 1.33 Hz, so once per second is ideal.

Power management is a key consideration for battery-powered projects. The total current consumption of the display (20mA) and the sensor (1.5mA for DHT22 or 1mA for DS18B20 during conversion, less than 1µA in standby) plus the Arduino Uno (about 50mA in active mode) can quickly drain a small battery. To extend battery life, you can put the microcontroller to sleep between readings using the LowPower library or the built-in sleep modes. For example, you can set the Arduino to sleep for 8 seconds, wake up, read the sensor, update the display, and then go back to sleep. This reduces the average current consumption significantly. The display can also be turned off between updates by sending a display off command, but this may not be necessary if the update interval is short. Using a 3.7V LiPo battery with a step-up regulator to 3.3V is a common approach, but ensure the regulator can supply at least 100mA to handle peak currents. Alternatively, you can use a 3.3V Arduino board like the Pro Mini 3.3V or an ESP32 that runs directly on 3.3V, eliminating the need for a regulator.

Finally, troubleshooting common issues can save time. If the display shows nothing, check that the VCC is 3.3V and not 5V, that the RES pin is not floating (it should be connected to a digital pin or pulled high), and that the CS pin is being pulled low during communication. If the display shows garbled characters, verify the SPI wiring, especially that MOSI and SCK are not swapped, and that the clock speed is not too high. If the temperature reading is erratic, ensure the pull-up resistor on the DS18B20 data line is present and correctly valued, and that the sensor is not placed near heat sources or in direct sunlight. If using the DHT22, ensure the data line is not longer than 20 meters and that the timing in the code matches the sensor’s specifications. With careful attention to these details, you can create a reliable and accurate temperature monitoring system using the 2.08 inch 256x64 OLED display and a temperature sensor.

About the author

admin is a contributing journalist to Article Daily. Every story on this page is hand-edited by our 42-person newsroom and reviewed before publication.