Raspberry Pi - RFID - Servo Motor

This tutorial instructs you how to use a Raspberry Pi and an RFID NFC RC522 module to control a servo motor. The process works as follows:

This can be used to secure a cabinet, drawer, door, or to open and close a pet feeder...

Hardware Preparation

1×Raspberry Pi 4 Model B
1×RFID/NFC RC522 Kit (reader + tags)
1×RFID Key Fob
1×Servo Motor
1×5V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of RFID/NFC RC522 Module and Servo Motor

If you are unfamiliar with the RFID/NFC RC522 Module and Servo Motor (including pinout, how it works, and how to program), the following tutorials can provide more information:

How It Works

  • The UIDs of certain RFID/NFC tags are programmed into the Raspberry Pi code.
  • When the user taps an RFID/NFC tag onto the RFID/NFC reader, the reader reads the UID from the tag.
  • The Raspberry Pi then retrieves the UID from the reader and compares it to the predefined UIDs.
  • If the UID matches one of the predefined UIDs, the Raspberry Pi will control the servo motor to 90°.
  • If the tag is tapped again, the Raspberry Pi will control the servo motor back to 0°.
  • This process is repeated continuously.

Wiring Diagram

The wiring diagram between Raspberry Pi and RFID RC522 servo motor

This image is created using Fritzing. Click to enlarge image

In the wiring diagram above, a 5V adapter is used to supply power to the Raspberry Pi, the servo motor, and the RC522 module indirectly through the 3.3V pin of the Raspberry Pi.

※ NOTE THAT:

The arrangement of pins may differ depending on the manufacturer. ALWAYS use the labels printed on the module. The image above displays the pinout of modules from DIYables manufacturer.

For the sake of simplicity, the above wiring diagram is used for testing or educational purposes, and for a servo motor with a small torque. We strongly suggest using an external power source for the servo motor in practice. See how to provide the external power for servo motor on the Raspberry Pi - Servo Motor tutorial

Wiring table of RFID/NFC RC522 Module

RFID/NFC RC522 Raspberry Pi
SS → GPIO 8 (SPI0 CS)
SCK → GPIO 11 (SPI0 SCL)
MOSI → GPIO 10 (SPI0 MOSI)
MISO → GPIO 9 (SPI0 MISO)
IRQ not connected
GND → Any GND Pin
RST → Pin 31 (GPIO12)
VCC → Pin 1 or Pin 16 (3.3V)

Wiring table of Servo Motor

Servo Motor Arduino 5V DC Adapter
VCC (red) → positive
GND (brown) → negative
SIG (yellow) → A5

Wiring table of 5V DC Adapter

5V DC Adapter Servo Motor Raspberry Pi
PositiveVCC
Positive -> Vin
NegativeGND
Negative GND

Raspberry Pi Code - Single RFID/NFC Tag

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Enable the SPI interface on Raspberry Pi by following the instruction on Raspberry Pi - how to enable SPI inteface
  • Make sure you have the spidev library installed. If not, install it using the following command:
sudo apt-get install python3-pip python3-dev git sudo pip3 install spidev
  • Make sure you have the mfrc522 library installed. If not, install it using the following command:
sudo pip3 install mfrc522
  • Create a Python script file rfid_servo.py and add the following code:
# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-rfid-servo-motor import RPi.GPIO as GPIO import MFRC522 import time import math # Define GPIO pins RC522_RST_PIN = 12 # GPIO pin connected to RC522's RST pin SERVO_PIN = 16 # GPIO pin connected to servo motor # Set up GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(SERVO_PIN, GPIO.OUT) servo_motor = GPIO.PWM(SERVO_PIN, 50) # 50Hz frequency for the servo motor # Create an instance of the RFID reader reader = MFRC522.MFRC522() # Authorized UID authorized_uid = [0xAA, 0xBB, 0xCC, 0xDD] def is_authorized(uid): return uid == authorized_uid def move_servo(angle): duty_cycle = (angle / 18) + 2 GPIO.output(SERVO_PIN, True) servo_motor.ChangeDutyCycle(duty_cycle) print(f"Rotate Servo Motor to {angle}°") try: servo_motor.start(0) # Start PWM with 0% duty cycle print("Tap RFID/NFC Tag on reader") while True: (status, TagType) = reader.MFRC522_Request(reader.PICC_REQIDL) if status == reader.MI_OK: (status, uid) = reader.MFRC522_Anticoll() if status == reader.MI_OK: if is_authorized(uid): print("Authorized Tag") # Toggle servo angle current_angle = 90 if current_angle == 0 else 0 move_servo(current_angle) time.sleep(2) # Add delay after moving the servo else: print(f"Unauthorized Tag with UID: {' '.join(format(b, '02x') for b in uid)}") except KeyboardInterrupt: servo_motor.stop() GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 rfid_servo.py

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

