📺 Displaying Information
Displaying Information
Learn how to show information on LCD and OLED displays - perfect for showing sensor readings and status!
Types of Displays
There are many types of displays you can use with Raspberry Pi. Here are the most common ones:
LCD (16x2)
16 characters, 2 lines
Simple text display, easy to use
OLED
Small, bright, pixel-based
Can show graphics and text
LED Matrix
Grid of LEDs
Can show patterns and animations
Using LCD Display (16x2)
LCD displays are great for showing text. They're easy to use and perfect for displaying sensor readings!
Materials Needed:
- Raspberry Pi
- 16x2 LCD Display (with I2C backpack recommended)
- Jumper wires
- Optional: Potentiometer (for contrast adjustment)
Wiring Instructions (I2C LCD):
- VCC: Connect to 5V on Raspberry Pi
- GND: Connect to GND
- SDA: Connect to GPIO 2 (I2C data line)
- SCL: Connect to GPIO 3 (I2C clock line)
Note: I2C LCD backpacks make wiring much easier - only 4 wires needed!
Installing the Library:
First, install the required library for LCD control:
Basic LCD Code (I2C):
Understanding the Code:
- CharLCD: Creates an LCD object for character display
- address=0x27: I2C address (may be 0x3F - check with i2cdetect)
- cols=16, rows=2: 16 characters per line, 2 lines
- write_string(): Displays text on the screen
- cursor_pos: Sets cursor position (row, column)
- clear(): Clears the entire display
Using OLED Display
OLED displays are smaller but can show graphics. They're great for showing numbers and simple graphics!
OLED Display Code:
Project: Temperature Display with Screen
Let's create a system that reads temperature and displays it on an LCD screen!
Materials Needed:
- Raspberry Pi
- 16x2 LCD Display (I2C)
- Temperature sensor (DHT11 or DS18B20) - or use simulated value
- Jumper wires
The Complete Code:
Understanding the Code:
- The loop continuously reads temperature and updates the display
str(temp)converts the number to text for display- Display updates every 2 seconds
- On exit, shows "Goodbye!" message
Displaying Multiple Values
You can display multiple pieces of information by formatting your text carefully:
Displaying Multiple Values:
Common Mistakes to Avoid
⚠️ Watch Out For:
- Wrong I2C Address: Check address with
i2cdetect -y 1command - Not Converting Numbers: Use
str()to convert numbers to text - Text Too Long: LCD has limited characters - keep text short
- Forgetting to Show: On OLED, call
show()after drawing - Not Clearing: Clear display before writing new content
Summary
You've learned:
- ✅ Displays let you show information visually
- ✅ LCD displays are great for text (16x2 is common)
- ✅ OLED displays can show graphics and text
- ✅ I2C makes wiring displays much easier
- ✅ Use
str()to convert numbers to text for display - ✅ You can display sensor readings, status, and messages
- ✅ Displays update in real-time by using loops
🎮 Try It: Practice with Displays!
Practice writing code for displays. Try these challenges:
📝 Challenge 1: Simple Message
Write code that displays "Hello!" on the first line and "Python!" on the second line of an LCD:
📝 Challenge 2: Display Counter
Write code that displays a counter that counts from 1 to 10, updating every second:
str() to convert numbers to text, and use clear() before writing new content!
🎯 Activity: Temperature Display Project
What You'll Build:
Create a system that displays temperature readings on an LCD screen!
Step-by-Step Instructions:
- Install Library: Install RPLCD library for LCD control
- Wire LCD: Connect I2C LCD to Raspberry Pi (VCC, GND, SDA, SCL)
- Find Address: Use
i2cdetect -y 1to find LCD I2C address - Write Code: Create code to display temperature (simulated or from sensor)
- Test: Run code and verify display shows temperature
- Enhance: Add humidity or other sensor readings
Display Ideas:
- ✅ Show temperature with "Temp: 25.5 C"
- ✅ Show time and date
- ✅ Show sensor status (ON/OFF)
- ✅ Show counter or timer
💪 Practice Challenges
Challenge 1: Welcome Message
Create a display that shows a welcome message for 5 seconds, then clears:
Challenge 2: Scrolling Text
Create text that scrolls across the display (hint: use a loop and change cursor position):
Challenge 3: Multi-Value Display
Display multiple values on the screen:
- Line 1: "Temp: 25C Hum: 60%"
- Line 2: "Status: Normal"
Challenge 4: Status Indicator
Create a display that shows different messages based on temperature:
- If temp > 30: "Status: HOT!"
- If temp 20-30: "Status: Normal"
- If temp < 20: "Status: Cold"