Adding display output to RFID HEX code print

There is a thread here by @vishnuanand "Convert MFRC522 UID to string, byte array to string" amd since its closed I can no long comment. I am trying to add a display.print output to show the HEX UID on my ssd1306. I have been going at it for a couple hours and figured I would now seek some assistance.

I know its probably very simple but I am just missing it.

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN 9
#define SS_PIN 10

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
  
void setup() {
   Serial.begin(9600);
   SPI.begin();      // Initiate  SPI bus
   mfrc522.PCD_Init();   // Initiate MFRC522
   Serial.println("Approximate your card to the reader...");
}


void loop() {
   if ( ! mfrc522.PICC_IsNewCardPresent()) {
      return;
   }

   // Select one of the cards
   if ( ! mfrc522.PICC_ReadCardSerial()) {
      return;
   }

   char str[32] = "";
   array_to_string(mfrc522.uid.uidByte, 4, str); //Insert (byte array, length, char array for output)
   Serial.println(str); //Print the output uid string
   mfrc522.PICC_HaltA();
}

void array_to_string(byte array[], unsigned int len, char buffer[])
{
   for (unsigned int i = 0; i < len; i++)
   {
      byte nib1 = (array[i] >> 4) & 0x0F;
      byte nib2 = (array[i] >> 0) & 0x0F;
      buffer[i*2+0] = nib1  < 0xA ? '0' + nib1  : 'A' + nib1  - 0xA;
      buffer[i*2+1] = nib2  < 0xA ? '0' + nib2  : 'A' + nib2  - 0xA;
   }
   buffer[len*2] = '\0';
}

If you only want to print, you don't have to convert to a character array (assuming the display library that you use derives print functionality from the print class (which it more than likely does).

You can give below a try.

for (unsigned int i = 0; i < 4; i++)
{
  if(mfrc522.uid.uidByte[i] < 0x10)
  {
    display.print("0");
    // for debugging
    Serial.print("0");
  }
  
  display.print(mfrc522.uid.uidByte[i], HEX);  // thanks @johnwasser
  // for debugging
  Serial.print(mfrc522.uid.uidByte[i], HEX);  // thanks @johnwasser
}

Use it instead of

   array_to_string(mfrc522.uid.uidByte, 4, str); //Insert (byte array, length, char array for output)
   Serial.println(str); //Print the output uid string

Not compiled, not tested.

// edit:
added the HEX to print in hexadecimal.

Don't forget the ", HEX"! :slight_smile:

  display.print(mfrc522.uid.uidByte[i], HEX);
  // for debugging
  Serial.print(mfrc522.uid.uidByte[i], HEX);

Thanks. Though you did vorrect it, I will update my post.

Thank you both.

I have it "working" I can see the code on my lcd. I ditched the oled for the lcd and it works better but now I need to get the display looking nice.

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