control servo motor using rfid arduino

How To Control Servo Motor Using RFID RC522 Arduino?

Posted on

Control Servo Using RFID – Servo is one type of motor that can control the rotation of the angle. The Application of servo motors is diverse, like RC cars, RC aircraft, Arm Robots, industrial pipe valves, key lock systems and others. You can see the basic explanation of servo motors in the article Controlling The Servo Motor With The Keyboard. In this article I will explain to you how to control the movement of a servo motor using RFID. Previously I explained how RFID works. Please look at the TUTORIAL RFID Arduino RC522 article. In addition, I have also made a article to turn on lights using RFID. Please read the article How To Turn On / Off Lamp Using RFID RC522 Arduino.

In general, the system we create is, comparing the ID code of one tag stored in a microcontroller with an ID code detected by RFID Reader. Each RFID has one ID code. When RFID tag or card is put to a RFID reader, the RFID reader will get an ID code and will be compared with the ID code that has been stored in the mokrokontroller. If the ID code is the same, it will drive a servo motor by 90 degrees. If it’s different, nothing will happen. To return the servo rotation to 0 degrees, please put the card and tag it to RFID Reader.

 

Schematic

Please connect your RFID to Arduino as shown below.

Control Servo Using RFID Arduino

 

Scan the ID Code from the Tag

So that we can compare the ID code between the microcontroller and the RFID reader, we must know the ID code of the card or tag that we use. to find out the ID code or tag that we have, upload the following program then open the serial monitor on the Arduino IDE. Put your own tag to RFID reader amd see Serial Monitor. For example, after I inserted my card into an RFID Reader, I got my ID code “5A86340”. This ID code that we will use in the main program.

#include <SPI.h>
#include <MFRC522.h>

#define SS_PIN 10
#define RST_PIN 9

MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup()
{

  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  Serial.println("Put your card or tag at RFID Reader...");
  Serial.println();

}
void loop()
{
  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }
  Serial.print("ID tag :");
  String content = "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    content.concat(String(mfrc522.uid.uidByte[i], HEX));

  }
  Serial.println();
  delay(2000);
}

Main Program

For example, we have got the RFID ID code above with ID code “5A86340”. But in program I convert capital letters to lowercase letters to be “5a86340”.  Please replace the myID code program line according to the ID code you have. Please use the program below.

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>

#define SS_PIN 10
#define RST_PIN 9

MFRC522 mfrc522(SS_PIN, RST_PIN);
Servo myservo;

String myID = "5a86340";
int lamp = 8;
int cond = 0;
int serPin = 3;

void setup()
{

  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  Serial.println("Put your card or tag at RFID Reader...");
  Serial.println();
  pinMode(lamp, OUTPUT);
  myservo.attach(serPin);
  myservo.write(0);

}
void loop()
{
  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }
  Serial.print("ID tag :");
  String content = "";
  byte letter;

  for (byte i = 0; i < mfrc522.uid.size; i++)
  {
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  
  if ((content == myID) && (cond == 0)) {
    myservo.write(90);
    Serial.println("Servo is 90 degre");
    cond = 1;
    content = "";
    delay(2000);
  }
  if ((content == myID) && (cond == 1)) {
    myservo.write(0);
    Serial.println("Servo is 0 degre");
    cond = 0;
    content = "";
    delay(2000);
  }
}

Then upload it to your arduino. See your circuit or serial monitor. May be useful.

Read other articles  How To Program ATTiny13 / ATTiny13A using Arduino

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *