Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

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

View and Edit Project In Cirkit DesignerImage 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

A4988 Stepper Motor Driver Carrier

Limit Switch 3wires

12v Power Supply

HC-SR04 Ultrasonic Sensor

Bipolar Stepper Motor (NEMA 17)

RFID-RC522

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