webwinkelkeur logo

4.7 avg.

5101+ reviews
webwinkelkeur logoView all

5101+ reviews

5101+ reviews

Order by 16:00 for same day shipping

14 days return

EN

Individual

Business

GPIO Project 11 - Security System with Keypad

Beginner
30 Minuten
114,90

In this project you will learn to build a simple security system using a 4x4 keypad, an RGB LED and the GPIO pins of the Raspberry Pi. This project uses Python and the Thonny IDE, available by default in Raspberry Pi OS.

Connection diagram

  1. Connecting the RGB LED:
    • Place the RGB LED on the breadboard.
    • Connect the RGB LED to GPIO 17 (red), GPIO 27 (green) and GPIO 22 (blue).
    • Connect the common cathode (long pin) to GND (pin 6) of the Raspberry Pi.
  2. Connecting the keypad:
    • Connect the 4 rows of the keypad to GPIO 5, 6, 13 and 19.
    • Connect the 4 columns of the keypad to GPIO 12, 16, 20 and 21.
  3. Connecting the buzzer (optional):
    • Connect the positive pin of the buzzer to GPIO 18.
    • Connect the negative pin to GND.

Pinout Reference

Component

GPIO Pin

Physical Pin

RGB RedGPIO 17Pin 11
RGB GreenGPIO 27Pin 13
RGB BlueGPIO 22Pin 15
Keypad RowsGPIO 5, 6, 13, 19Pin 29, 31, 33, 35
Keypad ColumnsGPIO 12, 16, 20, 21Pin 32, 36, 38, 40
Buzzer (optional)GPIO 18Pin 12

Python-code in Thonny

  1. Start Thonny: Click on the Raspberry Pi menu > Programming > Thonny Python IDE.
  2. Write your code: Create a new file in Thonny and enter the following code:
import RPi.GPIO as GPIO
from time import sleep

# Pin-definities
RED_PIN = 17
GREEN_PIN = 27
BLUE_PIN = 22
BUZZER_PIN = 18

ROWS = [5, 6, 13, 19]  # GPIO-pinnen voor rijen
COLS = [12, 16, 20, 21]  # GPIO-pinnen voor kolommen

# Keypad lay-out
KEYS = [
    ['1', '2', '3', 'A'],
    ['4', '5', '6', 'B'],
    ['7', '8', '9', 'C'],
    ['*', '0', '#', 'D']
]

PINCODE = "1234"  # Stel de gewenste pincode in

# GPIO setup
GPIO.setmode(GPIO.BCM)
GPIO.setup([RED_PIN, GREEN_PIN, BLUE_PIN, BUZZER_PIN], GPIO.OUT)

for row_pin in ROWS:
    GPIO.setup(row_pin, GPIO.OUT)
    GPIO.output(row_pin, GPIO.LOW)

for col_pin in COLS:
    GPIO.setup(col_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def set_color(r, g, b):
    """Stelt de kleur van de RGB LED in."""
    GPIO.output(RED_PIN, r)
    GPIO.output(GREEN_PIN, g)
    GPIO.output(BLUE_PIN, b)

def read_key():
    """Leest een toets van het 4x4 Keypad."""
    for row_num, row_pin in enumerate(ROWS):
        GPIO.output(row_pin, GPIO.HIGH)
        for col_num, col_pin in enumerate(COLS):
            if GPIO.input(col_pin) == GPIO.HIGH:
                GPIO.output(row_pin, GPIO.LOW)
                return KEYS[row_num][col_num]
        GPIO.output(row_pin, GPIO.LOW)
    return None

try:
    while True:
        print("Voer de pincode in:")
        input_code = ""  # Variabele wordt hier correct gedefinieerd als lege string

        # Lees de pincode van het keypad
        while len(input_code) < len(PINCODE):
            key = read_key()
            if key:
                print(f"Toets ingedrukt: {key}")
                if key in ['*', '#', 'A', 'B', 'C', 'D']:
                    print("Speciale toets genegeerd.")
                    continue
                input_code += key  # Voeg de toets toe aan de pincode
                sleep(0.3)  # Voor debounce

        # Controleer de ingevoerde code
        if input_code == PINCODE:
            print("Correcte pincode!")
            set_color(0, 1, 0)  # Groen
        else:
            print("Foute pincode!")
            set_color(1, 0, 0)  # Rood
            GPIO.output(BUZZER_PIN, GPIO.HIGH)
            sleep(0.5)
            GPIO.output(BUZZER_PIN, GPIO.LOW)
        
        sleep(2)
        set_color(0, 0, 0)  # LED uit
except KeyboardInterrupt:
    print("Programma gestopt.")
finally:
    GPIO.cleanup()

Save the file: Click File > Save As and name the file keypad_security.py.

Run the script: Click the green Run button (▶) at the top of the Thonny interface.

 

How does it work?

  • Correct PIN code: The RGB LED lights up green.
  • Incorrect PIN code: The RGB LED lights up red, and the buzzer (if present) beeps briefly.

Input: The system waits for an input via the keypad and compares it with the set PIN code.

Result

If everything is connected correctly and the code is running, the security system will work as follows:

  • If the input is correct, the RGB LED will light up green.
  • If the input is incorrect, the RGB LED will light up red and the buzzer will warn you.

Experimenting

  1. Other colors: Use different combinations of the RGB LED to provide more feedback.
  2. More complex PIN code: Add extra logic to support longer or more complex PIN codes.
  3. Extra alarm: Connect a siren or other actuator for a more comprehensive system.