How to display a thermometer on a 1.54 inch 128x64 OLED?
How to Display a Thermometer on a 1.54 inch 128x64 OLED
To display a thermometer on a 1.54 inch 128x64 oled display, you need to connect a temperature sensor like the DS18B20 or DHT22 to a microcontroller (e.g., Arduino, ESP32, or STM32), read the temperature data, and then render a graphical thermometer bar on the OLED using a library such as Adafruit_SSD1306 or U8g2. The display itself has a resolution of 128x64 pixels, which is enough to show a vertical thermometer scale from 0°C to 100°C with a moving mercury column, plus a numeric readout. The SPI interface is preferred for speed, especially if you want to update the display at 10 Hz or faster. The DS18B20 offers ±0.5°C accuracy from -10°C to +85°C, while the DHT22 covers -40°C to 80°C with ±0.5°C accuracy. For a reliable build, use a 4.7kΩ pull-up resistor on the DS18B20 data line, and connect the OLED to the microcontroller’s SPI pins: SCK (clock), MOSI (data), DC (data/command), CS (chip select), and RST (reset). The typical operating voltage for both the OLED and sensor is 3.3V or 5V, depending on your module. The 1.54 inch 128x64 oled display uses the SSD1309 driver, which is compatible with the Adafruit_SSD1306 library after minor tweaks. The display’s pixel pitch is 0.21mm, giving a clear viewable area of 35.0mm x 17.5mm. The thermometer graphic should occupy at least 40 pixels in width and 60 pixels in height to be legible, leaving room for the temperature label at the top. The refresh rate of the OLED is around 100 Hz, but the sensor update rate is typically 1 Hz for the DS18B20 (12-bit resolution) or 2 Hz for the DHT22. This mismatch means you can cache the last reading and only redraw the thermometer when the temperature changes by more than 0.1°C, reducing flicker and power consumption. The OLED consumes about 20mA when active, but you can drop to 0.1mA in sleep mode, which is useful for battery-powered projects. For the thermometer design, use a filled rectangle for the mercury column, scaled linearly from the bottom of the display (0°C) to the top (100°C). For example, at 25°C, the column height is 25% of the total height, so 16 pixels if the scale is 64 pixels tall. The background should be black (pixel off) and the mercury column white (pixel on) for maximum contrast. The OLED’s contrast ratio is 2000:1, so the white bar is clearly visible even in moderate ambient light. The viewing angle is 160 degrees, so the thermometer is readable from the side. The operating temperature range of the OLED is -30°C to +70°C, which matches the sensor range. The DS18B20 has a 64-bit serial number, so you can daisy-chain multiple sensors on one wire, but for a single thermometer, one sensor is enough. The wiring is straightforward: connect the OLED VCC to 3.3V, GND to GND, SCK to pin 13 (Arduino Uno), MOSI to pin 11, DC to pin 9, CS to pin 10, and RST to pin 8. For the DS18B20, connect VDD to 3.3V, GND to GND, and DQ to pin 2 with a 4.7kΩ resistor to VDD. The code for the thermometer uses the OneWire library for the sensor and the Adafruit_SSD1306 library for the display. The initialization sequence sets the display to 128x64 pixels, 1-bit color, and SPI mode. The main loop reads the temperature, converts it to a float, and then calculates the y-coordinate of the mercury top. The formula is: y_top = 64 - (temperature / 100.0) * 60, where 60 is the height of the scale in pixels, leaving a 4-pixel margin at the top and bottom. The thermometer graphic is drawn using display.drawRect() for the outer tube and display.fillRect() for the mercury. The tube is 10 pixels wide, centered at x=10, so the mercury is 8 pixels wide. The numeric readout is displayed at x=30, y=0 using display.setCursor() and display.print(). The font size is 1, which gives 8x8 pixels per character, so a 3-digit number fits in 24 pixels. The temperature is formatted to one decimal place, e.g., "25.3C". The display.update() command sends the buffer to the OLED. The total frame buffer size is 1024 bytes (128x64/8), which fits in the Arduino Uno’s 2KB SRAM. If you use an ESP32, you have more RAM and can add a gradient or color effect, but the OLED is monochrome, so you rely on dithering. The DS18B20 conversion time is 750ms at 12-bit, so you can read it every second. The DHT22 is faster at 2 seconds per reading but has a lower resolution of 0.1°C. For a more accurate thermometer, use the DS18B20 with a 10-bit resolution (187.5ms conversion) and average 10 readings to reduce noise. The standard deviation of the DS18B20 is 0.1°C, so the display shows stable values. The OLED’s SPI clock speed is 4 MHz, so the display update takes about 2ms, leaving plenty of time for sensor reading. The power consumption of the whole system is 50mA, which is fine for a USB-powered project. The thermometer can be calibrated by comparing the reading with a reference thermometer in a water bath. The offset is typically less than 0.5°C, but you can add a software correction factor. The display’s pixel layout is 128 columns and 64 rows, with the origin at the top-left. The thermometer tube is drawn from row 4 to row 60, so the mercury starts at the bottom and rises. The scale marks can be added every 10°C, using small horizontal lines 3 pixels long at the corresponding y-coordinates. For example, 0°C is at y=60, 10°C at y=54, 20°C at y=48, etc. The labels "0", "10", "20" are placed to the left of the tube. The font size 1 is readable, but you can use a custom 5x7 font for smaller characters. The OLED’s driver supports vertical scrolling, but it’s not needed for a static thermometer. The display has a built-in charge pump, so no external voltage booster is needed for 3.3V operation. The contrast can be adjusted via software with a command to the SSD1309, setting the contrast register from 0 to 255. A value of 128 is typical for indoor use. The display’s lifetime is 100,000 hours, so it lasts for years. The thermometer project is suitable for a weather station, a lab monitor, or a home automation panel. The code can be extended to log temperature data to an SD card or send it via Wi-Fi. The ESP32 version uses the WiFi library to post data to a server. The OLED can also display a trend arrow or a color-coded background (using dithering) to indicate hot or cold. The DS18B20 is waterproof in a stainless steel probe, so it can be used in liquids. The DHT22 is not waterproof, so it’s for air temperature only. The choice depends on your application. The wiring diagram is simple: use a breadboard or a custom PCB. The OLED module has a 7-pin header: GND, VCC, SCK, MOSI, DC, CS, RST. The DS18B20 has three wires: red (VCC), black (GND), and yellow (data). The 4.7kΩ resistor connects between red and yellow. The Arduino Uno’s 5V output can power both, but the OLED’s logic level is 3.3V, so you need a level shifter if you use 5V logic. Many OLED modules have a 3.3V regulator, so they accept 5V input. Check the datasheet. The DS18B20 works at 3.3V or 5V. The code is written in Arduino IDE, using the OneWire library (version 2.3.7) and the Adafruit SSD1306 library (version 2.5.7). The initialization of the OLED is done in setup(): display.begin(SSD1306_SWITCHCAPVCC, 0x3C) for I2C, but for SPI, you use display.begin(SSD1306_SWITCHCAPVCC, CS, DC, RST). The address 0x3C is for I2C, but SPI doesn’t use an address. The display.clearDisplay() clears the buffer. The delay(1000) in the loop sets the update rate. The sensor reading is done with sensors.requestTemperatures() and then sensors.getTempCByIndex(0). The result is a float. The thermometer graphic is drawn using a function that takes the temperature as input. The function calculates the y-coordinate, then draws the tube and mercury. The tube is a rectangle from (5,4) to (15,60), with a 1-pixel border. The mercury is a filled rectangle from (6, y_top) to (14, 60). The numeric readout is displayed at (20, 0) with a degree symbol. The degree symbol is ASCII 176, but the default font doesn’t have it, so you can use a custom character or print "C" instead. The display.display() sends the buffer. The total code size is about 8KB, which fits in the Arduino Uno’s 32KB flash. The RAM usage is 1.5KB, leaving 0.5KB for variables. The thermometer can be calibrated by adding a constant offset to the temperature reading. For example, if the sensor reads 0.5°C low, add 0.5. The offset is determined by comparing with a reference thermometer. The DS18B20 has a typical accuracy of ±0.5°C, but the offset can be up to 1°C. The display’s contrast can be adjusted in the code using display.setContrast(128). The contrast is linear, so 0 is dim and 255 is bright. The OLED’s brightness is 120 cd/m² at full contrast, which is enough for indoor use. The viewing angle is 160 degrees, so the thermometer is readable from the side. The display’s response time is 5ms, so there is no ghosting. The thermometer project can be powered by a 9V battery with a 5V regulator, or by USB. The power consumption is 50mA, so a 9V battery lasts about 10 hours. The ESP32 version consumes 80mA with Wi-Fi on, but you can use deep sleep to extend battery life. The OLED can be turned off between readings to save power. The sensor reading takes 750ms, so you can turn on the display for 100ms, then turn it off. The total power consumption is then 20mA * 0.1 + 0.1mA * 0.9 = 2.1mA, which is much lower. The DS18B20 has a parasitic power mode, where it draws power from the data line, but it’s not recommended for long cables. Use external power for reliability. The wiring distance for the DS18B20 is up to 100m with a twisted pair, but the display should be close to the microcontroller. The SPI bus is limited to 1m. The thermometer can be mounted in a weatherproof enclosure with a hole for the sensor. The OLED is visible through a clear window. The project is suitable for a beginner with basic soldering skills. The code is available on GitHub, but you can write it from scratch. The key is to map the temperature to the pixel coordinates. The scale is linear, so the formula is straightforward. The display’s buffer is a bit array, so you set pixels using setPixel(x, y). The fillRect function is faster because it sets multiple pixels at once. The thermometer graphic can be animated by gradually filling the mercury column as the temperature changes. This is done by drawing the mercury from the previous y to the new y, creating a smooth transition. The animation speed is 10ms per pixel, so a 60-pixel change takes 600ms. The sensor reading is updated every second, so the animation is smooth. The display’s update rate is 100 Hz, so the animation is fluid. The thermometer can also show the minimum and maximum temperatures, stored in EEPROM. The EEPROM has 100,000 write cycles, so it’s fine for daily updates. The temperature range is stored as a float, but you can store it as an integer by multiplying by 10. The display shows the current, min, and max values. The min and max are reset by a button. The button is connected to a digital pin with a pull-up resistor. The debounce time is 50ms. The code reads the button state and resets the values. The thermometer can also have a backlight, but the OLED is self-emissive, so no backlight is needed. The display’s thickness is 1.2mm, so it fits in a slim enclosure. The sensor probe is 6mm in diameter, so it fits in a standard thermowell. The project is a great way to learn about sensors and displays. The cost is about $10 for the OLED, $5 for the sensor, and $5 for the Arduino clone. The total is $20, which is cheap for a custom thermometer. The accuracy is better than a mercury thermometer, and the response time is faster. The DS18B20 responds in 2 seconds, while the DHT22 responds in 5 seconds. The OLED updates instantly. The thermometer can be used in a fridge, oven, or aquarium. The sensor range is -55°C to +125°C for the DS18B20, so it’s suitable for most applications. The display’s temperature range is -30°C to +70°C, so it’s the limiting factor. The OLED can be damaged if the temperature exceeds 70°C, so keep it away from heat sources. The sensor can be placed in the hot area, and the display in the cool area. The wiring length is up to 10m for the sensor. The display uses a ribbon cable, so it’s flexible. The project can be expanded with a real-time clock for logging, or a buzzer for alarms. The alarm threshold is set in the code. The buzzer beeps when the temperature exceeds a limit. The buzzer is connected to a transistor to handle the current. The OLED can also display a graph of the temperature over time. The graph uses the 128x64 pixels, with the x-axis as time and the y-axis as temperature. The graph scrolls left as new data is added. The graph is updated every minute. The data is stored in an array of 128 values, one per pixel. The array is shifted left every minute. The graph shows the last 128 minutes of data. The graph is drawn using lines between consecutive points. The line function is part of the Adafruit library. The graph is useful for tracking trends. The thermometer can also be connected to a computer via serial, sending data to a PC. The serial baud rate is 115200. The data is in CSV format: time, temperature. The PC can log the data to a file. The project is open-source, so you can modify it. The code is written in C++, but you can use MicroPython on an ESP32. The MicroPython version uses the ssd1306 library. The code is shorter but slower. The performance is similar. The choice depends on your preference. The thermometer is a practical project that teaches you about hardware and software. The result is a functional device that you can use every day. The display is bright and clear, and the sensor is accurate. The project is a good starting point for more complex projects like a weather station. The OLED can also show a battery level indicator if you use a voltage divider. The battery voltage is read with an analog pin. The voltage is mapped to a percentage. The indicator is a bar at the top of the display. The bar is 10 pixels wide and 5 pixels tall. The battery icon is drawn with a rectangle and a small terminal. The voltage is displayed as a number. The battery level is updated every minute. The project is self-contained and portable. The enclosure is 3D-printed or a plastic box. The dimensions are 50mm x 30mm x 20mm. The display is mounted on the front, and the sensor is on a cable. The cable is 1m long. The project is powered by a 18650 battery with a 5V boost converter. The battery lasts 24 hours on a single charge. The battery is charged via USB. The project is a great gift for a science enthusiast. The thermometer is accurate to 0.1°C, which is better than most household thermometers. The display is easy to read from a distance of 1m. The font size is 8 pixels, so the characters are 2mm tall. The temperature is readable at arm’s length. The thermometer can be calibrated with a known temperature source, like ice water (0°C) or boiling water (100°C). The calibration is done in software. The offset is stored in EEPROM. The thermometer is ready to use after calibration. The project is a success. The key is to test each component separately before assembling. The OLED is tested with the Adafruit example code. The sensor is tested with the OneWire example. The combination is tested with the thermometer code. The debugging is done with serial print. The serial monitor shows the temperature and the y-coordinate. The display shows the graphic. The troubleshooting is straightforward. The common issues are loose connections, wrong pins, or incorrect library versions. The libraries are installed from the Arduino Library Manager. The versions are compatible. The project is robust and reliable. The thermometer is a perfect example of a practical IoT device. The skills learned are transferable to other projects. The display is a key component, and the 1.54 inch 128x64 oled display is a great choice for its size
320,000+ issues. Every publisher. Zero subscriptions.
Same-day digital releases from Marvel, DC, Image, IDW, Dark Horse, Boom!, Dynamite, and Oni Press — readable in your browser in under three seconds.
Start Reading Free