16x RC522 and ATMega1284 crashing

Hello, I am trying to get 16 RC522 modules to work with an ATMega1284. The issue is I can not get the ATMega1284 to stop crashing. I am using an LD33V to power all 16 (Which should be plenty since the LD33V can output 800mA and each RC522 unit takes a maximum of 26mA). As seen in the photo below, each reader successfully initiates, but when the ATMega1284 starts to go read the readers for any available tags, it crashes randomly and it crashes on a different reader every time.

Here is my code, this is where I got to to make it work the best AND read cards before it crashes:

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

// PIN Numbers : RESET + SDAs
//Pins 0-3, 16-17, 24-31
#define RST_PIN         4
#define R1        0
#define R2        1
#define R3        2
#define R4        3
#define R5        13
#define R6        14
#define R7        16
#define R8        17
#define R9        24
#define R10        25
#define R11        26
#define R12        27
#define R13        28
#define R14        29
#define R15        30
#define R16        31


#define NR_OF_READERS    16

byte ssPins[] = {R1, R2, R3, R4, R5, R6, R7, R8, R9, R10, R11, R12, R13, R14, R15, R16};

// Create an MFRC522 instance :
MFRC522 mfrc522[NR_OF_READERS];

void setup() {
  Serial.begin(9600);           // Initialize serial communications with the PC
  SPI.begin();                  // Init SPI bus
  pinMode(RST_PIN, OUTPUT);
  digitalWrite(RST_PIN, LOW);    // mfrc522 readers hard power down.
  for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
    pinMode(ssPins[reader], OUTPUT);
    digitalWrite(ssPins[reader], HIGH);
  }
  for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
    //Serial.println(freeMemory());
    delay(1);
    mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN);
    Serial.print(F("Pin "));
    Serial.print(ssPins[reader]);
    Serial.print(F("-Reader "));
    Serial.print(reader);
    Serial.print(F(": "));
    mfrc522[reader].PCD_DumpVersionToSerial();
  }
  delay(5000);
}


void loop() {
  for (uint8_t readerS = 0; readerS < NR_OF_READERS; readerS++) {
      digitalWrite(ssPins[readerS], HIGH);
    }
  for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
    delay(10);
    digitalWrite(ssPins[reader], LOW);
    Serial.print(F("Reader "));
    Serial.println(reader);
    Serial.println();
    // Looking for new cards
    if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()){
      Serial.println(F("Read!!! "));
      Serial.print(F(": Card UID:"));
      dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
      Serial.println();
    }
    mfrc522[reader].PICC_HaltA();
    mfrc522[reader].PCD_StopCrypto1();
  }
}

void dump_byte_array(byte * buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

For the circuit, it is on a printed circuit board so connections shouldn’t be an issue(Which seems to be proven with the successful initiation of all 16 readers.)

I’ve tried adding more available power from other external sources with no luck, I’ve tested the voltage and signals of the lines all over the board and they all seem fine also. the ATMega1284 is not running out of RAM either as I tested that theory also.

Has anyone ran into a problem like this before or have any suggestions on what I could try? (I have a feeling it is with the reset pin and the SS pins to all of the readers. But I haven’t had any luck with any of my attempts.)

In the second for loop inside the loop () function you are setting each SS low, but you don't set them to high again until the next iteration of loop (). By the end of the second for loop all SS pins will be low.

1 Like

arduarn:
In the second for loop inside the loop () function you are setting each SS low, but you don’t set them to high again until the next iteration of loop (). By the end of the second for loop all SS pins will be low.

I added a digitalWrite line to the end of the first loop to set it back to high. Nothing changed. Here is the loop() function now:

void loop() {
  for (uint8_t readerS = 0; readerS < NR_OF_READERS; readerS++) {
      digitalWrite(ssPins[readerS], HIGH);
    }
  for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
    delay(10);
    digitalWrite(ssPins[reader], LOW);
    Serial.print(F("Reader "));
    Serial.println(reader);
    Serial.println();
    // Looking for new cards
    if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()){
      Serial.println(F("Read!!! "));
      Serial.print(F(": Card UID:"));
      dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
      Serial.println();
    }
    mfrc522[reader].PICC_HaltA();
    mfrc522[reader].PCD_StopCrypto1();
    digitalWrite(ssPins[reader], HIGH);
  }
}

and this is the output I get in the Serial Monitor, same as before:

Pin 0-Reader 0: Firmware Version: 0x92 = v2.0
Pin 1-Reader 1: Firmware Version: 0x92 = v2.0
Pin 2-Reader 2: Firmware Version: 0x92 = v2.0
Pin 3-Reader 3: Firmware Version: 0x92 = v2.0
Pin 13-Reader 4: Firmware Version: 0x92 = v2.0
Pin 14-Reader 5: Firmware Version: 0x92 = v2.0
Pin 16-Reader 6: Firmware Version: 0x92 = v2.0
Pin 17-Reader 7: Firmware Version: 0x92 = v2.0
Pin 24-Reader 8: Firmware Version: 0x92 = v2.0
Pin 25-Reader 9: Firmware Version: 0x92 = v2.0
Pin 26-Reader 10: Firmware Version: 0x92 = v2.0
Pin 27-Reader 11: Firmware Version: 0x92 = v2.0
Pin 28-Reader 12: Firmware Version: 0x92 = v2.0
Pin 29-Reader 13: Firmware Version: 0x92 = v2.0
Pin 30-Reader 14: Firmware Version: 0x92 = v2.0
Pin 31-Reader 15: Firmware Version: 0x92 = v2.0
Reader 0

