Introduction: Unveiling the Art of Crafting Common Security Systems---Raspberry Pi Pico

About: Osoyoo brand products are mainly focused on STEM education.User experience and customer feeling are always our first priority. Our goal is to create the best designed product to help students and hobbyist to e…

In the labyrinth of modern life, security systems are our vigilant guardians. They stand silently, shielding us from unseen threats, and preserving our peace of mind. But have you ever wondered how these sentinels are created, what makes them tick, or felt the urge to delve into their inner workings? Today, we embark on an exciting journey, hand in hand, to construct a simple security system. Together, we'll unravel the principles that underpin these technological marvels.

Inquisitive Minds at Play:

Are you one of those inquisitive souls, forever pondering the secrets behind everyday technology? Well, you're not alone! In a world filled with gadgets and gizmos, curiosity is our guiding light. Today, we'll satiate that curiosity by building a security system from scratch.

Our DIY Security System:

This isn't your run-of-the-mill project. It's a hands-on exploration of how security systems operate in our lives. By crafting our security system, you'll gain profound insights into its core principles. Together, we'll dive into the intricate world of sensors, communication protocols, and data processing.

The Spark of Understanding:

Through this endeavor, you'll witness the birth of security right before your eyes. You'll grasp how sensors detect anomalies, how data is transmitted, and how alarms are triggered. As you assemble this system, you'll gain a newfound appreciation for the technology that safeguards our homes and businesses.

The Joy of Learning:

Building your security system isn't just a lesson in technology; it's an experience that fosters a deeper understanding of the world around us. It's an opportunity to quench your thirst for knowledge and satisfy your curiosity about the inner workings of everyday marvels.

Conclusion:

So, let's embark on this journey of discovery, hand in hand, and witness the magic of technology come to life. As we build our security system, we'll unlock the mysteries that keep us safe, and we'll revel in the joy of learning. Curiosity leads to understanding, and together, we'll explore the fascinating world of security systems.

Supplies

In the realm of security systems, RFID readers stand as a popular and integral component. They possess the ability to read RFID card IDs and transmit this data to a computer system. In this instructional guide, we embark on an exploration of the OSOYOO RFID reader, harnessing it to construct a straightforward security verification system. This RFID reader employs a novel communication protocol known as SPI (Serial Peripheral Interface) to relay data to the Raspberry Pi Pico. By the end of this tutorial, you will have gained insights into three distinct communication protocols: SPI, I2C, and Serial (RS232). It's important to note that these communication methods exhibit varying speeds, with SPI being the fastest, followed by I2C, and Serial trailing behind.

More detailed information see :https://osoyoo.com/2021/07/23/raspberry-pi-pico-learning-kit-lesson-7-using-spi-port-to-access-rfid-reader/#google_vignette

Please order Raspberry Pi Pico Learning Kit from:https://osoyoo.store/products/raspberry-pi-pico-python-hardware-programming-learning-kit

Step 1: Prerequisites

Before commencing this project, ensure you have the following components at your disposal:

  1. Raspberry Pi Pico board with a microUSB cable.
  2. A computer equipped to run the Thonny Python IDE.
  3. A breadboard for assembling electronic components.
  4. Two LEDs.
  5. Two 220Ω resistors.
  6. A negative-enabled buzzer.
  7. One OSOYOO RFID reader.
  8. A set of jumper wires.


Step 2: Connection Instructions

To construct the necessary circuit for this project, adhere to the following pin connections:

  • Pico Pins to RFID Pins:
  • GP5 -> SDA
  • GP6 -> SCK
  • GP7 -> MOSI
  • GP4 -> MISO
  • GP18 -> RST
  • GND -> GND
  • 3.3V -> 3.3V
  • Pico Pins to Other Devices:
  • GP14 -> Red LED through 220Ω resistor
  • GP15 -> Green LED through 220Ω resistor
  • GP16 -> Buzzer I/O pin

Please note that this configuration enables the Raspberry Pi Pico to interface effectively with the OSOYOO RFID reader.

Step 3: Code Implementation

We will now proceed to run the Python code on the Raspberry Pi Pico. The code provided below includes comments for clarity:

import time

from machine import I2C, Pin, SPI

from mfrc522 import MFRC522


# Initialize pins and components

buzzer = Pin(16, Pin.OUT)

buzzer.value(1)

true = Pin(15, Pin.OUT)

false = Pin(14, Pin.OUT)

sck = Pin(6, Pin.OUT)

mosi = Pin(7, Pin.OUT)

miso = Pin(4, Pin.OUT)

sda = Pin(5, Pin.OUT)

rst = Pin(18, Pin.OUT)

spi = SPI(0, baudrate=100000, polarity=0, phase=0, sck=sck, mosi=mosi, miso=miso)

card1 = "0xe58a6223" # Change this value to match your testing RFID card 1

card2 = "0xf765bd60" # Change this value to match your testing RFID card 2


while True:

  rdr = MFRC522(spi, sda, rst) # Initialize the RFID reader

  (stat, tag_type) = rdr.request(rdr.REQIDL) # Read card ID

  if stat == rdr.OK:

    (stat, raw_uid) = rdr.anticoll()

    if stat == rdr.OK:

      uid = ("0x%02x%02x%02x%02x" % (raw_uid[0], raw_uid[1], raw_uid[2], raw_uid[3]))

      print(uid)

      if uid == card1:

        print("card 1 detected!")

        buzzer.value(0)

        time.sleep(0.3)

        buzzer.value(1)

        true.value(1)

        time.sleep(1)

        true.value(0)

        time.sleep(1)

      elif uid == card2:

        print("card 2 detected!")

        buzzer.value(0)

        time.sleep(0.3)

        buzzer.value(1)

        time.sleep(0.3)

        buzzer.value(0)

        time.sleep(0.3)

        buzzer.value(1)

        true.value(1)

        time.sleep(1)

        true.value(0)

        time.sleep(1)

      else:

        print("invalid card!")

        buzzer.value(0)

        time.sleep(2)

        buzzer.value(1)

        false.value(1)

        time.sleep(0.1)

        false.value(0)

        time.sleep(0.1)

        false.value(1)

        time.sleep(0.1)

        false.value(0)

        time.sleep(0.1)

        false.value(1)

        time.sleep(0.1)

        false.value(0)

        time.sleep(1)


Step 4: Running the Code

Follow these steps to run the code on your Raspberry Pi Pico:

  1. Connect your Pico board to a USB port on your PC.
  2. Open the Thonny Python IDE and select "MicroPython for Raspberry Pi Pico" as the interpreter.
  3. Choose the appropriate COM port to which your Pico board is connected.
  4. Click "OK" to save the settings.

Download and Load the Code:

  1. Download the code from this link, save it on your local PC, and unzip the file.
  2. You'll find two files: mfrc522.py and pico-lesson7.py. Open the mfrc522.py library file using Thonny.

Save the Library:

  1. Click "File" and then "Save as."
  2. Select "Raspberry Pi Pico" as the destination.
  3. Name the file as "mfrc522.py" and click "OK" to save.

Open and Run the Main Code:

  1. Open the file pico-lesson7.py using Thonny.
  2. Click the "Run" button (represented by a ► icon) to execute the Python code.


Step 5: Working Rusult

Now, you can test the system with different RFID cards. The IDs of the cards will be displayed in the shell window, and the corresponding LEDs and buzzer will indicate the security verification status. Be sure to modify lines 14 and 15 with the IDs of your RFID cards for accurate testing.