LED - 2. Driving 8 X 8 Dot Matrix
This document is derived from my book , “Raspberry Pi for Makers"
I'm using a Raspberry Pi 3 and python 2.7.
8X8 Dot matrix is an array of LED elements horizontally and vertically.
It has 64 LED elements. There are many types, from monochrome to RGB. Many easy-to-use display driver modules are also available. LED metrix has a much brighter screen than LCD or OLED. So showing this can be useful when the amount of information is small.
<8X8 Dot Matrix with MAX7219 Driver 8X8 Dot Matrix>
The easiest way to use the dot matrix is to use the display driver for the matrix. This driver can controll the dot matrix using shift registers. Shift registers typically communicate with Arduino or Raspberry Pi using serial communications such as SPI, UART, or I2C. Adafruit also recommends using the 74HC595 or TPIC6B595 shift registers (both manufactured by Texas Instruments) or the MAX7219 LED matrix display driver when using the LED matrix.
I recommend it. To understand how the LED matrix works, it is recommended to use the shift register directly. Use a shift register to fully understand how the LED matrix works, then use high-performance LED drives such as the MAX7219 and TLC5940.
Let's do it.
The basic structure of the 8X8 LED matrix is mostly the same, but the actual pin assignments vary slightly from manufacturer to manufacturer, so be sure to consult the manufacturer's data sheet. For example, the data sheet for the ld788bs LED matrix is shown in the figure below.
As you can see, the LED matrix has a total of 16 pins in two rows, top and bottom. However, these pins are not arranged in the row and column order.
Be careful : The 8X8 LED matrix has different row and column values corresponding to the pins depending on the manufacturer.
In the case of KWM-20882, Adafruit's 8X8 LED matrix, COL values from 1 to 8 in the order of pin number 5,6, 7, 8,16, 15, 14, 13, pin number 1, 2, 3, 5, 12, 11, 10, and 9 are assigned in the order of ROW 1 to 8. So you must refer to the data sheet of the product to find the column and row values for each pin.
If the burner is powered and columns 1 and 3 are connected to ground, the LEDs at positions (1, 1) (1, 3) (4,1) (4, 3) will light. The coordinates here are not the physical coordinates of the actual LED matrix, but the coordinates of the anode and cathode references.
The LED at the intersection of the anode where Vcc is supplied and the cathode connected to ground lights up. In the figure, only 4 LEDs are turned on because current flows
Now consider the following figure again to explain why you need to supply power line by line.
The first row 3 and 4 LEDs are on. Therefore, vertical lines 3 and 4 should be connected to ground. However, to satisfy the second line, only vertical lines 2 and 4 should be connected to ground. That is, do not connect the third column to ground.
Some tricks are needed to solve the above problem. Since we have 16 controllable pins, we should control 64 LEDs in 8 steps as follows.
As shown in the figure above, if you obtain the row value and column value in 8 steps and supply power in order, one image is created. This process is almost the same as for displaying a CRT TV screen. As the image is drawn at high speed, the residual image effect leaves us with a single image.
A total of 16 GPIOs are needed to control the 8X8 LED matrix. We will configure the circuit using only eight GPIO numbers using two 74HC595 shift registers. The 74HC595 shift registers are described in detail in Chapter 2, GPIO.
If you take a slow look it's not difficult, but you have to pay attention because there are many lines. Most operational errors are caused by faulty wire connections.
The anode control shift register determines whether the eight anode pins are on or off. When it is ON, the voltage supplied through Vcc is applied and electricity can flow to the anode pin. However, the final electricity supply is determined by the cathode control shift register.
The LD788BS LED has a rated voltage of 2.01V with 10mA forward current.
The resistance value is obtained from the following formula.
Substituting the above formula, a resistance of approximately 300 Ω is obtained when a 5V supply is connected. If you use a resistor that is slightly larger than this value, it should be fine. If a 3.3V supply is used, a resistance of 129 Ω is obtained. In a power supply below 2.01V, there is no need to connect a resistor.
In the figure above, we created the number 1, and the Bianry values (binary numbers) on the right are eight values automatically created, from B00110000 to B00111000. The first B means that the number is binary format, and 00110000 is off, off, on, on, off, off, off, and off in the order that the first line is lit. And the following Hex values convert the previous binary to hexadecimal. That is, eight 8-bit variables can represent display information of one screen. In the figure above, you can see that the eight values are enclosed in parentheses {}. Save these hexadecimal values. Do the above to 0-9.
Bitmap data from 0 to 9 are as follows.
0x3E, 0x63, 0x73, 0x7B, 0x6F, 0x67, 0x3E, 0x00
0x0C, 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x3F, 0x00
0x1E, 0x33, 0x30, 0x1C, 0x06, 0x33, 0x3F, 0x00
0x1E, 0x33, 0x30, 0x1C, 0x30, 0x33, 0x1E, 0x00
0x38, 0x3C, 0x36, 0x33, 0x7F, 0x30, 0x78, 0x00
0x3F, 0x03, 0x1F, 0x30, 0x30, 0x33, 0x1E, 0x00
0x1C, 0x06, 0x03, 0x1F, 0x33, 0x33, 0x1E, 0x00
0x3F, 0x33, 0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x00
0x1E, 0x33, 0x33, 0x1E, 0x33, 0x33, 0x1E, 0x00
0x1E, 0x33, 0x33, 0x3E, 0x30, 0x18, 0x0E, 0x00
You can download the lib_ledmatrix module used in this python code at this repo.
Run the code.
If you are driving a real large size LED, do not do this type of wiring. You may use a product that has shift registers and already wired. This article explains the use of shift registers because most commercial products work on the same principle. After understanding the principle of operation, it is easier to understand and operate the product by contacting various LED products.
You can download the source codes here(https://github.com/raspberry-pi-maker/IoT)
I'm using a Raspberry Pi 3 and python 2.7.
8X8 Dot matrix is an array of LED elements horizontally and vertically.
It has 64 LED elements. There are many types, from monochrome to RGB. Many easy-to-use display driver modules are also available. LED metrix has a much brighter screen than LCD or OLED. So showing this can be useful when the amount of information is small.
The easiest way to use the dot matrix is to use the display driver for the matrix. This driver can controll the dot matrix using shift registers. Shift registers typically communicate with Arduino or Raspberry Pi using serial communications such as SPI, UART, or I2C. Adafruit also recommends using the 74HC595 or TPIC6B595 shift registers (both manufactured by Texas Instruments) or the MAX7219 LED matrix display driver when using the LED matrix.
I recommend it. To understand how the LED matrix works, it is recommended to use the shift register directly. Use a shift register to fully understand how the LED matrix works, then use high-performance LED drives such as the MAX7219 and TLC5940.
Let's do it.
The basic structure of the 8X8 LED matrix is mostly the same, but the actual pin assignments vary slightly from manufacturer to manufacturer, so be sure to consult the manufacturer's data sheet. For example, the data sheet for the ld788bs LED matrix is shown in the figure below.
As you can see, the LED matrix has a total of 16 pins in two rows, top and bottom. However, these pins are not arranged in the row and column order.
Be careful : The 8X8 LED matrix has different row and column values corresponding to the pins depending on the manufacturer.
In the case of KWM-20882, Adafruit's 8X8 LED matrix, COL values from 1 to 8 in the order of pin number 5,6, 7, 8,16, 15, 14, 13, pin number 1, 2, 3, 5, 12, 11, 10, and 9 are assigned in the order of ROW 1 to 8. So you must refer to the data sheet of the product to find the column and row values for each pin.
Multiplexing
Most LED matrices use multiplexing to save IC driver numbers. First, let's see how LED matrix works for multiplexing. The 8X8 dot matrix contains 64 LEDs. Theoretically, controlling 64 LEDs requires 64 control pins (GPIOs) or IC drives. The following figure shows the power supply from the anode and the connection from the cathode to ground. Eight power inputs and eight ground connections are available. If all 16 are connected to power and ground, all 64 LEDs will light. In the picture below, 1, 4If the burner is powered and columns 1 and 3 are connected to ground, the LEDs at positions (1, 1) (1, 3) (4,1) (4, 3) will light. The coordinates here are not the physical coordinates of the actual LED matrix, but the coordinates of the anode and cathode references.
The LED at the intersection of the anode where Vcc is supplied and the cathode connected to ground lights up. In the figure, only 4 LEDs are turned on because current flows
Now consider the following figure again to explain why you need to supply power line by line.
The first row 3 and 4 LEDs are on. Therefore, vertical lines 3 and 4 should be connected to ground. However, to satisfy the second line, only vertical lines 2 and 4 should be connected to ground. That is, do not connect the third column to ground.
Some tricks are needed to solve the above problem. Since we have 16 controllable pins, we should control 64 LEDs in 8 steps as follows.
As shown in the figure above, if you obtain the row value and column value in 8 steps and supply power in order, one image is created. This process is almost the same as for displaying a CRT TV screen. As the image is drawn at high speed, the residual image effect leaves us with a single image.
Configuring LED Matrix Circuits Using Shift Registers
A total of 16 GPIOs are needed to control the 8X8 LED matrix. We will configure the circuit using only eight GPIO numbers using two 74HC595 shift registers. The 74HC595 shift registers are described in detail in Chapter 2, GPIO.
If you take a slow look it's not difficult, but you have to pay attention because there are many lines. Most operational errors are caused by faulty wire connections.
Shift register connection for the Raspberry Pi and anode control
The GPIO pins of the Raspberry Pi can be connected to other pins below.- Connect GPIO pin 20 and the STCP pin of the 74HC595 shift register.
- Connect GPIO pin 21 and the SHCP pin of the 74HC595 shift register.
- Connect GPIO pin 16 to the OE pin of the 74HC595 shift register.
- Connect GPIO pin 12 and the DS pin of the 74HC595 shift register. We will use GPIO # 12 to pass data to the shift register.
- Connect Vcc and GND of the shift register to the breadboard power and ground. The MR pin is connected to Vcc.
The anode control shift register determines whether the eight anode pins are on or off. When it is ON, the voltage supplied through Vcc is applied and electricity can flow to the anode pin. However, the final electricity supply is determined by the cathode control shift register.
Shift register connection for the Raspberry Pi and cathode control
- Connect GPIO pin 25 and the STCP pin of the 74HC595 shift register.
- Connect GPIO pin 24 and the SHCP pin of the 74HC595 shift register.
- Connect GPIO pin 23 and the OE pin of the 74HC595 shift register.
- Connect GPIO pin 18 and the DS pin of the 74HC595 shift register. We will use GPIO # 20 to pass data to the shift register.
- Connect Vcc and GND of the shift register to the breadboard power and ground. The MR pin is connected to Vcc.
Connection of Shift Registers and LED Matrix
Now finally connect the output pins Q0 to Q7 of the two shift registers to the LED matrix and the wire connection is finished. Find the anode and cathode values mentioned earlier and connect them as follows:- Connect all eight of the anode control shift registers to the LED anodes in order. Refer to the previous figure, not the physical pin assignment, to find and connect the row number.
- The shift control for cathode control connects and connects the eight pins in order to the remaining cathode.
- Make sure to connect via a resistor when connecting.
Calculating the Resistor value
In order to calculate the appropriate resistance value of the resistor used for the cathode, some reference must be made to the data sheet.The LD788BS LED has a rated voltage of 2.01V with 10mA forward current.
The resistance value is obtained from the following formula.
Substituting the above formula, a resistance of approximately 300 Ω is obtained when a 5V supply is connected. If you use a resistor that is slightly larger than this value, it should be fine. If a 3.3V supply is used, a resistance of 129 Ω is obtained. In a power supply below 2.01V, there is no need to connect a resistor.
Driving the dot matrix using Python
I will create and display a number from 0 to 9 on the dot matrix . There are many programs and websites to help with the task converting the numbers to bitmap. Search Google for ‘8x8 led matrix font generator’ to find many free programs or sites. The page (http://blog.riyas.org/2013/12/online-led-matrix-font-generator-with.html) is one of the freely available places.In the figure above, we created the number 1, and the Bianry values (binary numbers) on the right are eight values automatically created, from B00110000 to B00111000. The first B means that the number is binary format, and 00110000 is off, off, on, on, off, off, off, and off in the order that the first line is lit. And the following Hex values convert the previous binary to hexadecimal. That is, eight 8-bit variables can represent display information of one screen. In the figure above, you can see that the eight values are enclosed in parentheses {}. Save these hexadecimal values. Do the above to 0-9.
Bitmap data from 0 to 9 are as follows.
0x3E, 0x63, 0x73, 0x7B, 0x6F, 0x67, 0x3E, 0x00
0x0C, 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x3F, 0x00
0x1E, 0x33, 0x30, 0x1C, 0x06, 0x33, 0x3F, 0x00
0x1E, 0x33, 0x30, 0x1C, 0x30, 0x33, 0x1E, 0x00
0x38, 0x3C, 0x36, 0x33, 0x7F, 0x30, 0x78, 0x00
0x3F, 0x03, 0x1F, 0x30, 0x30, 0x33, 0x1E, 0x00
0x1C, 0x06, 0x03, 0x1F, 0x33, 0x33, 0x1E, 0x00
0x3F, 0x33, 0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x00
0x1E, 0x33, 0x33, 0x1E, 0x33, 0x33, 0x1E, 0x00
0x1E, 0x33, 0x33, 0x3E, 0x30, 0x18, 0x0E, 0x00
You can download the lib_ledmatrix module used in this python code at this repo.
#!/usr/bin/env python import RPi.GPIO as GPIO import numpy as np import lib_ledmatrix NUMBER = [ [0x3E, 0x63, 0x73, 0x7B, 0x6F, 0x67, 0x3E, 0x00], [0x0C, 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x3F, 0x00], [0x1E, 0x33, 0x30, 0x1C, 0x06, 0x33, 0x3F, 0x00], [0x1E, 0x33, 0x30, 0x1C, 0x30, 0x33, 0x1E, 0x00], [0x38, 0x3C, 0x36, 0x33, 0x7F, 0x30, 0x78, 0x00], [0x3F, 0x03, 0x1F, 0x30, 0x30, 0x33, 0x1E, 0x00], [0x1C, 0x06, 0x03, 0x1F, 0x33, 0x33, 0x1E, 0x00], [0x3F, 0x33, 0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x00], [0x1E, 0x33, 0x33, 0x1E, 0x33, 0x33, 0x1E, 0x00], [0x1E, 0x33, 0x33, 0x3E, 0x30, 0x18, 0x0E, 0x00] ] def LED_number(num): led = np.zeros((8,8), dtype=int) for x in range(0, 8): bitMask = 0x01 val = NUMBER[num][x] for y in range(0, 8): if (val & bitMask): led[x][y] = 1 else: led[x][y] = 0 bitMask <<= 1 print led lib_ledmatrix.LED_copy_image(led) lib_ledmatrix.init_lib(8,8,20,21,12,16,25,24,18,23) lib_ledmatrix.LED_Reset() LED_number(3) try: while True: lib_ledmatrix.LED_multiplex() except (KeyboardInterrupt, SystemExit): print("Ctrl C --> Exit...") finally: lib_ledmatrix.LED_Reset() GPIO.cleanup() print "Good bye!"
Run the code.
python 8X8matrix_test.py
Wrapping up
For driving 8X8 LED(64 leds) matrix, you need to do a lot of wiring. If you think you are going to wire the circuit to operate a 640x480 size LED(307,200 leds), you need a huge number of shift registers and a super big size breadboard. And it may take several days for wiring and there should be mal wiring, you may spend for another days to find out the problems. It's terrible.If you are driving a real large size LED, do not do this type of wiring. You may use a product that has shift registers and already wired. This article explains the use of shift registers because most commercial products work on the same principle. After understanding the principle of operation, it is easier to understand and operate the product by contacting various LED products.
You can download the source codes here(https://github.com/raspberry-pi-maker/IoT)
댓글
댓글 쓰기