Here is my code to connect a keypad to my Raspberry Pi. Please help me to connect a 4 digit 7 segment using a keypad to my Raspberry Pi using Python.

from pad4pi import rpi_gpio
import time

# Setup Keypad

# same as calling: factory.create_4_by_4_keypad, still we put here fyi:
ROW_PINS = [4, 14, 15, 17] # BCM numbering
COL_PINS = [18, 27, 22, 23] # BCM numbering

factory = rpi_gpio.KeypadFactory()

# Try factory.create_4_by_3_keypad
# and factory.create_4_by_4_keypad for reasonable defaults
keypad = factory.create_keypad(keypad=KEYPAD, row_pins=ROW_PINS, col_pins=COL_PINS)


def printKey(key):
  if (key=="1"):
  elif (key=="A"):

# printKey will be called each time a keypad button is pressed

  • You'll need to specify which display in particular you're using; how it's connected (ideally with a diagram) and what's not working in order for us to help. You can edit your post to add this information.
    – Aurora0001
    Jun 10, 2018 at 14:40
  • 1
    Guessing that you have a 4 digit 7 segment display arranged as a matrix of 28 LEDs, I would suggest tossing it and getting an alpha numeric (Hitachi style interface) LCD with an I2C interface. There are many Python libraries which support such a display. And such a display only requires a few connections to the RPi.
    – st2000
    Jun 10, 2018 at 14:51

1 Answer 1


This article describes how to interface an 4 digit 7 segment LED consisting of a simple matrix of LEDs. For this particular display it is easy to abstract each digit as 4 individual columns and each row as 7 individual rows each connected to the same segment of all digits.

The Python code example contains an array mapping the intended number to the necessary pattern needed to create the number on the display:

num = {' ':(0,0,0,0,0,0,0),

In Python this array is called a Dictionary and the Dictionary keys are the single quoted numbers. When you use the key the Dictionary will return the list of 1's and 0's in parenthesis. This is is used to drive the individual LEDs in the display's matrix.

Consider what the RPi is doing here. The RPi runs an OS which is likely subject to variations in performance as different tasks are executed. At the same time, this program is taxing the RPi with the responsibility to continuously scan this display. Likely, segment brightness variations and flickering due to uneven loading of the RPi will result.

Not the answer you're looking for? Browse other questions tagged or ask your own question.