ESP8266 - RFID - Servo Motor
This tutorial instructs you how to use an ESP8266 and RFID NFC RC522 module to control a servo motor. The process works as follows:
- When an authorized tag is tapped, ESP8266 will rotate the servo motor to 90°
- If the same tag is tapped again, the servo motor will be rotated back to 0°
- This cycle will repeat indefinitely
This can be used to secure a cabinet, drawer, door, or control the pet feeder's opening and closing...
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of RFID/NFC RC522 Module and Servo Motor
If you are unfamiliar with the RFID/NFC RC522 Module and Servo Motor (including pinout, functioning, and programming), check out the following tutorials:
- ESP8266 - RFID/NFC RC522 tutorial
- ESP8266 - Servo Motor tutorial
How It Works
- Some UIDs of RFID/NFC tags are already programmed into the ESP8266 code.
- When a user taps an RFID/NFC tag onto the RFID/NFC reader, the reader reads the UID from the tag.
- The ESP8266 then receives the UID from the reader.
- It compares the read UID with the predefined UIDs that were set in the code.
- If the UID matches one of the predefined UIDs, the ESP8266 will control the servo motor to 90°.
- When the tag is tapped again, the ESP8266 will control the servo motor back to 0°.
- This process will repeat indefinitely.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
For the sake of simplicity, the above wiring diagram is utilized for testing or learning purposes, and for a small-torque servo motor. In actuality, we strongly suggest using an external power supply for the servo motor. The wiring diagram below illustrates how to connect the servo motor to an external power source.
This image is created using Fritzing. Click to enlarge image
See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.
※ 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 producer.
Wiring table of RFID/NFC RC522 Module
RFID/NFC RC522 | ESP8266 |
---|---|
SS | → GPIO 5 (SPI CS/SS) |
SCK | → GPIO 8 (SPI CLK) |
MOSI | → GPIO 7 (SPI MOSI) |
MISO | → GPIO 6 (SPI MISO) |
IRQ(not connected) | |
GND | → GND |
RST | → TO_BE_UPDATED |
VCC | → 3.3V |
ESP8266 Code - Single RFID/NFC Tag
Detailed Instructions
To get started with ESP8266 on Arduino IDE, follow these steps:
- Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
- Wire the components as shown in the diagram.
- Connect the ESP8266 board to your computer using a USB cable.
- Open Arduino IDE on your computer.
- Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
- Click to the Libraries icon on the left bar of the Arduino IDE.
- Search for “MFRC522” and locate the library by GithubCommunity.
- Press the Install button to install the MFRC522 library.
In order to discover the UID of an RFID/NFC tag, the first step is to upload code to ESP8266 using the Arduino IDE and then click the Upload button. After that, open the Serial Monitor and tap the tag onto the RFID-RC522 module. The UID will be displayed on the Serial Monitor.
Once you have your UID:
- Replace the UID in line 20 of the code with your own. For example, change byte authorizedUID[4] = {0xFF, 0xFF, 0xFF, 0xFF}; to byte authorizedUID[4] = {0x3A, 0xC9, 0x6A, 0xCB};
- Upload the code to your ESP8266 board
- Tap an RFID/NFC tag on the RFID-RC522 module
- The servo motor should rotate to 90°
- Check the output on the Serial Monitor
- Tap the RFID/NFC tag on the RFID-RC522 module once more.
- The servo motor will rotate to 0°, and you can observe this on the Serial Monitor.
- Tap an RFID/NFC tag to the RFID-RC522 module.
- Check the Serial Monitor for the output.