Hey guys, I needed some help with a project (miniature garage door entry and park assist project). I'm a beginner here with stuff related to Arduino's and electronics in general. I was working on a project where I have a servo, led display(16X2), rfid scanner, ultrasonic sensors (2), and a buzzer attached to my arduino uno r3. I'm trying to do 2 things, detect the distance of an object (using one of the ultrasonic sensors) and if less than a particular value then run the servo to go to 180, along with that, detect a particular rfid card and do the same thing with the servo. And after a delay, make the servo go back to 0. At the same time, the other ultrasonic sensor is to detect if the object gets too close and displays text on the LED display (16X2) and get the buzzer to buzz. This was the plan, but if i connect all of this up, i dont think there's any power reaching the servo at all. I directly powered all this up with the arduino connected to my laptop. But nothing was working. Only the Rfid had power because it was directly connected to the 3.3v point on the arduino. I've been completely stopped dead in my tracks here, not able to figure out what to do. Please help
The RFID gets connected to the rest of the ports on it.
Code for it:
#include <Servo.h>
#include <MFRC522.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#define RST_PIN 9
#define SS_PIN 10
#define threshold 50
MFRC522 rfid(SS_PIN, RST_PIN);
String Mastertag = "D3 11 77 A9"; // Enter you tag UID which we get it from first code.
String UIDcard = "";
LiquidCrystal_I2C lcd(0x27, 16, 2);
int servoPosition = 0;
int led = 8;
// Remember buzzer = A3
int switcher = 2;
const int trig1 = 3;
const int echo1 = 4;
const int trig2 = 7;
const int echo2 = 6;
Servo myservo;
int switch_state;
long duration; // Duration for sensor 1
long duration2; // Duration for sensor 2
int distance; // Distance for sensor 1
int distance2; // Distance for sensor 2
bool isObjectDetected; // Flag for object detection
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
lcd.init();
lcd.backlight();
lcd.clear();
myservo.attach(5);
myservo.write(10);
pinMode(switcher, INPUT);
pinMode(trig1, OUTPUT);
pinMode(echo1, INPUT);
pinMode(trig2, OUTPUT);
pinMode(echo2, INPUT);
pinMode(led, OUTPUT);
lcd.clear();
lcd.print(" Access Control ");
lcd.setCursor(0,1);
lcd.print("Scan Your Card:");
}
void loop() {
// Read motion sensor and update servo
digitalWrite(A3,LOW);
myservo.write(10);
switch_state = digitalRead(switcher);
if(switch_state == HIGH)
{
digitalWrite(trig2, LOW);
delayMicroseconds(2);
digitalWrite(trig2, HIGH);
delayMicroseconds(10);
digitalWrite(trig2, LOW);
duration2 = pulseIn(echo2, HIGH);
distance2 = duration2 * 0.034 / 2;
isObjectDetected = distance2 <= threshold;
if (isObjectDetected) {
servoPosition = 360;
digitalWrite(led,HIGH);
Serial.println(distance2);
Serial.println("Object Detected");
} else {
servoPosition = 0;
digitalWrite(led,LOW);
Serial.println(distance2);
Serial.println("Object not Detected");
}
myservo.write(servoPosition);
delay(isObjectDetected ? 1000 : 2000); // Use ternary operator for concise delay
}
else
{
while(getUID())
{
Serial.print("UID: ");
Serial.println(UIDcard);
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Restricted");
lcd.setCursor(0, 1);
if(UIDcard == Mastertag)
{
lcd.print("Access Granted");
myservo.write(100);
delay(50);
for(int i = 0; i < 10;i++){
digitalWrite(led, HIGH);
delay(250);
digitalWrite(led, LOW);
delay(250);
}
}
else
{
digitalWrite(led, LOW);
lcd.print("Access Denied");
myservo.write(10);
}
delay(2000);
lcd.clear();
lcd.print(" Access Control ");
lcd.setCursor(0,1);
lcd.print("Scan Your Card>>");
}
}
// Read ultrasonic sensor and control LED
digitalWrite(trig1, LOW);
delayMicroseconds(2);
digitalWrite(trig1, HIGH);
delayMicroseconds(10);
digitalWrite(trig1, LOW);
duration = pulseIn(echo1, HIGH);
distance = duration * 0.034 / 2;
// LED control based on distance from sensor 1 (optional)
if (distance < 10) {
digitalWrite(A3,HIGH);
Serial.println("Too Close!!!");
}
else
{
digitalWrite(A3,LOW);
Serial.println("Come Close");
}
}
boolean getUID()
{
if (! rfid.PICC_IsNewCardPresent()) {
//Serial.println("card Not found");
return false;
}
if (! rfid.PICC_ReadCardSerial()) {
//Serial.println("Not able to read the card");
return false;
}
UIDcard = "";
for (byte i = 0; i < rfid.uid.size; i++) {
UIDcard.concat(String(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "));
UIDcard.concat(String(rfid.uid.uidByte[i], HEX));
}
UIDcard.toUpperCase();
UIDcard = UIDcard.substring(1);
rfid.PICC_HaltA();
return true;
}
