-1

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
KEYPAD = [
        ["1","2","3","A"],
        ["4","5","6","B"],
        ["7","8","9","C"],
        ["*","0","#","D"]
]

# 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)

#keypad.cleanup()

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

# printKey will be called each time a keypad button is pressed
keypad.registerKeyPressHandler(printKey)

try:
  while(True):
    time.sleep(0.2)
except:
 keypad.cleanup()
2
  • 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

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),
    '0':(1,1,1,1,1,1,0),
    '1':(0,1,1,0,0,0,0),
    '2':(1,1,0,1,1,0,1),
    '3':(1,1,1,1,0,0,1),
    '4':(0,1,1,0,0,1,1),
    '5':(1,0,1,1,0,1,1),
    '6':(1,0,1,1,1,1,1),
    '7':(1,1,1,0,0,0,0),
    '8':(1,1,1,1,1,1,1),
    '9':(1,1,1,1,0,1,1)}

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.