RFID reader reset pin doesn't work with C ++code

Hi everyone, I was using python. Now I want to use Arduino Code. RFID reader is working with UART. RFID reader has only tx pin for communication. RFID reader has reset pin and I need to use it.
RFID reader has low level reset. I was using it like this;

reset=Pin(13,Pin.OUT)
    time.sleep(1)
    reset.low()
    data=uart.read()
    reset.high()

This code is working. I could read the RFID tag as long as it was within range of the rfid reader. So I want to implement it to Arduino.

int resetrfid = 13;
void setup()
{
  Serial.begin (9600);
  Serial1.begin (9600);
  pinMode(resetrfid, OUTPUT);
}
void loop()
{
delay(1000);
digitalWrite(resetrfid, LOW);
  while (Serial1.available())
  {
    b = Serial1.read();
    Serial.print(b);
  }
 digitalWrite(resetrfid, HIGH);
}

I tried another sleep situations for example like this;

digitalWrite(resetrfid, LOW);
delay(500);

  while (Serial1.available())
  {
    b = Serial1.read();
    Serial.print(b);
  }
 digitalWrite(resetrfid, HIGH);
delay(500);

}

But I couldn't do this. With this code, RFID is only reading one time, that's it. I want to read more if RFID tag is still in RFID reader range. What is the problem ?

Don't really understand, what are you trying to do. Which RFID-Reader do you use? Is there a datasheet or anything else?

Hi, I am using this RFID.

This is arduinoforum I post this question in a topic but as I said I was using micropython so I manage to do my problem. I couldn't do the same with arduino.

You need to send that reader a LOW (I think, otherwise try HIGH) pulse.

Try

digitalWrite(resetrfid, HIGH);    // Set high to start with
delay(200);                       // Keep high for a while
digitalWrite(resetrfid, LOW);     // Set LOW for x ms
delay(50);                        // You might need to vary this time... start with 50ms and increase
digitalWrite(resetrfid, HIGH);    // Set high again

And you only need do this after you find a card... so you can immediately read again.

void loop()
{
  while (Serial1.available())
  {
    b = Serial1.read();
    Serial.print(b);
  }
  digitalWrite(resetrfid, HIGH);    // Set high to start with
  delay(200);                       // Keep high for a while
  digitalWrite(resetrfid, LOW);     // Set LOW for x ms
  delay(50);                        // You might need to vary this time... start with 50ms and
  digitalWrite(resetrfid, HIGH);    // Set high again

}
void loop()
{
  digitalWrite(resetrfid, HIGH);    // Set high to start with
  delay(200);                       // Keep high for a while
  digitalWrite(resetrfid, LOW);     // Set LOW for x ms
  delay(50); 
digitalWrite(resetrfid, HIGH);    // Set high again
  while (Serial1.available())
  {
    b = Serial1.read();
    Serial.print(b);
  }                   
  

}

void setup()
{
  Serial.begin (9600);
  Serial1.begin (9600);
  pinMode(resetrfid, OUTPUT);
  digitalWrite(resetrfid, HIGH);    // Set high to start with


}
void loop()
{
  digitalWrite(resetrfid, LOW);     // Set LOW for x ms
  delay(200);                       // Keep high for a while
  while (Serial1.available())
  {
    b = Serial1.read();
    Serial.print(b);
  }         
  digitalWrite(resetrfid, HIGH);    // Set high again
  delay(50);
}

I am trying like this but it doesn't work. I tried to change HIGH to LOW this didn't work too.

Don't do it every loop... ONLY do it once you have read a card.

Put the reset code in a routine, and only call it when you need to. The end of the while loop does not necessarily mean you have finished reading a card... at 9600 baud the processor is way faster than the data is coming in.

Where's your actual code that reads the cards? ... the above code is really just displaying bytes as they come in.

Here is my full code ( I am using your codes):

