Electronic – problem with LED matrix and raspberry pi

gpioledled-matrixraspberry pi

I have a raspberry pi model B+ and an 8×8 LED single color matrix. I connected 8 GPIO pins to the 8 row pins of the matrix and another 8 GPIO pins to the 8 column pins. Note that I am a beginner in this, so have some questions. I have observed by a few tests that if I assign Vcc (HIGH) to the rows and ground (LOW) to the colomn, the LED lights up.

def enlight(i, j):
    GPIO.output(row[i],GPIO.HIGH)
    GPIO.output(col[j],GPIO.LOW)

If I want the LED of coordinate (2,3) to light up, I would set row2 to HIGH and col3 to LOW.

The problem arises in the following scenario. I want two LEDS i.e (0,0) and (1,1) to turn on at once.

    enlight(0,0)
    enlight(1,1) 

but instead of only these two, I have two more LEDs turned on i.e (0,1) and (1,0) ; which is quite reasonable because I am setting row0 and row1 HIGH and col0 and col1 LOW. So these 2 pairs of rows and columns are going to intersect in 4 points, hence four points lighting up. To avoid this , I created a delight() function which basically does the opposite of enlight(), i.e setting row to LOW and col to HIGH, turning off LED. In order to light up both (0,0) and (1,1) at once, without any other LEDs turning on, I command it to:

while true:
    enlight(0,0)
    delight(0,0)
    enlight(1,1)
    delight(1,1)

By this, an infinite loop occurs and (0,0) LED is blinking infinitely and just at the moment (0,0) LED is turning off, LED(1,1) is turning on. All of this happen so fast that human eye can't distinguish the blinking and instead sees both LEDs constantly on.

My question is, is this "trick" actually a good trick? Is this how major programs related to LED matrix are done? Or is there any other ways to do so? If there is, please explain in simple terms.

Best Answer

My question is, Is this "trick" actually a good trick? is this how major programs related to LED matrix are done? or is there any other ways to do so? If there is, please explain in simplicity.

You have done what is called Scanning a Multiplex Display. Due to the shared pin setup of Columns and Rows of a Matrix, you will find it Ghosts if multiple pins are enabled at the same time. This is called Ghosting and happens in not only simple LED display matrixes, but Multiplexed Displays, LCDs, even Keyboards (Microsoft article on Keyboard Ghosting)

"Ghosting" is the problem that some keyboard keys don't work when multiple keys are pressed simultaneously. The key presses that don't show up on the computer or seem to have disappeared are said to have been "ghosted". On most keyboards, even some that are explicitly marketed as "Anti-Ghosting," this happens with many three key combinations.

Your Trick, as mentioned, is called scanning. By alternating which row and column are on, so only one is on at any given time, there is no conflict. If done faster than human vision can process (30 times per second (Hertz or Hz) if they are not moving their eyes/heads, but smoother at 60 Hz, and recommended faster than 100 Hz), you take advantage of the phenomenon called Persistence of Vision. Your trick is how all multiplex displays are handled, with some variations.

An Application Note by a leading semiconductor producer on how to avoid ghosting and other common issues. This is targeted to professional engineers but is very informative if you want to learn more.

An alternative to doing this directly, in your code, or even in drivers as the OS for the RPI might block access due to cpu usage, is offloading this to a dedicated LED Display Driver IC. The only difference is that the IC was developed to do the same scanning more efficiently, freeing your main processor from having to keep track of the display.

Additionally, the RPI is not meant to drive much current on it's GPIO pins. A larger display, or one that has LEDs that require more current, will not work well. LED Display Driver ICs tend to support much higher current than the 16mA max per pin of the RPI. But your example of a single color 8x8 display with only 1 led on at a time will be fine.