Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino UNO-Based Smart Stepper Motor System with RFID and Ultrasonic Sensing

Image of Arduino UNO-Based Smart Stepper Motor System with RFID and Ultrasonic Sensing

Circuit Documentation

Summary

This circuit integrates an Arduino UNO microcontroller with various sensors and modules, including an RFID reader, an ultrasonic sensor, a stepper motor driver, a limit switch, and a power supply. The Arduino UNO controls the components and processes data from the sensors to perform specific tasks.

Component List

  1. Arduino UNO

    • Description: A microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. A4988 Stepper Motor Driver Carrier

    • Description: A carrier board for the A4988 microstepping bipolar stepper motor driver.
    • Pins: ENABLE, MS1, MS2, MS3, RESET, SLEEP, STEP, DIR, GND, VCC, 1B, 1A, 2A, 2B, VMOT
  3. Limit Switch 3wires

    • Description: A mechanical switch used to detect the presence or absence of an object.
    • Pins: signal, GND, V in
  4. 12v Power Supply

    • Description: A power supply providing 12V DC.
    • Pins: +, -
  5. HC-SR04 Ultrasonic Sensor

    • Description: An ultrasonic distance sensor.
    • Pins: VCC, TRIG, ECHO, GND
  6. Bipolar Stepper Motor (NEMA 17)

    • Description: A bipolar stepper motor.
    • Pins: A-, A+, B+, B-
  7. RFID-RC522

    • Description: An RFID reader module.
    • Pins: VCC (3.3V), RST, GND, IRQ, MISO, MOSI, SCK, SDA

Wiring Details

Arduino UNO

  • 3.3V: Connected to VCC (3.3V) of RFID-RC522
  • 5V: Connected to VCC of HC-SR04 Ultrasonic Sensor, V in of Limit Switch 3wires, and VCC of A4988 Stepper Motor Driver Carrier
  • GND: Connected to GND of HC-SR04 Ultrasonic Sensor, GND of Limit Switch 3wires, GND of RFID-RC522, GND of A4988 Stepper Motor Driver Carrier, and - of 12v Power Supply
  • Vin: Connected to + of 12v Power Supply
  • D13: Connected to SCK of RFID-RC522
  • D12: Connected to MOSI of RFID-RC522
  • D11: Connected to MISO of RFID-RC522
  • D10: Connected to SDA of RFID-RC522
  • D9: Connected to RST of RFID-RC522
  • D7: Connected to signal of Limit Switch 3wires
  • D6: Connected to TRIG of HC-SR04 Ultrasonic Sensor
  • D5: Connected to ECHO of HC-SR04 Ultrasonic Sensor
  • D4: Connected to DIR of A4988 Stepper Motor Driver Carrier
  • D3: Connected to STEP of A4988 Stepper Motor Driver Carrier

A4988 Stepper Motor Driver Carrier

  • VCC: Connected to 5V of Arduino UNO
  • GND: Connected to GND of Arduino UNO and - of 12v Power Supply
  • VMOT: Connected to + of 12v Power Supply
  • DIR: Connected to D4 of Arduino UNO
  • STEP: Connected to D3 of Arduino UNO
  • RESET: Connected to SLEEP of A4988 Stepper Motor Driver Carrier
  • 1B: Connected to A- of Bipolar Stepper Motor (NEMA 17)
  • 1A: Connected to A+ of Bipolar Stepper Motor (NEMA 17)
  • 2A: Connected to B+ of Bipolar Stepper Motor (NEMA 17)
  • 2B: Connected to B- of Bipolar Stepper Motor (NEMA 17)

Limit Switch 3wires

  • V in: Connected to 5V of Arduino UNO
  • GND: Connected to GND of Arduino UNO
  • signal: Connected to D7 of Arduino UNO

12v Power Supply

  • +: Connected to Vin of Arduino UNO and VMOT of A4988 Stepper Motor Driver Carrier
  • -: Connected to GND of Arduino UNO and GND of A4988 Stepper Motor Driver Carrier

HC-SR04 Ultrasonic Sensor

  • VCC: Connected to 5V of Arduino UNO
  • GND: Connected to GND of Arduino UNO
  • TRIG: Connected to D6 of Arduino UNO
  • ECHO: Connected to D5 of Arduino UNO

Bipolar Stepper Motor (NEMA 17)

  • A-: Connected to 1B of A4988 Stepper Motor Driver Carrier
  • A+: Connected to 1A of A4988 Stepper Motor Driver Carrier
  • B+: Connected to 2A of A4988 Stepper Motor Driver Carrier
  • B-: Connected to 2B of A4988 Stepper Motor Driver Carrier

RFID-RC522

  • VCC (3.3V): Connected to 3.3V of Arduino UNO
  • GND: Connected to GND of Arduino UNO
  • SCK: Connected to D13 of Arduino UNO
  • MOSI: Connected to D12 of Arduino UNO
  • MISO: Connected to D11 of Arduino UNO
  • SDA: Connected to D10 of Arduino UNO
  • RST: Connected to D9 of Arduino UNO

Documented Code

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

const int stepPin = 3; 
const int dirPin = 4; 
const int UltrasonicPin = 6;
const int EndstopPin = 7;
const int Echo_Pin = 5;
const int MaxDistance = 200;
const int RST_PIN = 9;        // Pin 9 para el reset del RC522
const int SS_PIN = 10;        // Pin 10 para el SS (SDA) del RC522
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Crear instancia del MFRC522

byte validKey1[4] = { 0x40, 0x3E, 0x45, 0x39 };  // Clave valida

//Función para comparar dos vectores
bool isEqualArray(byte* arrayA, byte* arrayB, int length)
{
  for (int index = 0; index < length; index++)
  {
    if (arrayA[index] != arrayB[index]) return false;
  }
  return true;
}

NewPing sonar(UltrasonicPin, Echo_Pin, MaxDistance);

void setup() {                                    //Bucle Setup
  Serial.begin(9600);
  SPI.begin();        // Iniciar SPI
  mfrc522.PCD_Init(); // Iniciar MFRC522
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
  pinMode(EndstopPin, INPUT);
}

void loop() {                                       //Bucle Loop
  ultrasonidos();                                  // Codigo de ultrasonidos
  rfid();                                         // Codigo del lector de tarjetas
  //stepper();                                      // Codigo del motor
  int switchState = digitalRead(EndstopPin);
      if(switchState == LOW) {
            // switch is closed
            Serial.println("Switch is Closed");
        } else if(switchState == HIGH) {
            // switch is open
            Serial.println("Switch is Open");
        }
}

void ultrasonidos(){
  delay(100);                                      // esperar 50ms entre pings 
  Serial.print(sonar.ping_cm());                   // obtener el valor en cm (0 = fuera de rango)
  Serial.println("cm");
    if (sonar.ping_cm()>3 && sonar.ping_cm()<15) {
    Serial.println("cocheeeeee");
    }
}

void rfid() {
  // Detectar tarjeta
  if (mfrc522.PICC_IsNewCardPresent())
  {
    Serial.println("Tarjeta detectada");
    //Seleccionamos una tarjeta
    if (mfrc522.PICC_ReadCardSerial())
    {
      // Comparar ID con las claves válidas
      if (isEqualArray(mfrc522.uid.uidByte, validKey1, 4))
        Serial.println("Tarjeta valida");
      else
        Serial.println("Tarjeta invalida");

      // Finalizar lectura actual
      mfrc522.PICC_HaltA