Intermediate Level • Lesson 6

📺 Displaying Information

⏱️ 50 minutes 📚 Intermediate 🎯 Display Control
📺

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):

  1. VCC: Connect to 5V on Raspberry Pi
  2. GND: Connect to GND
  3. SDA: Connect to GPIO 2 (I2C data line)
  4. 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:

# In terminal, type: sudo apt-get update sudo apt-get install python3-smbus pip3 install RPLCD

Basic LCD Code (I2C):

from RPLCD.i2c import CharLCD import time # Initialize LCD (address may vary - check with: i2cdetect -y 1) lcd = CharLCD(i2c_expander='PCF8574', address=0x27, port=1, cols=16, rows=2, dotsize=8) # Clear display lcd.clear() # Write text lcd.write_string("Hello, World!") lcd.cursor_pos = (1, 0) # Move to second line lcd.write_string("Raspberry Pi!") time.sleep(5) lcd.clear()

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:

# Install library first: pip3 install adafruit-circuitpython-ssd1306 import board import digitalio from adafruit_ssd1306 import SSD1306_I2C from PIL import Image, ImageDraw, ImageFont # Initialize I2C and display i2c = board.I2C() oled = SSD1306_I2C(128, 64, i2c, addr=0x3C) # Clear display oled.fill(0) oled.show() # Create image and draw text image = Image.new('1', (128, 64)) draw = ImageDraw.Draw(image) draw.text((0, 0), "Temperature:", fill=255) draw.text((0, 20), "25.5 C", fill=255) # Display the image oled.image(image) oled.show()

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:

from RPLCD.i2c import CharLCD import time # Initialize LCD lcd = CharLCD(i2c_expander='PCF8574', address=0x27, port=1, cols=16, rows=2, dotsize=8) # Simulate temperature reading (replace with actual sensor code) def read_temperature(): # In real project, this would read from DHT11 or DS18B20 return 25.5 # Simulated temperature try: while True: # Read temperature temp = read_temperature() # Clear and update display lcd.clear() lcd.write_string("Temperature:") lcd.cursor_pos = (1, 0) lcd.write_string(str(temp) + " C") # Update every 2 seconds time.sleep(2) except KeyboardInterrupt: lcd.clear() lcd.write_string("Goodbye!") time.sleep(2) lcd.clear()

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:

from RPLCD.i2c import CharLCD import time lcd = CharLCD(i2c_expander='PCF8574', address=0x27, port=1, cols=16, rows=2, dotsize=8) temperature = 25.5 humidity = 60 try: while True: lcd.clear() # First line: Temperature lcd.write_string("Temp: " + str(temperature) + "C") # Second line: Humidity lcd.cursor_pos = (1, 0) lcd.write_string("Humidity: " + str(humidity) + "%") time.sleep(2) except KeyboardInterrupt: lcd.clear()

Common Mistakes to Avoid

⚠️ Watch Out For:

  • Wrong I2C Address: Check address with i2cdetect -y 1 command
  • 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
🎉 Excellent! You can now display information on screens! In the next lesson, you'll learn to control motors.

🎮 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:

💡 Tip: Remember to use 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:

  1. Install Library: Install RPLCD library for LCD control
  2. Wire LCD: Connect I2C LCD to Raspberry Pi (VCC, GND, SDA, SCL)
  3. Find Address: Use i2cdetect -y 1 to find LCD I2C address
  4. Write Code: Create code to display temperature (simulated or from sensor)
  5. Test: Run code and verify display shows temperature
  6. 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
🏆 Bonus Challenge: Can you create a display that shows both temperature and a status message (like "Normal" or "Hot") based on the temperature value?

💪 Practice Challenges

Challenge 1: Welcome Message

Create a display that shows a welcome message for 5 seconds, then clears:

# Display "Welcome!" on line 1 # Display "Raspberry Pi" on line 2 # Wait 5 seconds # Clear display

Challenge 2: Scrolling Text

Create text that scrolls across the display (hint: use a loop and change cursor position):

# Display "Hello World" scrolling from right to left

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"