[SOLVED] USB Host Shield(Barcode Scanner) + MFRC522 does not work

Hi there(again).

I am doing experiment with a USB barcode scanner connected through USB Host Shield and a MFRC522 RFID reader/writer. I am using Arduino Uno(maybe a counterfeit one).

Both of these devices communicate through SPI. The devices work perfect independently.

When I try to combine both, my USB Barcode scanner is not outputting anything in serial monitor but theres a sound that indicates it successfully decoded the barcode.

I put the MFRC522 in the button routine so that it is only activated when I press the button.

MFRC522 works as expected but not the USB Barcode scanner.

Heres my code

#include <hid.h>                           
#include <hiduniversal.h>                  
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <hidboot.h>

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

#define SS_USB 10
#define SS_RFID 3  
#define RST_PIN 4 
MFRC522 mfrc522(SS_RFID, RST_PIN);
MFRC522::MIFARE_Key key;

#define MAXQRCODE  50

class KbdRptParser : 
public KeyboardReportParser
{
public:
  KbdRptParser();
  void resetString();
  bool newCar;
  bool newString;
  uint8_t currentCursor;
  char QRCodeBuffer[MAXQRCODE + 1];
protected:
  virtual void OnKeyDown (uint8_t mod, uint8_t key);
  virtual void OnKeyPressed(uint8_t key);
};

KbdRptParser::KbdRptParser(): 
KeyboardReportParser()
{
  resetString();
}

void KbdRptParser::resetString()
{
  newCar = false;
  newString = false;
  currentCursor = 0;
  QRCodeBuffer[0] = '\0';
}

void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
{
  uint8_t c = OemToAscii(mod, key);
  if (c) OnKeyPressed(c);
}

/* what to do when symbol arrives */
void KbdRptParser::OnKeyPressed(uint8_t key)
{
  QRCodeBuffer[currentCursor++] = key;
  QRCodeBuffer[currentCursor] = '\0';
  if (currentCursor >= MAXQRCODE) currentCursor = MAXQRCODE - 1; // not enough space, next character will overwrite last one
  newCar = true;
  newString = true;
}


// ====================================================

USB     Usb;
USBHub     Hub(&Usb);     
HIDUniversal      Hid(&Usb);  
HIDBoot<HID_PROTOCOL_KEYBOARD>  Keyboard(&Usb);
KbdRptParser Prs;
const unsigned long timeOutDuration = 200ul;
uint64_t serial = 0;
int block=2;

byte blockcontent[16] = {
  "makecourse_____"};

byte readbackblock[18];//

const int buttonPin = 2;
int buttonState = 0;         
int lastButtonState = 0;


void setup()
{
  pinMode(buttonPin, INPUT);
  pinMode(SS_RFID, OUTPUT);
  digitalWrite(SS_RFID, HIGH);
  pinMode(SS_USB, OUTPUT);
  digitalWrite(SS_USB, LOW);

  SPI.begin();  
  mfrc522.PCD_Init(); 
  Serial.begin(115200);

  Serial.println("Start");
  for (byte i = 0; i < 6; i++) {
    key.keyByte[i] = 0xFF;
  }

  if (Usb.Init() == -1) {
    Serial.println("OSC did not start. Stop here.");
    while (1);
  }

  Hid.SetReportParser(0, (HIDReportParser*)&Prs);
}


void loop()
{
  static uint32_t lastKeyTime = 0;      
  static bool timedOut = false;

  Usb.Task();

  if (Prs.newCar) 
  { 
    Prs.newCar = false; 
    lastKeyTime = millis();
  }

  if (Prs.newString && (millis() - lastKeyTime >= timeOutDuration)) { 

    serial = atol(Prs.QRCodeBuffer);

    if (serial == 882020554)
    {
      Serial.println("ITEM 1 WOMBO");
    }
    else if (serial == 28060132)
    {
      Serial.println("ITEM 2 WOMBO");
    }

    Prs.resetString();

  }


  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {

    if (buttonState == HIGH)
    {    

      digitalWrite(SS_USB, HIGH);
      digitalWrite(SS_RFID, LOW);
      if ( ! mfrc522.PICC_IsNewCardPresent()) {
        return;
      }
      if ( ! mfrc522.PICC_ReadCardSerial()) {
        return;
      }
      Serial.println("card selected");
      writeBlock(block, blockcontent);


      readBlock(block, readbackblock);
      Serial.print("read block: ");
      for (int j=0 ; j<16 ; j++)
      {
        Serial.write (readbackblock[j]);
      }
      Serial.println("");
    }
    delay(50);
  }
  lastButtonState = buttonState;


}



