Weird LEDs + Servo problems

Hello everyone,

I'm doing a University project and designing a small parking with a RFID_RC522 for access control and a Micro Servo that controls a barrier (if that's the word you use in English for that). I verified and re-verified my code and connections and can't get the hang of it. The LED together with the buzzer are pinned to a breadboard and since I'm doing testing RED LED, GREEN LED and BUZZER share the same GROUND from POWER "section" of an Arduino UNO.

When the RFID TAG UID is not stored in the system, we have the RED LED lightning and the buzzer making a "reject" sound.

When the RFID TAG UID is in the system, the GREEN LED SHOULD light up, a short buzzer beep and the servo does his stuff.

The thing is, the green LED never turns on - I verified it and it's in working condition (even if I replace the RED LED with GREEN it never lights), some fractions of code:

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
 
#define SS_PIN 10
#define RST_PIN 9
#define LED_G 5 //define green LED pin
#define LED_R 4 //define red LED
#define BUZZER 2 //buzzer pin
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
Servo myServo; //define servo name

void setup() 
{
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  myServo.attach(3); //servo pin
  myServo.write(0); //servo start position
  pinMode(LED_G, OUTPUT);
  pinMode(LED_R, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  noTone(BUZZER);
  Serial.println("Put your card to the reader...");
  Serial.println();

...

if (content.substring(1) == "XX XX XX XX") 
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    digitalWrite(LED_G, HIGH);
    tone(BUZZER, 500);
    delay(300);
    noTone(BUZZER);
    myServo.write(180);
    delay(5000);
    myServo.write(0);
    digitalWrite(LED_G, LOW);
  }
 
 else   {
    Serial.println(" Access denied");
    digitalWrite(LED_R, HIGH);
    tone(BUZZER, 300);
    delay(1000);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER);
  }
}

Else, the system works flawlessly - I'm guessing there is a problem with the delay conflict of the motor and LED or other problem with my code that I'm not aware of. Can someone give me some hints?

Thank you,
InvalidIP

We're not Snippets R Us Please post all the code.

Ayay, captain!

#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
 
#define SS_PIN 10
#define RST_PIN 9
#define LED_G 7 //define green LED pin
#define LED_R 4 //define red LED
#define BUZZER 2 //buzzer pin
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
Servo myServo; //define servo name
 
void setup() 
{
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  myServo.attach(3); //servo pin
  myServo.write(0); //servo start position
  pinMode(LED_G, OUTPUT);
  pinMode(LED_R, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  noTone(BUZZER);
  Serial.println("Put your card to the reader...");
  Serial.println();

}
void loop() 
{
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  //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.println();
  Serial.print("Message : ");
  content.toUpperCase();
  if (content.substring(1) == "A0 B9 8D 7C") 
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    digitalWrite(LED_G, HIGH);
    tone(BUZZER, 500);
    delay(300);
    noTone(BUZZER);
    myServo.write(160);
    delay(5000);
    myServo.write(0);
    digitalWrite(LED_G, LOW);

  }
 
 else   {
    Serial.println(" Access denied");
    digitalWrite(LED_R, HIGH);
    tone(BUZZER, 300);
    delay(1000);
    digitalWrite(LED_R, LOW);
    noTone(BUZZER);
  }
}

Alright, lot of comments but let's start with, what DOES happen when you preset a valid card? You say "no green light" but then I assume no sound and no barrier either? Just a Access denied?

Alright, let's start with the comments :stuck_out_tongue: It would be a heck of a lot faster//easier to just check the uidByte instead of first making it a String...

Well, I'm fine with the string part. So when I have a valid card (stored in the string) I have Authorized access, the buzzer works and the servo works but the green light doesn't want to light up.

At least if "Authorized access" is printed the (terrible) code doesn't seem to be the problem :slight_smile: Then move to the electronics, time to post a schematic :slight_smile:

Sorry for the delay, kind of busy:

The RFID-RC522 usual layout:

MOSI -> pin 11
MISO -> pin 12
SCK -> pin 13
SS (or SDA) -> pin 10
RST -> pin 9
3.3V -> 3.3V
GND -> GND

The rest (servo, buzzer and 2 LEDs)

LED_G -> pin 7
LED_R -> pin 4
Buzzer -> pin 2
//both LED's GREEN and RED + BuZzer are sharing the same GND connection to Arduino UNO Ground on POWER "line"
//both LED's have a 220 Ohm resistor added to PIN 7 and PIN 4

The Servo Orange is connected to Arduino PIN3, POWER to Arduino 5V and GND to Arduino GND on powerstrip.

Thank you for trying to help septillion.
The project this far is a clone of How to Make Arduino Security Access Lock | RFID MFRC522 - YouTube - I want to add the capSense library also.

That's not a schematic :wink: Because the serial print is done the write to high must be done as well. So I'm still convinced the problem is in how it's connected.

And if it's a clone, why not grab that code?

Well, I don't know what you mean with the serial print.

The code is cloned entirely.

//I'm doing my miniature parking right now, I'll redo the whole wiring and I will come up with a proper schematic also.

InvalidIP:
Well, I don't know what you mean with the serial print.

InvalidIP:

    Serial.println("Authorized access");

Well, today I had a quick lunch break and I had some time left. I remade the whole wiring, this time the other LED stopped working. After I got a little mad I did some striping, soldering and threw away the cheap breadbord who seemed to cause me problems and everything is working as it should.

So, kids - what have we learned today? Check, check - get mad - do it the "classic" way - everything good.
@septillion, I will try to improve my string part code, I promise. After everything done I'll post everything on the arduino comunity :slight_smile:

Thank you, good luck!

Hehe, yeah, breadboards are fine for a quick small test but I'm also terrible at using them for bigger projects like Julian Ilett is doing all the time. I always have a bad connection, touching components, errors etc when I do so :stuck_out_tongue: