Raspberry Pi - Keypad - Relay

Deze handleiding legt uit hoe u een keypad, relay en Raspberry Pi combineert. Wanneer de gebruiker het juiste wachtwoord invoert op het keypad, zal de Raspberry Pi de relay inschakelen.

De tutorial bevat ook de Raspberry Pi-code die een relay gedurende een bepaalde tijd activeert en daarna weer uitschakelt. Bovendien kan de Raspberry Pi-code meerdere wachtwoorden verwerken.

Door een relay aan te sluiten op een Electromagnetic Lock, Solenoid Lock, Linear Actuator, Heating Element, Pump, of Fan... kunnen we deze vervolgens bedienen met een keypad.

Raspberry Pi keypad relay

Hardware vereist

1×Raspberry Pi 5
1×Toetsenbord
1×Relais
1×Jumper wires (verbindingsdraden)
1×(Aanbevolen) Schroefklem Block Shield voor Raspberry Pi
1×(Aanbevolen) Raspberry Pi Prototyping Basisplaat & Breadboard Kit
1×(Aanbevolen) HDMI-Touchscreen-Monitor voor Raspberry Pi

Of u kunt de volgende kits kopen:

1×DIYables Sensorkit (30 sensoren/displays)
1×DIYables Sensorkit (18 sensoren/displays)
Openbaarmaking: Sommige van de links in deze sectie zijn Amazon-affiliate links. We kunnen een commissie ontvangen voor aankopen die via deze links worden gedaan, zonder extra kosten voor u. We waarderen uw steun.

Over Keypad en Relay

Als u niet vertrouwd bent met het keypad en de relay (inclusief pinout, functionaliteit, programmeren, enz.), kunnen de volgende tutorials nuttig zijn:

Bedradingsschema

Raspberry Pi keypad relay bedradingsschema

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Code - Schakel relay aan bij correct wachtwoord

Snelle stappen

  • Zorg dat u Raspbian of een ander met Raspberry Pi compatibel besturingssysteem op uw Pi hebt geïnstalleerd.
  • Zorg ervoor dat uw Raspberry Pi verbonden is met hetzelfde lokale netwerk als uw pc.
  • Zorg dat uw Raspberry Pi internettoegang heeft als u bibliotheken moet installeren.
  • Als dit de eerste keer is dat u de Raspberry Pi gebruikt, bekijk dan hoe u de Raspberry Pi instelt.
  • Verbind uw pc met de Raspberry Pi via SSH met de ingebouwde SSH-client in Linux en macOS of PuTTY in Windows. Zie hoe u uw pc verbindt met de Raspberry Pi via SSH.
  • Zorg dat de RPi.GPIO bibliotheek geïnstalleerd is. Is dit niet het geval, installeer deze dan met het volgende commando:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Maak een Python-scriptbestand met de naam keypad_relay.py en voeg de volgende code toe:
# Deze Raspberry Pi code is ontwikkeld door newbiely.nl # Deze Raspberry Pi code wordt zonder enige beperking aan het publiek beschikbaar gesteld. # Voor volledige instructies en schema's, bezoek: # https://newbiely.nl/tutorials/raspberry-pi/raspberry-pi-keypad-relay import RPi.GPIO as GPIO import time # Define keypad layout KEYPAD = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ['*', 0, '#'] ] # Define GPIO pins for rows, columns, and relay ROW_PINS = [17, 27, 22, 24] COL_PINS = [25, 8, 7] RELAY_PIN = 16 # Adjust this to the actual GPIO pin connected to the relay # Password to unlock PASSWORD = [1, 2, 3, 4] # Initialize GPIO GPIO.setmode(GPIO.BCM) # Set up row pins as inputs with pull-up resistors for row_pin in ROW_PINS: GPIO.setup(row_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set up column pins as outputs for col_pin in COL_PINS: GPIO.setup(col_pin, GPIO.OUT) GPIO.output(col_pin, GPIO.HIGH) # Set up relay pin as an output GPIO.setup(RELAY_PIN, GPIO.OUT) GPIO.output(RELAY_PIN, GPIO.LOW) # Initially, keep the relay off def get_key(): key = None # Scan each column for col_num, col_pin in enumerate(COL_PINS): GPIO.output(col_pin, GPIO.LOW) # Check each row for row_num, row_pin in enumerate(ROW_PINS): if GPIO.input(row_pin) == GPIO.LOW: key = KEYPAD[row_num][col_num] # Wait for key release while GPIO.input(row_pin) == GPIO.LOW: time.sleep(0.05) GPIO.output(col_pin, GPIO.HIGH) return key def check_password(input_password): return input_password == PASSWORD entered_keys = [] try: while True: pressed_key = get_key() if pressed_key is not None: print(f"Pressed: {pressed_key}") if pressed_key == '*': entered_keys = [] # reset the input password elif pressed_key == '#': if check_password(entered_keys): print("Password correct! Access granted.") GPIO.output(RELAY_PIN, GPIO.HIGH) # Activate the relay break else: print("Incorrect password. Try again.") entered_keys = [] # reset the input password else: entered_keys.append(pressed_key) time.sleep(0.1) except KeyboardInterrupt: GPIO.cleanup()
  • Sla het bestand op en voer het Python-script uit met het volgende commando in de terminal:
python3 keypad_relay.py
  • Voer 9765 in gevolgd door de # toets, daarna 1234 gevolgd door de # toets.
  • Controleer de terminal voor het resultaat en de status van de relay.
PuTTY - Raspberry Pi
The incorrect password! try again The correct password! Turning ON relay

Het script draait in een oneindige lus totdat u Ctrl + C indrukt in de terminal.

Uitleg van de code

Geautoriseerde wachtwoorden zijn vooraf gedefinieerd in de Raspberry Pi-code. Een string wordt gebruikt om het door gebruikers ingevoerde wachtwoord op te slaan, de zogenaamde input string. Op het keypad worden twee toetsen (* en #) gebruikt voor speciale functies: wachtwoord wissen en wachtwoord beëindigen. Wanneer een toets op het keypad wordt ingedrukt:

  • Als de ingedrukte toets niet een van de twee speciale toetsen is, wordt deze toegevoegd aan de input string.
  • Als de ingedrukte toets * is, wordt de input string gewist. Dit kan worden gebruikt om het invoeren van het wachtwoord (opnieuw) te starten.
  • Als de ingedrukte toets # is:
    • Verifieert de Raspberry Pi of de input string overeenkomt met één van de vooraf gedefinieerde wachtwoorden, dan wordt de relay ingeschakeld.
    • Ongeacht of het wachtwoord correct is of niet, wordt de input string gewist voor de volgende invoer.

Raspberry Pi Code - Schakel relay aan gedurende een bepaalde tijd bij correct wachtwoord

Als het wachtwoord correct is, wordt de relay gedurende 5 seconden ingeschakeld. Na deze tijdsduur schakelt de relay weer uit.

# Deze Raspberry Pi code is ontwikkeld door newbiely.nl # Deze Raspberry Pi code wordt zonder enige beperking aan het publiek beschikbaar gesteld. # Voor volledige instructies en schema's, bezoek: # https://newbiely.nl/tutorials/raspberry-pi/raspberry-pi-keypad-relay import RPi.GPIO as GPIO import time # Define keypad layout KEYPAD = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ['*', 0, '#'] ] # Define GPIO pins for rows, columns, and relay ROW_PINS = [17, 27, 22, 24] COL_PINS = [25, 8, 7] RELAY_PIN = 16 # Adjust this to the actual GPIO pin connected to the relay # Password to unlock PASSWORD = [1, 2, 3, 4] # Initialize GPIO GPIO.setmode(GPIO.BCM) # Set up row pins as inputs with pull-up resistors for row_pin in ROW_PINS: GPIO.setup(row_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set up column pins as outputs for col_pin in COL_PINS: GPIO.setup(col_pin, GPIO.OUT) GPIO.output(col_pin, GPIO.HIGH) # Set up relay pin as an output GPIO.setup(RELAY_PIN, GPIO.OUT) GPIO.output(RELAY_PIN, GPIO.LOW) # Initially, keep the relay off def get_key(): key = None # Scan each column for col_num, col_pin in enumerate(COL_PINS): GPIO.output(col_pin, GPIO.LOW) # Check each row for row_num, row_pin in enumerate(ROW_PINS): if GPIO.input(row_pin) == GPIO.LOW: key = KEYPAD[row_num][col_num] # Wait for key release while GPIO.input(row_pin) == GPIO.LOW: time.sleep(0.05) GPIO.output(col_pin, GPIO.HIGH) return key def check_password(input_password): return input_password == PASSWORD entered_keys = [] try: while True: pressed_key = get_key() if pressed_key is not None: print(f"Pressed: {pressed_key}") if pressed_key == '*': entered_keys = [] # reset the input password elif pressed_key == '#': if check_password(entered_keys): print("Password correct! Access granted.") GPIO.output(RELAY_PIN, GPIO.HIGH) # Activate the relay time.sleep(5) # Wait for 5 seconds GPIO.output(RELAY_PIN, GPIO.LOW) # Deactivate the relay break else: print("Incorrect password. Try again.") entered_keys = [] # reset the input password else: entered_keys.append(pressed_key) time.sleep(0.1) except KeyboardInterrupt: GPIO.cleanup()

Video Tutorial

We overwegen het maken van videotutorials. Als u videotutorials belangrijk vindt, abonneer u dan op ons YouTube-kanaal om ons te motiveren de video's te maken.

Bekijk onze video-tutorial voor een visuele begeleiding bij dit project! De video biedt extra inzichten en helpt u om stap voor stap de verbindingen te maken en de code te begrijpen.

※ ONZE BERICHTEN

  • U bent welkom om de link naar deze tutorial te delen. Gebruik onze inhoud echter niet op andere websites. We hebben veel moeite en tijd gestoken in het maken van de inhoud, respecteer alstublieft ons werk!