Raspberry PI Project: Keypad

Keypad Project with Raspberry PI

About the Project:



Here is one of my favorites. This post is a step by step of how to set up a keypad while using Python on the Raspberry PI to perform mathematical computations. We will be using Python programming because it is compatible with the Raspberry PI. This project was inspired by my first project, which accomplishes the same thing but with C++ programming and Arduino. Make sure to go check that one out too! It will give you different view on the same project. I decided that I wanted to do this project on the Raspberry PI as well because I wanted to practice Python programming. I hope this is helpful!

**Make sure to check out the previous two posts to set up your Raspberry if it is not already set up**

Additional Material Needed:

(Apart from the material needed to connect the Raspberry PI to the monitor: refer to last post):
  • Wires
  • Keypad
Make sure that the pins are only on one side of the wire! (aka: male-female jumper cables)
 

**It is important to understand how the keypad works so here is just a brief overview:
The eight connection coming off of the keypad are connected to the rows and columns of the keypad. The first four(on the left) are associated with the rows and the last four are associated with the columns. To determine what key was pressed, the rows and columns are evaluated separately to pin point the exact location which was pressed and what the key was.     

















Connections:

With the connections, the pins that you use to connect the Raspberry PI do not matter. The pins will have to be changed in the code, and I will show you exactly where to change them. Here is a picture of where I connected my wires, and these pins match the ones in my code. 


**the first picture shows the front view, and the second shows the back

GPIOs:

GPIO stands for General Purpose Inputs/Outputs, and they are present on the Raspberry PI. We can see these pins at the top of the PI, and they are very important for our project. It is important to recognize the GPIOs when coding, and the official Raspberry PI website does a good job of this. I used this chart to figure out what GPIO my pins were in. 


Code:

I am using the Ninja IDE editor that comes with the Raspbian operating system to run my code. I created my own code after using the basics of a already written code on the web, and this basic code only allowed me to read the numbers entered in a keypad. One thing I found while writing this code was that it involved a lot of complex concept, and it was really difficult to write without using an already written set up. Let me know if you have anyway to make it better! The code I created is pasted below with comments explaining it:

import RPi.GPIO as GPIO
import time

#variables for rows and thier corresponding GPIOs(if you used different pins change it here)
L1 = 5
L2 = 6
L3 = 13
L4 = 19
#variables for the columns and thier corresponding GPIOs
C1 = 12
C2 = 16
C3 = 20
C4 = 21

#setup
keypadPressed = -1
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(L1, GPIO.OUT)
GPIO.setup(L2, GPIO.OUT)
GPIO.setup(L3, GPIO.OUT)
GPIO.setup(L4, GPIO.OUT)
GPIO.setup(C1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

#initializing variables and text needed for the claculator section
num1=0
num2=0
num=0
operand=""
input = ""
operDict = { "A":"+", "B":"-", "C":"*", "D":"/"}
print("Please select a Number, and operand and then another number.\nTreat Alphabets as below")
print("A = + \nB = - \nC = * \nD = /\n")

#more setup
def keypadCallback(channel):
    global keypadPressed
    if keypadPressed == -1:
        keypadPressed = channel
GPIO.add_event_detect(C1, GPIO.RISING, callback=keypadCallback)
GPIO.add_event_detect(C2, GPIO.RISING, callback=keypadCallback)
GPIO.add_event_detect(C3, GPIO.RISING, callback=keypadCallback)
GPIO.add_event_detect(C4, GPIO.RISING, callback=keypadCallback)
def setAllLines(state):
    GPIO.output(L1, state)
    GPIO.output(L2, state)
    GPIO.output(L3, state)
    GPIO.output(L4, state)

#Take the input and compare it to the input to the characters, and then set the input vharacter as a number.
def readLine(line, characters):
    global input
    global num
    global operand
    GPIO.output(line, GPIO.HIGH)
    if(GPIO.input(C1) == 1):
        input = input + characters[0] 
        num = characters[0]
 
    if(GPIO.input(C2) == 1):
        input = input + characters[1]
        num = characters[1]
    if(GPIO.input(C3) == 1):
        input = input + characters[2]
        num = characters[2]
    if(GPIO.input(C4) == 1):
        input = input + characters[3]
        operand=str(characters[3])
    GPIO.output(line, GPIO.LOW)

try:
    while True:
        if keypadPressed != -1:
            setAllLines(GPIO.HIGH)
            if GPIO.input(keypadPressed) == 0:
                keypadPressed = -1
                #If the first num is not defined, set the first number entered as num1, and if the num1 is something other than 0, set the second number enetered as num2 
                if num1==0:
                    num1 = num
                    print("First num is:" + str(num1))
                else :
                    num2=num
                    print("Second num is:" + str(num2))
                #Take the key and perform the intended operation
                if str(operand)!="" :
                    print("Operand selected is:" + operDict[str(operand)])
                    if operand == "A" :
                        print("Value of {0} + {1} is {2}".format(num1, num2, int(num1)+int(num2)))
                    if operand == "B" :
                        print("Value of {0} - {1} is {2}".format(num1, num2, int(num1)-int(num2)))
                    if operand == "C" :
                        print("Value of {0} * {1} is {2}".format(num1, num2, int(num1)*int(num2)))
                    if operand == "D" :
                        print("Value of {0} / {1} is {2}".format(num1, num2, int(num1)/int(num2)))
                    #reset after the numbers are computed.
                    num=0
                    num1=0
                    num2=0
                    operand=""
            else:
                time.sleep(0.1)
        else:
            readLine(L1, ["1","2","3","A"])
            readLine(L2, ["4","5","6","B"])
            readLine(L3, ["7","8","9","C"])
            readLine(L4, ["*","0","#","D"])
            time.sleep(0.1)
except KeyboardInterrupt:
    print("\nApplication stopped!")

Here is the link to the website where I got the setup code:

Let me know if you have any suggestions on how to make the code better. It will surely help!







Comments

Popular posts from this blog

Arduino Project: Keypad