Interfacing RPI Pico with a Relay
A 5v relay module is a type of switch taht can be turned on /off electronically.
It controls a higher-powered device from a low-powered microcontroller without burning it
Internally it is made up of 2 physical and electrically separated circuits:
1.Input circuit -does the switching
2.Control circuit- this is what is getting switched on/off
It works on the principle of electromagnetic attraction where the microcontroller send an electric signal to the input circuit and in turn
it activates the control circuit.
RPI PICO and 5v Relay PROJECT with a {solenoid} &{with an LED and resistor}
from machine import Pin
import time
relay = Pin(16, Pin.OUT) # The pin that is connected to the Input Circuit of the Relay
while True: # Loop forever
relay.value(0) # Turn the relay ON
time.sleep(1)
relay.value(1) # Turn the relay OFF
time.sleep(1)
Access Control Systems
RFID-(Radio Frequency Identification) is a method of wireless communication which uses electromagnetic waves to identify specific targets without
establishing any physical contact between the identification system and the target.
RFID access control system using RPI Pico
from mfrc522 import MFRC522
import utime
from machine import Pin
lock =Pin(16,Pin.OUT)
buzzer = Pin(17, Pin.OUT)
Red =Pin(2,Pin.OUT)
Green =Pin(1,Pin.OUT)
lock.value(0)
buzzer.value(0)
Red.value(0)
Green.value(0)
#Blue.value(0)
def uidToString(uid):
mystring = ""
for i in uid:
mystring = "%02X" % i + mystring
return mystring
c522 = MFRC522(spi_id=0,sck=6,miso=4,mosi=7,cs=5,rst=22)
print("")
print("Place the RFID Card")
print("")
while True:
(stat, tag_type) = rc522.request(rc522.REQALL)
if stat == rc522.OK:
(status, raw_uid) = rc522.SelectTagSN()
if stat == rc522.OK:
rfid_data = "{:02x}{:02x}{:02x}{:02x}".format(raw_uid[0], raw_uid[1], raw_uid[2], raw_uid[3])
print("Card detected! UID: {}".format(rfid_data))
if rfid_data == "9621916220":
lock.value(1)
Green.value(1)
utime.sleep(5)
lock.value(0)
Green.value(0)
elif rfid_data == "60dba214":
lock.value(1)
Green.value(1)
utime.sleep(5)
lock.value(0)
Green.value(0)
else:
buzzer.value(1)
Red.value(1)
utime.sleep(1)
buzzer.value(0)
Red.value(0)
# if Touch_sensor.value()==1:
# print("Touch")
# lock.value(0)
# utime.sleep(5)
# lock.value(1)
RFID access control system using an ESP32
#include
#include
#define RST_PIN 27 // Reset pin for RC522 module
#define SDA_PIN 5 // Slave Select pin for RC522 module
#define LED_GREEN 2
#define LED_RED 4
#define BUZZER 26
#define RELAY_PIN 13
MFRC522 rfid(SDA_PIN, RST_PIN); // Create MFRC522 instance
void setup() {
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_RED, OUTPUT);
pinMode (BUZZER, OUTPUT);
pinMode (RELAY_PIN, OUTPUT);
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
Serial.println("RFID Ready!");
Serial.println("");
}
void loop() {
if (rfid.PICC_IsNewCardPresent() && rfid.PICC_ReadCardSerial()) {
Serial.print("Card UID:");
String uid = "";
for (byte i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i] < 0x10 ? "0" : "");
Serial.print(rfid.uid.uidByte[i], DEC);
uid.concat(String(rfid.uid.uidByte[i] < 0x10 ? "0" : ""));
uid.concat(String(rfid.uid.uidByte[i], DEC));
}
Serial.println("");
if( uid =="9621916220" )
{
Serial.println("Virginia Njeri");
else if(uid=="9621916220" )
{
Serial.println("Virginia Njeri");
digitalWrite(LED_GREEN, HIGH);
digitalWrite (RELAY_PIN, HIGH);
delay(3000);
digitalWrite(LED_GREEN, LOW);
digitalWrite (RELAY_PIN,LOW);
}
else {
Serial.println("Unregistered user");
digitalWrite(LED_RED, HIGH);
digitalWrite(BUZZER, HIGH);
delay(1000);
digitalWrite(LED_RED, LOW);
digitalWrite(BUZZER, LOW);