0

Hi I have a wemos d1 r1 and I want to activate a relay with rc522 RFID module. the problem is that when I connect the board to power or when I reset it, or when I'm uploading the code, the led on the relay module flickers, I'm using D9 (GPIO02) for relay, and I'm afraid at some point activates the relay, I tried the D8 (GPIO0) but in above states(power up , reset, uploading code) it activates the relay. is there any way to prevent this ? other pins are used by rc522 module. my code is :


#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN D4
#define RST_PIN D2

char *StrAB[] = {"29 EC C7 C1", "69 5B C9 C2", "22 3B 83 34",  "00 00 00 00", "C6 78 8E 2C","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","00 00 00 00","12 BF BF 34" };  
#define NOTE_G4  392
#define NOTE_C5  523
#define NOTE_G5  784
#define NOTE_C6  1047


int TrueMelody[] = {NOTE_G5, NOTE_C6};
int TrueNoteDurations[] = {12, 8};

int FalseMelody[] = {NOTE_C6, NOTE_G5};
int FalseNoteDurations[] = {12, 8};

#define playTrueMelody() playMelody(TrueMelody, TrueNoteDurations, 2)
#define playFalseMelody() playMelody(FalseMelody, FalseNoteDurations, 2)
 
#define PIEZO D8

#define RELAY D9

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
  digitalWrite(RELAY, HIGH);
  pinMode(RELAY, OUTPUT);
  pinMode(PIEZO, OUTPUT);
  
  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) == StrAB[0] || content.substring(1) == StrAB[1] || content.substring(1) == StrAB[2] || content.substring(1) == StrAB[3]  || content.substring(1) == StrAB[4]  || content.substring(1) == StrAB[5]  || content.substring(1) == StrAB[6]  || content.substring(1) == StrAB[7]  || content.substring(1) == StrAB[8]  || content.substring(1) == StrAB[9]  || content.substring(1) == StrAB[10]  || content.substring(1) == StrAB[11]  || content.substring(1) == StrAB[12]  || content.substring(1) == StrAB[13]  || content.substring(1) == StrAB[14]  || content.substring(1) == StrAB[15]  || content.substring(1) == StrAB[16]  || content.substring(1) == StrAB[17]  || content.substring(1) == StrAB[18]  || content.substring(1) == StrAB[19]  || content.substring(1) == StrAB[20]  || content.substring(1) == StrAB[21]  || content.substring(1) == StrAB[22]  || content.substring(1) == StrAB[23]  || content.substring(1) == StrAB[24]  || content.substring(1) == StrAB[25]  || content.substring(1) == StrAB[26]  || content.substring(1) == StrAB[27]  || content.substring(1) == StrAB[28]  || content.substring(1) == StrAB[29]  || content.substring(1) == StrAB[30]  || content.substring(1) == StrAB[31]  || content.substring(1) == StrAB[32]  || content.substring(1) == StrAB[33]  || content.substring(1) == StrAB[34]  || content.substring(1) == StrAB[35]  || content.substring(1) == StrAB[36]  || content.substring(1) == StrAB[37]  || content.substring(1) == StrAB[38]  || content.substring(1) == StrAB[39]  || content.substring(1) == StrAB[40]  || content.substring(1) == StrAB[41]  || content.substring(1) == StrAB[42]  || content.substring(1) == StrAB[43]  || content.substring(1) == StrAB[44]  || content.substring(1) == StrAB[45]  || content.substring(1) == StrAB[46]  || content.substring(1) == StrAB[47]  || content.substring(1) == StrAB[48]  || content.substring(1) == StrAB[49]  ) //change here the UID of the card/cards that you want to give access
  {
    playTrueMelody();
    digitalWrite(RELAY, LOW);
    delay(700);
    digitalWrite(RELAY, HIGH);
    
  }
 
 else   {

    playFalseMelody();

  }

}


void playMelody(int *melody, int *noteDurations, int notesLength)
{
  pinMode(PIEZO, OUTPUT);

  for (int thisNote = 0; thisNote < notesLength; thisNote++) {
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(PIEZO, melody[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(PIEZO);
  }
}

3
  • 2
    use GPIO 4 or 5; they are about the only pins "silent" during boot. if you're stuck with 0/2, depending on a few things you might be able to clean them up by placing a capacitor from gnd to relay signal in, and feeding the relay signal through a resistor. That creates a low-pass filter which can absorb the brief pulses experienced during boot, albeit at the cost of slightly slower (~150ms) switching time (which often doesn't matter at all).
    – dandavis
    May 23 at 18:36
  • thank you soooooooooooooooo much:)
    – dave
    May 23 at 18:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.