RFID and Servo

So, I’m a beginner and right now I’m trying to make a servo motor sweep using the RFID, its like the locking of doors etc. but somehow it doesn’t work.

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#define RST_PIN         9          // Configurable, see typical pin layout above
#define SS_PIN          10         // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN);  // Create MFRC522 instance
Servo servomotor;


void setup() {
  servomotor.attach(7);

  Serial.begin(9600);		// Initialize serial communications with the PC

  while (!Serial);		// Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)

  SPI.begin();			// Init SPI bus

  mfrc522.PCD_Init();		// Init MFRC522

  mfrc522.PCD_DumpVersionToSerial();	// Show details of PCD - MFRC522 Card Reader details

  Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));

}

void loop() {

  servomotor.write( 0 );

  // Look for new cards

  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }

  // Select one of the cards

  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  if (mfrc522.uid.uidByte[0] == 0xD2 &&  mfrc522.uid.uidByte[1] == 0xD9 && mfrc522.uid.uidByte[2] == 0x79 && mfrc522.uid.uidByte[3] == 0x0E) //dumpinfo chane this part
  {
    Serial.println("Authorized Access");
    servomotor.write( 90 );
    delay(1000);
    servomotor.write( 0 );
  }

  else 
  {
    Serial.println("Denies Access");
    servomotor.write( 0 );
  }
  
}

Thanks in advance

What parts work? Are you able to detect a new card? Are you able to read the card? Are you getting the expected data from the card? You can get those answers by adding more Serial.println() to your sketch.

yeah, it detects the card, it will print access denied if the keychain is used, and will authorize the access if the card is used. but I wanted the servo motor to sweep once the the RFID detects the card. The servo is not moving at all but the detection of card is working fine. :confused:

OK, so the part that isn’t working is:

    servomotor.write( 90 );
    delay(1000);
    servomotor.write( 0 );

The code is simple enough so there is likely something wrong with your servo, wiring, or power. Servos typically need a separate power supply because they can draw more current than an Arduino can supply (causing a reset).

To test your servo, just run a simple sketch:

#include <Servo.h>
Servo servomotor;

void setup() {
  servomotor.attach(7);
}

void loop() {
  servomotor.write( 90 );
  delay(1000);
  servomotor.write( 0 );
   delay(1000);  
}
  Serial.println("Authorized Access");
   servomotor.write( 90 );
   delay(1000);
   servomotor.write( 0 );

So you are saying that it gets to this bit of code and prints "Authorized Access" but the servo doesn't move to 90 for a second then back to 0? Note that if it's a slow servo or heavily loaded it may not be able to get all the way to 90 in only a second but it should at least start moving.

If it doesn't move at all then the servo is not connected/powered correctly or it is bad. Please post a circuit diagram showing connections and how everything is powered.

Steve

It worked (the servo) when, I directly placed the power to the Arduino Uno, but somehow when I put it in the breadboard, it doesn't work? Any advices on what might be the problem?

boolgalIDE: It worked (the servo) when, I directly placed the power to the Arduino Uno, but somehow when I put it in the breadboard, it doesn't work? Any advices on what might be the problem?

Breadboards are notorious for poor connections. Also, some large breadboards have the power rails split in the middle. Use a multimeter to check the power getting to the servo.