Bird
0
0
Raspberry Piprogramming~10 mins

Displaying sensor readings on OLED in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Displaying sensor readings on OLED
Start Program
Initialize OLED Display
Initialize Sensor
Read Sensor Data
Format Data for Display
Clear OLED Screen
Display Data on OLED
Wait or Loop
Back to Read Sensor Data
The program starts by setting up the OLED and sensor, then repeatedly reads sensor data, formats it, clears the screen, and shows the data on the OLED.
Execution Sample
Raspberry Pi
import board
import adafruit_ssd1306
import time
import random

display = adafruit_ssd1306.SSD1306_I2C(128, 32, board.I2C())
while True:
    temp = random.uniform(20, 30)
    display.fill(0)
    display.text(f"Temp: {temp:.1f} C", 0, 0, 1)
    display.show()
    time.sleep(1)
This code simulates reading a temperature sensor and shows the temperature on the OLED screen every second.
Execution Table
StepActionSensor ReadingDisplay Buffer ContentOutput on OLED
1Initialize OLED and sensorN/AEmpty screen bufferBlank screen
2Read sensor data25.3Empty (before update)Previous content or blank
3Clear display buffer25.3All pixels offScreen cleared
4Write text to buffer25.3"Temp: 25.3 C" at (0,0)Not yet visible
5Show buffer on OLED25.3"Temp: 25.3 C" in bufferText displayed on screen
6Wait 1 secondN/ABuffer unchangedText visible
7Repeat read sensor26.1Buffer with old textOld text visible
8Clear display buffer26.1All pixels offScreen cleared
9Write new text26.1"Temp: 26.1 C" at (0,0)Not yet visible
10Show buffer on OLED26.1"Temp: 26.1 C" in bufferUpdated text displayed
11Wait 1 secondN/ABuffer unchangedText visible
...Loop continues.........
💡 Program runs indefinitely, updating sensor reading and OLED display every second.
Variable Tracker
VariableStartAfter 1After 2...final
tempN/A25.326.1...
display bufferEmpty"Temp: 25.3 C""Temp: 26.1 C"...
Key Moments - 3 Insights
Why do we clear the display buffer before writing new text?
Clearing the buffer removes old text so the new sensor reading shows clearly without overlapping. See execution_table rows 3 and 8.
When does the text actually appear on the OLED screen?
Text appears only after calling display.show(), which sends the buffer to the OLED hardware. See execution_table rows 5 and 10.
Why does the program loop reading and displaying sensor data?
Sensors change over time, so looping updates the display with fresh readings continuously. See execution_table rows 2-11.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the display buffer content at step 4?
AAll pixels off
B"Temp: 25.3 C" at (0,0)
CEmpty screen buffer
DOld text from previous reading
💡 Hint
Check the 'Display Buffer Content' column at step 4 in the execution_table.
At which step does the OLED screen get cleared before showing new data?
AStep 2
BStep 5
CStep 3
DStep 7
💡 Hint
Look for 'Clear display buffer' action in the execution_table.
If the sensor reading was not updated in the loop, how would the variable 'temp' change in variable_tracker?
AIt would stay the same after start
BIt would increase each iteration
CIt would be empty
DIt would reset to zero
💡 Hint
Refer to the 'temp' row in variable_tracker to see how it changes over iterations.
Concept Snapshot
Displaying sensor readings on OLED:
- Initialize OLED and sensor
- Read sensor data repeatedly
- Clear OLED buffer before writing
- Write formatted text to buffer
- Call display.show() to update screen
- Loop to refresh readings continuously
Full Transcript
This program shows how to display sensor readings on an OLED screen using a Raspberry Pi. First, it sets up the OLED display and sensor. Then it reads the sensor data, clears the OLED screen buffer, writes the new sensor value as text, and updates the OLED display. This process repeats every second to keep the display current. Clearing the buffer before writing prevents overlapping text. The display.show() command sends the buffer content to the screen, making the text visible. The loop ensures the sensor readings and display update continuously.