int writeBlock(int blockNumber, byte arrayAddress[]) 
{

  int largestModulo4Number=blockNumber/4*4;
  int trailerBlock=largestModulo4Number+3;
  if (blockNumber > 2 && (blockNumber+1)%4 == 0){
    Serial.print(blockNumber);
    Serial.println(" is a trailer block:");
    return 2;
  } 
  Serial.print(blockNumber);
  Serial.println(" is a data block:");

  /*****************************************authentication of the desired block for access***********************************************************/
  byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));

  if (status != MFRC522::STATUS_OK) {
    Serial.print("PCD_Authenticate() failed: ");
    Serial.println(mfrc522.GetStatusCodeName(status));
    return 3;
  }



  /*****************************************writing the block***********************************************************/

  status = mfrc522.MIFARE_Write(blockNumber, arrayAddress, 16); 
  if (status != MFRC522::STATUS_OK) {
    Serial.print("MIFARE_Write() failed: ");
    Serial.println(mfrc522.GetStatusCodeName(status));
    return 4;
  }
  Serial.println("block was written");
}


int readBlock(int blockNumber, byte arrayAddress[]) 
{
  int largestModulo4Number=blockNumber/4*4;
  int trailerBlock=largestModulo4Number+3;

  /*****************************************authentication of the desired block for access***********************************************************/
  byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid));

  if (status != MFRC522::STATUS_OK) {
    Serial.print("PCD_Authenticate() failed (read): ");
    Serial.println(mfrc522.GetStatusCodeName(status));
    return 3;
  }



  /*****************************************reading a block***********************************************************/

  byte buffersize = 18;
  status = mfrc522.MIFARE_Read(blockNumber, arrayAddress, &buffersize);
  if (status != MFRC522::STATUS_OK) {
    Serial.print("MIFARE_read() failed: ");
    Serial.println(mfrc522.GetStatusCodeName(status));
    return 4;
  }
  Serial.println("block was read");
}

I also included the picture of my setup. If anyone needs a better picture, please tell me so.

Maybe the SS of both devices are conflicting as when I look through MFRC522.cpp, the SS is alternately set to LOW and HIGH and setting it in the main sketch doesn’t seem to have any effect.

I would love to get the help from the community again. Thank you!

Provide full details about your hardware wiriring

Arduino Uno USB Host Shield MFRC522
5V---------------------------------5V------------------------------
3.3V-------------------------------3.3V--------------------- 3.3V
13---------------------------------SCK----------------------SCK
12-------------------------------- MISO---------------------MISO
11---------------------------------MOSI---------------------MOSI
10---------------------------------SS------------------------------
3------------------------------------------------------------SS
4------------------------------------------------------------RST
RESET----------------------------RESET---------------------------
GND------------------------------GND-----------------------GND

There is also a switch that is connected through a 10k resistor on pin 2 of Arduino. The switch works fine.

If theres anything else I miss, please tell me. I will try to add it.

Will there be any solution or will there be not?

Hmm anybody? Did I miss anything in this post or my problem is way too complicated?

Suggestion anyone?

I put the MFRC522 in the button routine so that it is only activated when I press the button.

in the button routine SPI is busy for your USB shield and you do something different there, wondering if there isn't a pb with salve sélect.

But didn’t I pull the slave select pin of USB LOW? Is there any other way around to work with this?

One thing to note here(which I forgot to mention)

The USB Barcode Scanner is not outputting anything in the serial monitor right after I put this code

  writeBlock(block, blockcontent);


      readBlock(block, readbackblock);
      Serial.print("read block: ");
      for (int j=0 ; j<16 ; j++)
      {
        Serial.write (readbackblock[j]);
      }
      Serial.println("");

The weird things is that the barcode scanner indicated a successful reading(theres a blue light and sound on the scanner after scanning an item).

Are their SPI setting not compatible with each other?

What can be done to fix this?

Figured out why and solved.

Thanks.

What was the pb?

Ah sorry. I forgot to update the solution.

Both library handle their SPI without the need of the main sketch to interfere with. Meaning, that you should not control the SS lines manually. Just leave it be and problem solved.

perfect - why make it complicated :slight_smile: