

Required Parts


Step 1


Step 2


Step 3


Step 4


Step 5


Step 6


Step 7


Step 8


Step 9


Step 10

Calibration is required after the servo is mounted. Connect the servo to io19 on the board and connect the board to computer via USB cable.

Open Servo_Calibration.py in Files and click
.
'''
* Filename : Servo_Calibration
* Thonny : Thonny 4.1.4
* Auther : http//www.keyestudio.com
'''
from machine import Pin, PWM
import time
servo = PWM(Pin(19))
servo.freq(50) #T = 1/f = 20ms
def angle(x):
return int((((x + 45) * 1.8 / 270) + 0.6 )/ 20 *65535)
while True:
servo.duty_u16(angle(0))
After calibration, disconnect the board to the computer and continue the assembly.

Step 11


Step 12
Note that the two gears mesh in order to drive smoothly.


Step 13


Step 14


Completed



Open 4.1Card-scanning access control machine.py in Files and click
.
'''
* Filename : Smart Safe Home: Card-scanning access control machine
* Thonny : Thonny 4.1.4
* Auther : http//www.keyestudio.com
'''
from machine import Pin, PWM
from mfrc522_i2c import mfrc522
import time
servo = PWM(Pin(19))
servo.freq(50) #T = 1/f = 20ms
# IIC configuration
addr = 0x28
scl = 5
sda = 4
rc522 = mfrc522(scl, sda, addr)
rc522.PCD_Init() # Initialization
rc522.ShowReaderDetails() # Display details of the PCD-MFRC522 card reader
UID =[244, 160, 150, 219] # UID code of the IC card, modify it to yours.
def angle(x):
return int((((x + 45) * 1.8 / 270) + 0.6 )/ 20 *65535)
servo.duty_u16(angle(0)) # Initalize servo
while True:
if rc522.PICC_IsNewCardPresent(): # Scan for a new card
if rc522.PICC_ReadCardSerial() == True: # New card is found
if rc522.uid.uidByte[0 : rc522.uid.size] == UID:
servo.duty_u16(angle(120)) # open the door
time.sleep(3)
servo.duty_u16(angle(0)) # close the door
else:
servo.duty_u16(angle(0)) # close the door

Conceive:
First, set the rotation angles for opening and closing the door. Herein, we set angle to 120° to open the door, and 0° to close the door.
Then, when the RFID module receives a correct IC card code, it drives the servo to open the door for 3s. Otherwise, servo stays still.

Code structure:
Initialization.
On the basis of Chapter 3.8 and 3.10, we add an initialization code servo.duty_u16(angle(0)) for servo to ensure the door is closed before running code.
Define UID code. The code of each card is unique, so please replace the UID in code with yours.
Loop.
Determine whether the UID is correct. If yes, servo opens the door. If not, close the door.


After uploading code, scan the correct IC card, and the door will open for 3s and then close.



