Need help in coding

I am new to this Arduino platform. I want to make a RFID controlled stepper motor. That is when I scan a recognized tag the servo motor should rotate 90 degree and stop. I am using an Arduino UNO and a 5v stepper motor (28BYJ-48) with the driver and my RFID chip is RC522. I am using this setup to control a chalk dispenser. Help would be very much appreciated. Thanks in advance

Is there a question hidden somewhere?

Is there a question hidden somewhere?

How do I start? Is there any code that I can use?

You start by playing with basic coding examples - such as those under “file/examples” so you get a feel for coding, layout and so on .
Then have a look at controlling a servo , then have a look at rfid tags ( google” Arduino rfid” and have a go at putting some code together .

You need to go up that learning curve !

There is code for the reader, and code for the stepper driver. Look under “examples” in the relevant libraries.

There is code for the reader, and code for the stepper driver. Look under "examples" in the relevant libraries.

Could someone help me in combining them? Like I could post the two different codes and someone could combine and tell me what the codes do.

Combine what?
How would I test it?
I don’t have the hardware.

Start, as adviced in #3 and #4. Get hold of some stepper code and learn how that works. Then Catch an RFid example and learn how that works. Then…. You can start thinking of making one packet of code containing code for both stepper and RFid. That's called system integration and is the way all large Projects are developed.
Trying to put everything in the sack is a recepi fighting lots of problems.

Search the web for arduino merge code or arduino combine code.

The first step would be to solve possible pin conflicts. E.g if you use pin 13 to blink the on-board LED and the rfid reader uses SPI, you have to use an external LED (plus resistor) on another pin.

Next you can start copying and pasting parts of the one code into the other till you have the desired behaviour. Use serial print statements at strategic places to follow the flow of the code.

It will probably be the easiest to start with the reader; once a valid card is detected (use an if statement), move the motor.

Pseudo code for loop()

if card available
  get card data
  if card is valid
    move motor

Are you locked into that type of RFID chip? Using them looks quite complex, compared to say the ones that read over software serial through an RDM6300.

With one of those, the code I posted above would be 95% of what you need…

edit: Just dug out a rdm6300 and a few tags to play. Not quite that simple but still not too bad. I have a system turning the stepper 90 degrees for each presentation of a tag. (But not rc522 style, alas.)

Could send me the code to stop the motor at 90

This code might get you started. As I said I don’t have the rfid gizmo, so in the sketch I’m mimicking reading the tag by typing text in the serial monitor. The typed in “pseudo” tag id is read ala Example3 of Robin2’s serial tutorial and compared to “abc” which is just hard-coded as a character array in the code.

(edit: it’s good idea to get things working in manageable chunks, so you should get the stepper working by itself with code like the below anyway, before worrying about the rfid doobry.)

If the tag id is good, twirl the stepper; if not, don’t.


// 8 mar 2020
// run stepper when rfid tag scanned
// this version has no tag hardware, mimics that with serial input
//    serial input ala Robin2 example3
const byte numChars = 32;
char receivedChars[numChars];

boolean newData = false;
boolean validTag = false;
char masterTag=“abc”;
#define MATCH 0

// BASED ON Example sketch to control a 28BYJ-48 stepper motor with ULN2003 driver board and Arduino UNO.
// More info: */

// Include the Arduino Stepper.h library:
#include <Stepper.h>

// Define number of steps per rotation:
const int stepsPerRevolution = 2048;

// Wiring:
// Pin 8 to IN1 on the ULN2003 driver
// Pin 9 to IN2 on the ULN2003 driver
// Pin 10 to IN3 on the ULN2003 driver
// Pin 11 to IN4 on the ULN2003 driver

// Create stepper object called ‘myStepper’, note the pin order:
Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);

void setup()
  // Set the speed to 5 rpm:

// Begin Serial communication at a baud rate of 9600:
  Serial.println(“stepper with rfid tag”);
  Serial.println(“mimic tag by typing in serial”);
  //Serial.println(“test with 90degree step”); dispenseChalk(stepsPerRevolution/4);

void loop()
  recvWithStartEndMarkers(); //to mimic reading the rfid
  checkNewData();  //to mimic checking the rfid tag
  if (validTag)
    dispenseChalk(stepsPerRevolution / 4);
    validTag = false; //for next time

void recvWithStartEndMarkers()
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = ‘<’;
  char endMarker = ‘>’;
  char rc;

while (Serial.available() > 0 && newData == false) {
    rc =;

if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        if (ndx >= numChars) {
          ndx = numChars - 1;
      else {
        receivedChars[ndx] = ‘\0’; // terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;

else if (rc == startMarker) {
      recvInProgress = true;

void checkNewData()
  if (newData == true) {
    Serial.print(“Tag ID: “);
    if (strcmp(receivedChars, masterTag) == MATCH)
      Serial.println(”, dispensing chalk”);
      validTag = true;
      Serial.println(", invalid, sorry");
    newData = false;

void dispenseChalk(int steps)

The function recvWithStartEndMarkers() could be replaced with something suitable from an appropriate rfid library.

Could you explain on how I should change the stuffs to work with RFID?

I did some combining two codes that I found on the internet. But the problem is that I pressing the manual reset button every time I have to try the recognized tag again. Like for eg. the RFID reads a recognized tag and then makes the motor rotate a required step and then I cant try to get the same motor movement when I keep the same tag again. I have to press the manual reset button to get the tag make the motor move the required step again. Could someone add a reset code in to my code. Thanks.
This is my combined code.

 * All the resources for this project:
 * Modified by Rui Santos
 * Created by FILIPEFLOP
#include <SPI.h>
#include <MFRC522.h>
#include <Stepper.h>
const float stepper_rev = 32;
const float Gear_ratio = 64;
const float step_rev = stepper_rev * Gear_ratio;
int StepsRequired;
int LED = 13;
Stepper myStepper(stepper_rev, 4, 6, 5, 7);

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
void setup() 
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  pinMode(LED, OUTPUT);
  Serial.println("Approximate your card to the reader...");

void loop() 
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  Serial.print("Message : ");
  if (content.substring(1) == "D6 91 8D 23") //change here the UID of the card/cards that you want to give access
     digitalWrite(LED, HIGH);
     Serial.println("Anti-Clockwise Rotation");
     StepsRequired = step_rev / 4;
     digitalWrite(LED, LOW);
    Serial.println(" Access denied");