#include <Arduino.h>
byte b;
byte buffer[30];
uint8_t idx;
boolean started = false;
byte     XOR;
byte     inverted;
char receivedChar;
uint64_t   value;
unsigned long startTimeGet = 0;
String i, z;
int resetrfid = 13;
char *uint64_to_string(uint64_t input)
{
  static char result[21] = "";
  // Clear result from any leftover digits from previous function call.
  memset(&result[0], 0, sizeof(result));
  // temp is used as a temporary result storage to prevent sprintf bugs.
  char temp[21] = "";
  char c;
  uint8_t base = 10;
  while (input)
  {
    int num = input % base;
    input /= base;
    c = '0' + num;
    sprintf(temp, "%c%s", c, result);
    strcpy(result, temp);
  }
  return result;
}
void print_uint64_t(uint64_t num)
{
  char rev[128];
  char *p = rev + 1;
  while (num > 0)
  {
    *p++ = '0' + (num % 10);
    num /= 10;
  }
  p--;
  /*Print the number which is now in reverse*/
  while (p > rev)
  {
    Serial.print(*p--);
  }
}
void setup()
{
  Serial.begin (9600);
  Serial1.begin (9600);
  pinMode(resetrfid, OUTPUT);
  digitalWrite(resetrfid, HIGH);    // Set high to start with

}
void loop()
{
  if (Serial1.available())
  {


    while (Serial1.available())
    {

      b = Serial1.read();
      if (b == 0x02) // Start byte
      {
        idx = 0;
        started = true;

      }
      if (started) // Ignore anything received until we get a start byte.
      {
        buffer[idx++] = b;
        if (b == 0x03) // End byte
        {
          digitalWrite(resetrfid, HIGH);    // Set high again
          delay(200);                       // Keep high for a while
          digitalWrite(resetrfid, LOW);     // Set LOW for x ms
          delay(50);

          started = false;
          // Extract the card number from bytes 01 (LSB) - 10 (MSB).
          value = 0;
          for (int x = 10; x >= 1; x--)
          {
            if (buffer[x] <= '9')
              value = (value << 4) + buffer[x] - '0';
            else
              value = (value << 4) + buffer[x] - '7';
          }
          Serial.print("Card number: ");
          i = uint64_to_string(value);
          Serial.println(value);
          // Extract the country number from bytes 11 (LSB) - 14 (MSB).
          value = 0;
          for (int x = 14; x >= 11; x--)
          {
            if (buffer[x] <= '9')
              value = (value << 4) + buffer[x] - '0';
            else
              value = (value << 4) + buffer[x] - '7';
          }
          Serial.print("Country number: ");
          z = uint64_to_string(value);
          if (z == NULL)
            z = "0";
          Serial.println(z);
          Serial.println(z + i);
          digitalWrite(resetrfid, HIGH);    // Set high again
        }
      }
    }
  }
}
#include <Arduino.h>
byte b;
byte buffer[30];
uint8_t idx;
boolean started = false;
byte     XOR;
byte     inverted;
char receivedChar;
uint64_t   value;
unsigned long startTimeGet = 0;
String i, z;
int resetrfid = 13;

