SPI and RFDI Reader

Status
Not open for further replies.

gary01

Member
Good Day

thank you guys for an awesome board, and lots of support i have been reading.

I have a Teensy 3.6, and wish to hookup the RFID Reader to SPI (0), the default one

I am following the instructions from here, its very basic. http://www.instructables.com/id/Interfacing-RFID-RC522-With-Arduino-MEGA-a-Simple-/

but i just cant seem to make it work, nothing comes up on the Serial interface.

code below, and i am reassigning the SPI.SETSCK pin from 13 to 14.

any idea, i have tired with other libs also no luck:

thank you



Code:
#include <Arduino.h>
#include <SPI.h>
#include <RFID.h>
#define SDA_DIO 9
#define RESET_DIO 8


/* Create an instance of the RFID library */
RFID RC522(SDA_DIO, RESET_DIO);

void setup()
{
SPI.setSCK(14);

  Serial.begin(9600);
  /* Enable the SPI interface */
  SPI.begin();
  /* Initialise the RFID reader */
  RC522.init();
    // put your setup code here, to run once:
}

void loop()

{/* Has a card been detected? */


  if (RC522.isCard())
  {  Serial.println("here");
    /* If so then get its serial number */
    RC522.readCardSerial();
    Serial.println("Card detected:");
    for(int i=0;i<5;i++)
    {
    Serial.print(RC522.serNum[i],DEC);
    //Serial.print(RC522.serNum[i],HEX); //to print card detail in Hexa Decimal format
    }
    Serial.println();
    Serial.println();
  }
  delay(1000);
    // put your main code here, to run repeatedly:
}
 
Last edited by a moderator:
Does the blink example work if you compile and run it ?

To your sketch, in the loop() before the delay(1000); add
Serial.println("hello");
to confirm that sketch is running. It's likely you have mis-wired your SPI device, since you're not seeing anything printed on the serial monitor. Can you attach a photo of your T3.6 and its wiring to the SPI device.
 
This really should work.

My guess is there's a mistake or misunderstanding in connecting the wires. We need to see photos of how you actually connected the wires to be able to help diagnose whatever's wrong.
 
Taking a quick look at their library from the instructables link , I see that the constructor does hardware updates:
Code:
RFID::RFID(int chipSelectPin, int NRSTPD)
{
	_chipSelectPin = chipSelectPin;
	
  pinMode(_chipSelectPin,OUTPUT);			// Set digital as OUTPUT to connect it to the RFID /ENABLE pin 
  digitalWrite(_chipSelectPin, LOW); 
  
  
  pinMode(NRSTPD,OUTPUT);					// Set digital pin, Not Reset and Power-down
  digitalWrite(NRSTPD, HIGH);
  _NRSTPD = NRSTPD;
}


I know that things like that have bit me in the past. That is not knowing for sure if the underlying hardware has been configured properly before the constructor is called.
I would try moving the pinMode/digitalWrites to the start of the init() function and see if that helps.

And if it were me, I would probably sprinkle some print statements at the start to make sure things are working...

Something like:
Code:
void setup()
{
  while (!Serial && (millis() < 4000)) ;  // wait up to 4 seconds for serial monitor to start up.
  SPI.setSCK(14);

  Serial.begin(9600);
  /* Enable the SPI interface */
  SPI.begin();
  Serial.println("After SPI begin");  Serial.flush();  // output string, wait until it outputs
  /* Initialise the RFID reader */
  RC522.init();
  Serial.println("After RC522 init"); Serial.flush();
    // put your setup code here, to run once:
}

You don't necessarily need to Serial.flush() calls. I sometimes use or not use, depending on how it dying or hanging. If it dies as does something like use an invalid memory location, the flush before allows me to see the message...
If it is simple hang, the system will normally output anyway so not needed...
 
Status
Not open for further replies.
Back
Top