Reader 1

Reader 2

Perhaps a schematic would help.

OK, after having looked quickly at the library, it seems you shouldn't need to handle the CS pins manually at all after the PCD_Init().

It may help if you post a circuit diagram. /* edit: raschemmel beat me to it */

edit: raschemmel beat me to it

I have that line on speed dial... 8)

16 SPI devices? Just how long are the wires? How far apart are the sensors (they interact and should not be active simultaneously if close to each other)?

The SPI bus is high speed logic designed for local comms with a chip on the same PCB as the microcontroller, or very close by.

Might need a little signal buffering on SCK and the Latch pin.
I've strung 45 TPIC6B595 together and clocked in data at 8 MHz, 20 KHz update rate.
Had 9 on a board, 5 boards, each buffered SCK, MOSI, and SS, passed it on to the next board.
And '1284P was the source.

Yes but 16 NFC antennas are going to be spread out over quite some area I think...

Can't their coax (?) all run back to one central location where the SPI parts (and their power supply connections) are located? My roof antenna is not located anywhere my TV for example.

raschemmel:
Perhaps a schematic would help.

Alright so I tried uploading the schematic earlier and now my account no longer works. I contacted support and waiting to hear back. So here I am to continue under this account. My schematic is attached!

MarkT:
16 SPI devices? Just how long are the wires? How far apart are the sensors (they interact and should not be active simultaneously if close to each other)?

The SPI bus is high speed logic designed for local comms with a chip on the same PCB as the microcontroller, or very close by.

Yes these 16 devices are actually lined up in a 4x4 grid, very close together. The longest line might be 10cm from the ATMega1284. They are meant to be read separately at separate times also. So interference should not be an issue.

I'm afraid to ask , but did you expect the 3.3V regulator to be able to power 16 RF devices that draw
up to (and maybe more) than 100 mA which would total 1.6A ? Am I missing something ?

raschemmel:
I'm afraid to ask , but did you expect the 3.3V regulator to be able to power 16 RF devices that draw
up to (and maybe more) than 100 mA which would total 1.6A ? Am I missing something ?

Okay I could be missing something then. I looked up and the RC522 modules, it says, will only pull a maximum of 26mA. Is this incorrect? Because that would definitely be an issue.

I looked up the wrong datasheet. Post a link for the datasheet.
Even if it is only 26mA, x16 is over 400mA , and then there is this:

SEE bottom of PAGE 397/Active Supply Current vs Xtal Freq
(the part where the active supply current DEAD ENDS at 12MHz !)

TRANSLATION: You are OVERCLOCKING IT BY 33.3 % ! (no wonder the poor thing keeps crashing, your
beating it to death)

You have to change the crystal to 12Mhz, and the regulator will still probably be running warm from
close to half an amp of current.

ATMEGA1284 DATASHEET

raschemmel:
I looked up the wrong datasheet. Post a link for the datasheet.
Even if it is only 26mA, x16 is over 400mA , and then there is this:

SEE bottom of PAGE 397/Active Supply Current vs Xtal Freq
(the part where the active supply current DEAD ENDS at 12MHz !)

TRANSLATION: You are OVERCLOCKING IT BY 33.3 % ! (no wonder the poor thing keeps crashing, your
beating it to death)

You have to change the crystal to 12Mhz, and the regulator will still probably be running warm from
close to half an amp of current.

ATMEGA1284 DATASHEET

Looking at my schematic, you can see I'm running the ATmega1284 with a separate 5v regulator, which is being fed from 12v power supply (12v line is also powering the 3.3v regulator). And if I'm reading this correctly, on page 501, the Active Supply current vs freq graph for the ATmega1284, I shouldn't have an issue with running the ATmega1284 @ 5v @ 16 MHz.

here is the datasheet for the RC522 readers that I am using

What's the voltage on the regulator ?
Is it running hot ?
What's voltage on the 3.3V regulator ?

raschemmel:
What's the voltage on the regulator ?
Is it running hot ?
What's voltage on the 3.3V regulator ?

The 5v Regulator, part 7805CT, is running at about 4.88vdc and giving off very little heat. The 3.3v regulator, part LD33V is running at 3.22vdc and is running quite warm with a heatsink. I actually did try a fan on the heatsink to see if that would fix any problems but nothing changed.

Can you add another 3.3Vregulator in parallel ?
(external) to see if itstops the crashing ?

What does "crashing" mean ?
Does it work properly for 30 to 60 seconds ?

raschemmel:
Can you add another 3.3Vregulator in parallel ?
(external) to see if itstops the crashing ?

What does "crashing" mean ?
Does it work properly for 30 to 60 seconds ?

I have figured out the issue, thank you rashemmel for pointing me in the right direction. I was testing for several hours with 2 LD33V voltage regulators, I was able to get it working for a longer duration but it still crashed. I measured the 3.3v line with my oscilloscope and found the line to be very noisy, with fluctuations up to +/- .5vdc. I changed my regulator to an LM317 and hooked it up to output about 3.3v and hooked it inline with the existing LD33V soldered to the board and that stopped the crashing completely and there is now a smooth 3.3v when I measured it again. I'm now having issues with the readers actually reading but I haven't troubleshooted that yet. I'll post my findings on that when I solve it. Thanks for the help!

Your welcome. In my 40 years in electronics , almost all of the most difficult problems I’ve dealt with were able to be solved with basic electronics 101, the exception being two signal conditioning issues on tesla
motor test fixtures. One required a custom signal conditioning circuit to convert a -24V proximity sensor
output signal to TTL compatible +5V logic signal. That required a rather complex op amp circuit. The other
was another signal on the same test fixture (Tesla Roadster Motor Test Fixture) that required a much more
complex op amp signal conditioning circuit with two Second Order Butterworth Active Filters back to back to
get a 4th Order Butterworth response. That took a little more ‘doing’ but I got it to work but it had like
6 calibration pots. What a nightmare ! The most interesting electronics 101 fix was at a laser company.
The company was being relocated to Redmond , Washington at the corporate headquarters. Everyone had
been laid off except the Documentation guy, the Field Service Manager, the Vice President and his secretary
and me because I was the only one who knew anything about their new ‘magic’ circuit that made the laser
marker work, so I was basically, ‘on call’ , since I had nothing else to do. No engineers, no projects. One
day I’m reading a magazine and talking the janitor about UFOs and he sticks his head in the door and says:
“Hey Robert, you know anything about the Laser Spotwelder in the lab nextdoor ? To which I reply “yeah,
it’s big, has a bunch of humonous coils and caps inside (2 feet high , 18” wide x 10” capacitor and a coil
that was about 1 foot wide and 18" long) and there’s a stack of D size prints an inch thick. Other than that, no. I don’t know anything about it. He says "We got a dozen Japanese business men that’ll be here
in 20 minutes for a demo. Can you operate it ? I turned pale and said “All I can do is try.” to which he said
“That’s all I’m asking”. So go in the lab and there’s this big box like 6 feet high by 4 feet deep by 8 feet
long. The only controls are a key switch and a foot pedal and the only indicators is a high voltage panel
meter. So I take a deep breath and turn the key switch and the needle bangs to the max point so hard
it hit the stop pin and kept on going , bending the needle 45 degrees. I thought “That’s not good.” So
now I have go tell Jerry the spot welder is DOWN HARD and I’m in troubleshooting mode. He says “Can
you fix it ?” I said "Jerry, I’ve never seen it before until today except to walk past it. All I can do is try.
He says “That’s all I’m asking for”. So I go back and start thumbing through the 1 inch thick stack of
D size prints with the clock ticking. I’m looking for that HV panel meter. I finally find it about 7 pages
down from the top and I see it is connected to the contacts on a relay which is driven by a 2N3904
transistor. You can guess what I was thinking “Do you feel LUCKY ?” I pull the drawer out , pop the
card out of the motherboard , ohm out the transistor on diode scale and would you believe it ? SHORTED ! So I turn on the soldering iron and (thank god the Laser tech kept an organized lab because it only
took me 10 seconds to find a 2N3904 transistor. I desolder the bad one and pop in the good one and
run to the stockroom to find a replacement panel meter. I couldn’t find a round one but I found with
the exact same range that was square. I run back and swap the meter. Look at the clock and I’m down
to about 8 or 9 minutes from the original 20 min start time when Jerry asked me to test it. I take a
deep breath and slowing turn the key and the needle slowing started rising indicating the caps were
charging (a spot welder (old school) charges up giant caps and the with the coil discharges all the energy
into the laser in a single pulse which hits the gold plated laser cavity and exits out to the target surface)
When the needle stopped charging I looked around for something to weld and found some paper clips.
I put them on the target and spin the wheels to put the cross hairs on it and press the foot switch.
I could hear a kachunk ! and I looked through the binocular viewer and low and behold, the two paper
clips were welded together ! I’m so excited I think I almost peed myself and ran back to tell Jerry it’s
working as the clock winds down to about 4 or 5 minutes to go. He says “Great ! Can you find something
to weld for the demo ?” I said I’d look around. I run back to the machine and there on the table next
to where I was working was the box of paperclips. So grab one and shape it into a “C” , trim it and
grab another one and shape it into an ‘o’ (Company name was “Compulaser”(formerly Korad Lasers) so
you can see where I’m going with this. The demo went fine when the businessmen arrived an the next
day I went back looking for the spot welded paperclip “Compulaser” demo part and the friggen Marketing
guy had stolen it and had it gold plated to put on his desk ! (“That SOB !”)
What’s the moral of the story ? All I needed to fix a machine I knew nothing about was knowing how to read a schematic, test a transistor and how to solder and that’s it. Everything else was going through the motions.