I am trying to display a number on a Dot Matrix ltp305 display that increases with each button press, enabled by the pressing of a normal button connected to the GPIO. I am sure everything is correctly wired, as both the button and the display work fine on other programs. I am using a Raspberry Pi Zero WH with a breadboard. Here is my code:

import RPi.GPIO as GPIO
from ltp305 import LTP305
import threading

number = 0
left = 0
right = 0
display = LTP305(address=0x61)

def signal_handler(signal, frame):

def button_callback(channel):
    print('Button was pushed! Adding to counter!')
    global number
    number = number + 1
    if number == 99:
        number = 0
    writenumber = str(number)
    global left
    global right
    if len(writenumber) == 2:
        res_first = test_str[0:len(test_str)//2]
        res_second = test_str[len(test_str)//2]
        left = 0
        right = writenumber
    left = int(left)
    right = int(right)
    global display      
    display.set_character(0, left)
    display.set_character(5, right)

GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

forever = threading.Event()

Here is the error message:

Traceback (most recent call last):
  File "/home/pi/Code/GPIO/Pin1-10_DotMatrixCounter.py", line 36, in button_callback
    display.set_character(0, left)
  File "/usr/local/lib/python3.7/dist-packages/ltp305/__init__.py", line 127, in set_character
    char = font[char]
KeyError: 0

I have checked multiple other forums and documentation, but there seems to be nobody who has had the same error message in the same context as me. The only other times I have found KeyError in a forum post is when that error is in their code, and not in the code of a package. A strange observation I found is that all other 'display.set_character()' lines were in a loop, but when I tried that it made no difference. Maybe it is because it is in a defined command.

The button is connected to 3v3 and GPIO15, and the Dot Matrix breakout is connected to the other 3v3, GPIO2, GPIO3, GPIO4 and Ground, as it would usually be if directly attached to the Pi.

  • Only thing I can think of is that left is defined and may not be initialised as you have an if len(writenumber) == 2 condition that does not set left to anything. I cannot see what you are doing with res_first or res_second - copy paste error??? Odd though, I would have thought the int may have errored!
    – user130616
    Apr 4, 2021 at 10:45
  • Oh yeah, I just noticed that! I doubt fixing that will fix this exact problem, but it will probably prevent one in the future. Thanks! Apr 4, 2021 at 11:52


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.