In order to identify the UID of an RFID/NFC tag, tap an RFID/NFC tag on the RFID-RC522 module. The UID can be seen on the Terminal.

PuTTY - Raspberry Pi
Tap RFID/NFC tag on reader Unauthorized Tag with UID: 3A C9 6A CB

Once you have your UID:

  • Replace line 20 of the code with the UID, for example byte authorizedUID[4] = {0x3A, 0xC9, 0x6A, 0xCB};
  • Run the Python script again
python3 rfid_servo.py
  • Place an RFID/NFC tag on the RFID-RC522 module
  • The servo motor will rotate to 90°
  • Check out the output on the Terminal
PuTTY - Raspberry Pi
Tap RFID/NFC tag on reader Authorized Tag Rotate Servo Motor to 90°
  • Tap the same RFID/NFC tag that was used on the RFID-RC522 module once more.
  • Check out the servo motor rotate to 0°.
  • Check out the output on the Terminal
PuTTY - Raspberry Pi
Tap RFID/NFC tag on reader Authorized Tag Rotate Servo Motor to 90° Authorized Tag Rotate Servo Motor to 0°
  • Tap an RFID or NFC tag on the RFID-RC522 module.
  • Check out the output on the Terminal
PuTTY - Raspberry Pi
Tap RFID/NFC tag on reader Authorized Tag Rotate Servo Motor to 90° Authorized Tag Rotate Servo Motor to 0° Unauthorized Tag with UID: BD 1E 1D 00

Raspberry Pi Code - Multiple RFID/NFC Tags

We can enable multiple RFID/NFC tags to control a servo motor. As an example, the code below uses three RFID tags.

# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-rfid-servo-motor import RPi.GPIO as GPIO import MFRC522 import time import math # Define GPIO pins RC522_RST_PIN = 12 # GPIO pin connected to RC522's RST pin SERVO_PIN = 16 # GPIO pin connected to servo motor # Set up GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(SERVO_PIN, GPIO.OUT) servo_motor = GPIO.PWM(SERVO_PIN, 50) # 50Hz frequency for the servo motor # Create an instance of the RFID reader reader = MFRC522.MFRC522() # List of authorized UIDs authorized_uids = [ [0xAA, 0xBB, 0xCC, 0xDD], [0x11, 0x22, 0x33, 0x44], [0xFF, 0xFF, 0xFF, 0xFF] ] def is_authorized(uid): for auth_uid in authorized_uids: if uid == auth_uid: return True return False def move_servo(angle): duty_cycle = (angle / 18) + 2 GPIO.output(SERVO_PIN, True) servo_motor.ChangeDutyCycle(duty_cycle) print(f"Rotate Servo Motor to {angle}°") try: servo_motor.start(0) # Start PWM with 0% duty cycle current_angle = 0 # Initial angle print("Tap RFID/NFC Tag on reader") while True: (status, TagType) = reader.MFRC522_Request(reader.PICC_REQIDL) if status == reader.MI_OK: (status, uid) = reader.MFRC522_Anticoll() if status == reader.MI_OK: if is_authorized(uid): print("Authorized Tag") # Toggle servo angle current_angle = 90 if current_angle == 0 else 0 move_servo(current_angle) else: print(f"Unauthorized Tag with UID: {' '.join(format(b, '02x') for b in uid)}") except KeyboardInterrupt: servo_motor.stop() GPIO.cleanup()

Repeat the same steps as before, and then tap each tag in succession on the RFID-RC522 module. The output on the Terminal should appear as follows:

PuTTY - Raspberry Pi
Tap RFID/NFC tag on reader Authorized Tag Rotate Servo Motor to 90° Authorized Tag Rotate Servo Motor to 0°

You can expand the code above to include four, or more RFID tags.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!