char *uint64_to_string(uint64_t input)
{
  static char result[21] = "";
  // Clear result from any leftover digits from previous function call.
  memset(&result[0], 0, sizeof(result));
  // temp is used as a temporary result storage to prevent sprintf bugs.
  char temp[21] = "";
  char c;
  uint8_t base = 10;
  while (input)
  {
    int num = input % base;
    input /= base;
    c = '0' + num;
    sprintf(temp, "%c%s", c, result);
    strcpy(result, temp);
  }
  return result;
}
void print_uint64_t(uint64_t num)
{
  char rev[128];
  char *p = rev + 1;
  while (num > 0)
  {
    *p++ = '0' + (num % 10);
    num /= 10;
  }
  p--;
  /*Print the number which is now in reverse*/
  while (p > rev)
  {
    Serial.print(*p--);
  }
}
void setup()
{
  Serial.begin (9600);
  Serial1.begin (9600);
  pinMode(resetrfid, OUTPUT);
  digitalWrite(resetrfid, HIGH);    // Set high to start with

}
void loop()
{
  if (Serial1.available())
  {
    digitalWrite(resetrfid, HIGH);    // Set high again
    delay(200);                       // Keep high for a while
    digitalWrite(resetrfid, LOW);     // Set LOW for x ms
    delay(50);

    while (Serial1.available())
    {

      b = Serial1.read();
      if (b == 0x02) // Start byte
      {
        idx = 0;
        started = true;

      }
      if (started) // Ignore anything received until we get a start byte.
      {
        buffer[idx++] = b;
        if (b == 0x03) // End byte
        {


          started = false;
          // Extract the card number from bytes 01 (LSB) - 10 (MSB).
          value = 0;
          for (int x = 10; x >= 1; x--)
          {
            if (buffer[x] <= '9')
              value = (value << 4) + buffer[x] - '0';
            else
              value = (value << 4) + buffer[x] - '7';
          }
          Serial.print("Card number: ");
          i = uint64_to_string(value);
          Serial.println(value);
          // Extract the country number from bytes 11 (LSB) - 14 (MSB).
          value = 0;
          for (int x = 14; x >= 11; x--)
          {
            if (buffer[x] <= '9')
              value = (value << 4) + buffer[x] - '0';
            else
              value = (value << 4) + buffer[x] - '7';
          }
          Serial.print("Country number: ");
          z = uint64_to_string(value);
          if (z == NULL)
            z = "0";
          Serial.println(z);
          Serial.println(z + i);


        }
      }
    }
    digitalWrite(resetrfid, HIGH);    // Set high again
  }
}

I am trying to use like this but it doesn't work.

Haha... yeah I recognise it.

So inside this if statement... at the end... that's when you want to do the reset of the RFID reader RESET pin.

 if (b == 0x03) // End byte

and get rid of this .. it does nothing.

  if (Serial1.available())
  {

Yes this one is unnecessary . I add it because I try to add resetrfid pulses inside this. I still can't use reset I don't understand why.

Did you do this?

Post your latest code.

I tried these ;

if (b == 0x02) // Start byte
      {
        digitalWrite(resetrfid, HIGH);    // Set high again
        delay(200);
        digitalWrite(resetrfid, LOW);    // Set high again
        delay(50);
        idx = 0;
        started = true;
      }
      if (started) // Ignore anything received until we get a start byte.
      {
        buffer[idx++] = b;
        if (b == 0x03) // End byte
        { 
          digitalWrite(resetrfid, HIGH);    // Set high again

        if (b == 0x03) // End byte
        { 
          digitalWrite(resetrfid, HIGH);    // Set high again
          delay(200);
          digitalWrite(resetrfid, LOW);    // Set high again
          delay(50);
          digitalWrite(resetrfid, HIGH);    // Set high again
          started = false;

The first one is a waste of time... do everything at the END of receiving a message.

The second one might but there are a couple of things you need to confirm.

Is the reader expecting a HIGH or LOW pulse... you should try both.

How long a pulse is it expecting.. again you will need to try.

You had this working previously with python... what were you doing then?

Sorry for late response. This reader is low level rfid reader.

How long a pulse is it expecting.. again you will need to try.
In micropython, I can use it with 0.1 second.

uart1 = UART(0, rx=Pin(1), tx=Pin(0), baudrate=9600)
reset=Pin(13,Pin.OUT)
while True:
    time.sleep(0.1);
    reset.low()
    data=uart1.read()
    if data!=None:
        data = "".join([chr(c) for c in data])
        txt = data[0:11]
        reverse = "".join(reversed(txt)).strip()
        country = data[11:14]
        reverse2 = "".join(reversed(country)).strip()
        i = int(reverse[0:10],16)
        z=  int(reverse2[0:3],16)
        print(z,i,sep="")
    reset.high()

In python, If I use only reset.high(), rfid is not working because reset pin is high (this reset pin low level triggered), but in Arduino Code, if I only write high or low , it doesn't change anything. It reads one time in range. I really don't understand why.

Ok so try changing the duration of the low pulse to 0.1 seconds (100 ms).

I tried with 100,200,500,1000 doesn't change anything. I still don't understand what is the problem. In micropython if I only write reset.high(), rfid don't work. In Arduino, it doesn't change anything, It reads one time in range.

Can you show how you have everything connected. Diagram and photo.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.