Displays
-
Not only can you connect a monitor to the Raspberry Pi, but there are also a multitude of different output devices capable of displaying text, numbers, or graphics, and you can even control entire LED panels. To cover each type of existing display in detail would take too much time, so in this chapter, we'll focus on the most practical and interesting displays and some complementary devices.
RGB LEDs consist of three light-emitting diodes combined in a single housing. Another type of LEDs, known as "addressable LEDs," also include a control microchip within their housing. This microchip controls the intensity of the three LED colors using pulse-width modulation (PWM), similar to how it's done with Arduino or Raspberry Pi. It's worth noting that the control microchip is integrated directly into the LED itself.
Addressable LEDs are designed for controlling a large number of them using a single microcontroller or computer, such as Arduino or Raspberry Pi. You can purchase addressable LEDs at Adafruit, which they call NeoPixels (note that the name NeoPixels is used quite frequently, especially on websites and descriptions of other addressable LEDs unrelated to Adafruit). The most popular of these are WS2812 type addressable LEDs. They support a standard serial data transmission, allowing you to create long chains of these LEDs to build large displays. In addition to using them as RGB addressable LEDs, WS2812 LEDs can also be used as single-color light emitters.
Sometimes, addressable LEDs are arranged in a matrix form, but you can also purchase them as a roll of tape from which you can cut LED strips of the desired length for your project (see the image below).
In Figure 14.1, you can see that the tape is divided into segments, resembling a ribbon or a roll of film. Three wires on each side of the LED carry ground (GND) and 5V power supply (5V) as well as serial data D0, which is transmitted from one LED to another throughout the strand. In other words, one LED's output is connected to the input of the next. A vertical bar is drawn in the middle of the segments, indicating the spot to cut the LED strip if you need to shorten it.
Pay attention to the arrows on the tape, indicating the direction of the serial data flow. According to this direction, when connecting LEDs, you should always attach to the left side of the strip.
Addressable LEDs are designed for controlling a large number of them using a single microcontroller or computer, such as Arduino or Raspberry Pi. You can purchase addressable LEDs at Adafruit, which they call NeoPixels (note that the name NeoPixels is used quite frequently, especially on websites and descriptions of other addressable LEDs unrelated to Adafruit). The most popular of these are WS2812 type addressable LEDs. They support a standard serial data transmission, allowing you to create long chains of these LEDs to build large displays. In addition to using them as RGB addressable LEDs, WS2812 LEDs can also be used as single-color light emitters.
Sometimes, addressable LEDs are arranged in a matrix form, but you can also purchase them as a roll of tape from which you can cut LED strips of the desired length for your project (see the image below).
In Figure 14.1, you can see that the tape is divided into segments, resembling a ribbon or a roll of film. Three wires on each side of the LED carry ground (GND) and 5V power supply (5V) as well as serial data D0, which is transmitted from one LED to another throughout the strand. In other words, one LED's output is connected to the input of the next. A vertical bar is drawn in the middle of the segments, indicating the spot to cut the LED strip if you need to shorten it.
Pay attention to the arrows on the tape, indicating the direction of the serial data flow. According to this direction, when connecting LEDs, you should always attach to the left side of the strip.
When acquiring one or two meters of LED strip, you'll likely want to try it out in practice. Connecting it to an Arduino is quite straightforward (see the diagram), but connecting the strip to a Raspberry Pi will require some additional effort.
Components
To conduct this experiment with Arduino, you will need the following components:
1. WS2812 Addressable LED strip
2. Three "male-to-male" jumper wires
Since the logic input of NeoPixels LEDs is not compatible with 3V logic signals, additional components are needed for the Raspberry Pi experiment. However, before taking additional steps, it's worth trying to work without a level shifter, as your LED strip may function properly with 3V logic signals.
Nevertheless, for the Raspberry Pi experiment, you may need the following additional components:
1. 400-point breadboard
2. R1, R2: Two 470 Ohm 0.25W resistors
3. Q1: MOSFET transistor 27000
4. 01: "female-to-male" jumper wires
Note that for this experiment, a low-power MOSFET transistor 27000 is used, as it handles high-frequency serial data better compared to 2N3904. You can also use a transistor like FQP30N06L, but its power handling capability would be excessive for this experiment.
Raspberry Pi Connection
To connect your Raspberry Pi to the NeoPixel LED strip, try connecting it without a level shifter first. Here's how you can do it:
1. Using "female-to-female" jumper wires, create the following connections:
- GND (NeoPixel LED strip) -> GND (Raspberry Pi)
- 5V (NeoPixel LED strip) -> 5V (Raspberry Pi)
- D0 (NeoPixel LED strip) -> GPIO 18 (Raspberry Pi)
The image below shows the direct connection of the LED strip to the Raspberry Pi.
Proceed to the "Raspberry Pi Program" section and try running the program. If the LED strip doesn't light up and display a colorful, multi-color display, you may need to add a level shifter to increase the signal level from 3V to 5V.
Level Shifting from 3V to 5V:
The 3V signal level on the Raspberry Pi is lower than the expected "high" level for WS2812 addressable LEDs, which is 4V. The diagram shown below demonstrates how to use an MOSFET transistor to raise the signal level to 5V.
A side effect of such a level shift is signal inversion. This means that when the GPIO pin on the Raspberry Pi is set to a logic low (0V), the output to the LED strip will be a 5V signal, and when the GPIO pin on the Raspberry Pi is set to a logic high (3.3V), the output to the LED strip will be a 0V signal.
Fortunately, this feature can be easily corrected in the control program.
The image shows level shifting from 3 to 5V.
The image below shows the connection of the LED strip to the Raspberry Pi using a level shifter.
Raspberry Pi Program
The Raspberry Pi program is based on the guide provided by Adafruit (see [https://learn.adafruit.com/neopixels-on-raspberry-pi](https://learn.adafruit.com/neopixels-on-raspberry-pi)). However, the C library on which Adafruit's guide relied was incompatible with the Raspberry Pi 2 at the time the book was prepared.
Fortunately, for Raspberry Pi 2 owners, Richard Hurst created a version of the program that works with Raspberry Pi 2. This version is also compatible with earlier versions of the Raspberry Pi. To install the mentioned library and necessary software packages, use the following command:
$ sudo apt-get install build-essential python-dev git scons swig
The next step diverges from the direction provided in the Adafruit guide because you need to obtain the program version modified for use with Raspberry Pi 2. Fetch the modified NeoPixel code from GitHub using the following command:
$ git clone https://github.com/richardghirst/rpi_ws281x.git
Modify the extracted code from GitHub and then build the C code using the following command:
$ scons
After compiling the C code, you need to install the Python library that interacts with the fast C code using the following command:
$ cd python
$ sudo python setup.py install
Downloading and Running the Program
Here is the program version designed for use with an inverting level shifter. Let's go over some key points in the program marked by comments:
1. Import the NeoPixel library.
2. These configuration parameters don't require changes unless you need to use a different pin for LED_PIN.
3. The main program loop performs nearly the same actions as its Arduino counterpart. It generates a random color and applies it to a pixel.
Components
To conduct this experiment with Arduino, you will need the following components:
1. WS2812 Addressable LED strip
2. Three "male-to-male" jumper wires
Since the logic input of NeoPixels LEDs is not compatible with 3V logic signals, additional components are needed for the Raspberry Pi experiment. However, before taking additional steps, it's worth trying to work without a level shifter, as your LED strip may function properly with 3V logic signals.
Nevertheless, for the Raspberry Pi experiment, you may need the following additional components:
1. 400-point breadboard
2. R1, R2: Two 470 Ohm 0.25W resistors
3. Q1: MOSFET transistor 27000
4. 01: "female-to-male" jumper wires
Note that for this experiment, a low-power MOSFET transistor 27000 is used, as it handles high-frequency serial data better compared to 2N3904. You can also use a transistor like FQP30N06L, but its power handling capability would be excessive for this experiment.
Raspberry Pi Connection
To connect your Raspberry Pi to the NeoPixel LED strip, try connecting it without a level shifter first. Here's how you can do it:
1. Using "female-to-female" jumper wires, create the following connections:
- GND (NeoPixel LED strip) -> GND (Raspberry Pi)
- 5V (NeoPixel LED strip) -> 5V (Raspberry Pi)
- D0 (NeoPixel LED strip) -> GPIO 18 (Raspberry Pi)
The image below shows the direct connection of the LED strip to the Raspberry Pi.
Proceed to the "Raspberry Pi Program" section and try running the program. If the LED strip doesn't light up and display a colorful, multi-color display, you may need to add a level shifter to increase the signal level from 3V to 5V.
Level Shifting from 3V to 5V:
The 3V signal level on the Raspberry Pi is lower than the expected "high" level for WS2812 addressable LEDs, which is 4V. The diagram shown below demonstrates how to use an MOSFET transistor to raise the signal level to 5V.
A side effect of such a level shift is signal inversion. This means that when the GPIO pin on the Raspberry Pi is set to a logic low (0V), the output to the LED strip will be a 5V signal, and when the GPIO pin on the Raspberry Pi is set to a logic high (3.3V), the output to the LED strip will be a 0V signal.
Fortunately, this feature can be easily corrected in the control program.
The image shows level shifting from 3 to 5V.
The image below shows the connection of the LED strip to the Raspberry Pi using a level shifter.
Raspberry Pi Program
The Raspberry Pi program is based on the guide provided by Adafruit (see [https://learn.adafruit.com/neopixels-on-raspberry-pi](https://learn.adafruit.com/neopixels-on-raspberry-pi)). However, the C library on which Adafruit's guide relied was incompatible with the Raspberry Pi 2 at the time the book was prepared.
Fortunately, for Raspberry Pi 2 owners, Richard Hurst created a version of the program that works with Raspberry Pi 2. This version is also compatible with earlier versions of the Raspberry Pi. To install the mentioned library and necessary software packages, use the following command:
$ sudo apt-get install build-essential python-dev git scons swig
The next step diverges from the direction provided in the Adafruit guide because you need to obtain the program version modified for use with Raspberry Pi 2. Fetch the modified NeoPixel code from GitHub using the following command:
$ git clone https://github.com/richardghirst/rpi_ws281x.git
Modify the extracted code from GitHub and then build the C code using the following command:
$ scons
After compiling the C code, you need to install the Python library that interacts with the fast C code using the following command:
$ cd python
$ sudo python setup.py install
Downloading and Running the Program
Here is the program version designed for use with an inverting level shifter. Let's go over some key points in the program marked by comments:
Code: Select all
#!/usr/bin/env python
import time, random
from neopixel import * #1
# LED strip
LED_COUNT = 30 # Number of LED pixels. #2
LED_PIN = 18 # GPIO pin connected to the pixels (must support PWM!).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 5 # DMA channel to use for generating signal (try 5)
LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
# Initialize the display
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
strip.begin()
while True: #3
for i in range(strip.numPixels()):
red = random.randint(0, 255)
green = random.randint(0, 255)
blue = random.randint(0, 255)
strip.setPixelColor(i, Color(red, green, blue))
strip.show()
time.sleep(0.1)
2. These configuration parameters don't require changes unless you need to use a different pin for LED_PIN.
3. The main program loop performs nearly the same actions as its Arduino counterpart. It generates a random color and applies it to a pixel.
Raspberry Pi can be connected to any monitor, whether it's large or small, as long as it has HDMI or AV ports. However, there are times when you need to display just a few lines of information on the screen. Arduino Uno lacks any video output altogether, and in such cases, a small display capable of showing graphics or a few lines of text can be quite handy.
Small OLED (Organic Light-Emitting Diode) displays offer affordability, low power consumption, and provide clear and easily readable images (see the image below). In many consumer products, they are replacing liquid crystal displays. OLED displays are available in both monochrome and color variations.
The most common type of OLED display is a module that includes the display itself and a control microchip on a printed circuit board. Typically, these displays are controlled using the SSD1306 microchip, which has an I2C (pronounced "eye-two-see") interface, requiring only two pins for data transmission and two pins for power.
These displays have a small size and high resolution, so when using Arduino, there may be a memory shortage issue if appropriate precautions are not taken.
Small OLED (Organic Light-Emitting Diode) displays offer affordability, low power consumption, and provide clear and easily readable images (see the image below). In many consumer products, they are replacing liquid crystal displays. OLED displays are available in both monochrome and color variations.
The most common type of OLED display is a module that includes the display itself and a control microchip on a printed circuit board. Typically, these displays are controlled using the SSD1306 microchip, which has an I2C (pronounced "eye-two-see") interface, requiring only two pins for data transmission and two pins for power.
These displays have a small size and high resolution, so when using Arduino, there may be a memory shortage issue if appropriate precautions are not taken.
OLED displays with I²C interface can be used with both Arduino and Raspberry Pi. In this chapter, we will explore an experiment where we replace a temperature LED indicator with an OLED display that shows the current and set temperatures.
This experiment is primarily intended to demonstrate how to use the display with a Raspberry Pi. The experiment's program displays the current time and a small animation (see the image below).
Components
The OLED display has four pins that should be connected directly to the Raspberry Pi using four "female-to-female" jumper wires.
Make sure your OLED display has a resolution of 128x64 pixels and uses the SSD1306 control chip. Some displays may have additional pins on the SSD1306 that are not used, so for simplicity, choose a display with four pins: GND (ground), VCC (+5V), SDA (data), and SCL (clock). The experiment can be conducted with either a monochrome or a color OLED display.
Connection to Raspberry Pi
The SSD1306 chip can operate at 3V or 5V. Since earlier models of the Raspberry Pi might not provide sufficient current at 3V, it's recommended to connect the chip to 5V.
Connect the wires as follows:
- Connect the GND wire on the OLED display to the GND on the Raspberry Pi.
- Connect the VCC wire on the OLED display to the 5V on the Raspberry Pi.
- Connect the SCL (clock) wire on the OLED display to GPIO 3 on the Raspberry Pi.
- Connect the SDA (data) wire on the OLED display to GPIO 2 on the Raspberry Pi.
Raspberry Pi Program
If your Raspberry Pi isn't already set up to work with the I²C interface, refer to the sections above and configure it as described in the "Setting Up I²C on Raspberry Pi" section.
To work with the OLED display, you can use the Python library developed by Adafruit. It works well with OLED displays controlled by the SSD1306 chip, regardless of their manufacturer. To install this library, execute the following commands:
$ git clone https://github.com/adafruit/Adafruit-SS ... rduino.git
$ cd Adafruit_Python_SSD1306
$ sudo python setup.py install
Display Coordinate System
OLED displays are graphic displays where you can draw shapes and output text. To work with them, you need to specify coordinates for elements on the screen. The Adafruit library uses a coordinate system where the top left corner of the screen has coordinates (0, 0), and the bottom right corner has (127, 63).
Here's the program for this experiment:
Let's break it down step by step:
1. Two modules are imported from the OLED library: `SSD1306`, which is used for working with the device, and `Image`, which is used to create a canvas on which shapes and text will be drawn.
2. The `PIL` library (Python Imaging Library) is also imported.
3. A variable `device` is created, which provides access to the OLED display. The value `0x3C` is the I²C device address of the OLED display. This address can vary depending on the display manufacturer, so it's important to check it in your display's documentation.
4. The font used, which is 24 pixels in height, is specified for displaying the time on the screen.
5. Using the `with ... as` construct, a code block is created for displaying text on the screen.
6. Within this block, a shape representing the "Pac-Man" game animation is drawn. The shape is 30x30 pixels in size, and the angle of the open mouth ranges from -45° to 45°, creating the impression of movement. The color of the shape is set to 255, which corresponds to white.
7. The variable `x` is incremented by 10 on each iteration, allowing the animation to move to the right.
8. The current time is obtained, converted to a string, and displayed on the screen.
Running the Program
To run the program, execute the following command:
$ sudo python oled.py
If nothing is displayed on the screen, the issue may be related to an incorrectly specified I²C device address. Make sure the address is correct.
In this example, the `pieslice` function is used, which draws a circle with a cutout portion. You can also use other functions to draw various graphical elements. Additional examples and functions can be found on the website http://effbot.org/imagingbook/imagedraw.htm. Try different variations to bring your OLED display to life!
This experiment is primarily intended to demonstrate how to use the display with a Raspberry Pi. The experiment's program displays the current time and a small animation (see the image below).
Components
The OLED display has four pins that should be connected directly to the Raspberry Pi using four "female-to-female" jumper wires.
Make sure your OLED display has a resolution of 128x64 pixels and uses the SSD1306 control chip. Some displays may have additional pins on the SSD1306 that are not used, so for simplicity, choose a display with four pins: GND (ground), VCC (+5V), SDA (data), and SCL (clock). The experiment can be conducted with either a monochrome or a color OLED display.
Connection to Raspberry Pi
The SSD1306 chip can operate at 3V or 5V. Since earlier models of the Raspberry Pi might not provide sufficient current at 3V, it's recommended to connect the chip to 5V.
Connect the wires as follows:
- Connect the GND wire on the OLED display to the GND on the Raspberry Pi.
- Connect the VCC wire on the OLED display to the 5V on the Raspberry Pi.
- Connect the SCL (clock) wire on the OLED display to GPIO 3 on the Raspberry Pi.
- Connect the SDA (data) wire on the OLED display to GPIO 2 on the Raspberry Pi.
Raspberry Pi Program
If your Raspberry Pi isn't already set up to work with the I²C interface, refer to the sections above and configure it as described in the "Setting Up I²C on Raspberry Pi" section.
To work with the OLED display, you can use the Python library developed by Adafruit. It works well with OLED displays controlled by the SSD1306 chip, regardless of their manufacturer. To install this library, execute the following commands:
$ git clone https://github.com/adafruit/Adafruit-SS ... rduino.git
$ cd Adafruit_Python_SSD1306
$ sudo python setup.py install
Display Coordinate System
OLED displays are graphic displays where you can draw shapes and output text. To work with them, you need to specify coordinates for elements on the screen. The Adafruit library uses a coordinate system where the top left corner of the screen has coordinates (0, 0), and the bottom right corner has (127, 63).
Here's the program for this experiment:
Code: Select all
#!/usr/bin/env python
from oled.device import ssd1306 # 1
from oled.render import canvas
from PIL import ImageFont # 2
import time
device = ssd1306(port=1, address=0x3C) # 3
large_font = ImageFont.truetype('FreeMono.ttf', 24) # 4
x = 0
while True:
with canvas(device) as draw: # 5
draw.pieslice((x, 30, x+30, 60), 45, -45, fill=255) # 6
x += 10 # 7
if x > 128:
x = 0
now = time.localtime() # 8
draw.text((0, 0), time.strftime('%H:%M:%S', now), font=large_font, fill=255)
time.sleep(0.1)
1. Two modules are imported from the OLED library: `SSD1306`, which is used for working with the device, and `Image`, which is used to create a canvas on which shapes and text will be drawn.
2. The `PIL` library (Python Imaging Library) is also imported.
3. A variable `device` is created, which provides access to the OLED display. The value `0x3C` is the I²C device address of the OLED display. This address can vary depending on the display manufacturer, so it's important to check it in your display's documentation.
4. The font used, which is 24 pixels in height, is specified for displaying the time on the screen.
5. Using the `with ... as` construct, a code block is created for displaying text on the screen.
6. Within this block, a shape representing the "Pac-Man" game animation is drawn. The shape is 30x30 pixels in size, and the angle of the open mouth ranges from -45° to 45°, creating the impression of movement. The color of the shape is set to 255, which corresponds to white.
7. The variable `x` is incremented by 10 on each iteration, allowing the animation to move to the right.
8. The current time is obtained, converted to a string, and displayed on the screen.
Running the Program
To run the program, execute the following command:
$ sudo python oled.py
If nothing is displayed on the screen, the issue may be related to an incorrectly specified I²C device address. Make sure the address is correct.
In this example, the `pieslice` function is used, which draws a circle with a cutout portion. You can also use other functions to draw various graphical elements. Additional examples and functions can be found on the website http://effbot.org/imagingbook/imagedraw.htm. Try different variations to bring your OLED display to life!