1

A LED-strip (WS2812B, 46 pieces, PIN 8) and an Adafruit PN532 RFID shield (IRQ 2) are attached to my Arduino Mega 2560.

The GRB based strip seems to be working fine with FastLED in the beginning, I can confirm this by cycling through all three base colours in setup().

Once I call nfc.readPassiveTargetID(...) and use an NFC tag on it, things start to get weird. Whenever I comment out the line, it works properly again. The colors RGB are randomly swapped (as if I had passed a different color ordering), brightnesses may change randomly as well, and sometimes a few LEDs in the beginning or end of the strip have different brightness/color as the rest.

As I have put the brightness down to 32, I'm sure it's not a power problem.

My assumption is that there is either some memory corruption, or some PIN interference with the two libraries, although I don't know why it would do it. Please find my code below, any help is appreciated.

  #include <FastLED.h>
  #include <Adafruit_PN532.h>
  
  #define LED_COUNT 46
  #define LED_DATA_PIN 8
  CRGB leds[LED_COUNT];
  
  #define PN532_IRQ (2)
  #define PN532_RESET (3) // Not connected
  
  Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
  
  void setLeds(CRGB color) {
    Serial.print("setLeds("); Serial.print(color.r, DEC ); Serial.print(","); Serial.print(color.g, DEC ); Serial.print(","); Serial.print(color.b, DEC ); Serial.println(")");
    for (int i = 0; i < LED_COUNT; i++) {
      leds[i] = color;
    }
    FastLED.show();
  }
  
  void ledStartSequence() {
    setLeds(CRGB::Black);
    for (int crtLed = 0; crtLed < 23; crtLed++) {
      leds[crtLed] = CRGB::Blue;
      leds[crtLed + 23] = CRGB::Blue;
      FastLED.show();
      delay(20);
    }
    Serial.println("Red");
    setLeds(CRGB::Red);
    delay(1000);
    Serial.println("Green");
    setLeds(CRGB::Green);
    delay(1000);
    Serial.println("Blue");
    setLeds(CRGB::Blue);
    delay(1000);
  
    Serial.println("Off");
    setLeds(CRGB::Black);
  }
  
  void initLeds() {
    FastLED.addLeds<WS2812B, LED_DATA_PIN, GRB>(leds, LED_COUNT);
    FastLED.setBrightness(32);
  }
  
  void initSerial() {
    Serial.begin(9600);
    Serial.println("Hello!");
  }
  
  void initNFC() {
    nfc.begin();
    uint32_t versiondata = nfc.getFirmwareVersion();
    Serial.print(versiondata);
    if (! versiondata) {
      Serial.print("Error: Didn't find PN53x board");
      while (1); // halt
    }
    // Got ok data, print it out!
    Serial.print("Found chip PN5"); Serial.println((versiondata >> 24) & 0xFF, HEX);
    Serial.print("Firmware ver. "); Serial.print((versiondata >> 16) & 0xFF, DEC);
    Serial.print('.'); Serial.println((versiondata >> 8) & 0xFF, DEC);
  
    nfc.SAMConfig();
  }
  
  void setup() {
    initSerial();
    initLeds();
    initNFC();
    ledStartSequence();
  }
  
  void loop(void) {
    Serial.println("Waiting for an ISO14443A Card ...");
    setLeds(CRGB::Red); // only displayed correctly in the first iteration
    uint8_t success;
    uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
    uint8_t uidLength;
  
    success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength);
    /*
     *  Here's where things start to go nuts!
    */
    delay(1000);
    setLeds(CRGB::Green); // usually not displayed correctly
    delay(1000);
    setLeds(CRGB::Blue); // usually not displayed correctly
    delay(1000);
  }
2
  • 1
    You have an array uid with 7 bytes. If after nfc.readPassiveTargetID the uidLength value is > 7 you'll have a buffer overflow. Do you know how many bytes the UID of the RFID card containes? Another possibillity is that the PN532 driver uses a timer (e.g. for Hardware I2C) that the LED lib also uses. Then it possibly might help if you reinitialize the LEDs initLeds() after die call to readPassiveTargetID. Both are just guesses. Dec 10, 2020 at 16:41
  • 1
    What happens when after the delay and before setLeds(CRGB::Green); you disable interrupt 2? Or when you don't connect the interrupt line of the RFID shield at all? Just out of curiosity. See arduino.cc/reference/en/language/functions/external-interrupts/…
    – ocrdu
    Dec 10, 2020 at 18:54

1 Answer 1

1

After spending way too much time on the code, I figured that it had to do with something entirely different:

The RFID reader created an RF interference on the data wire towards the LED strip... Shielding it properly did the trick, no matter which pin is being used.

Thanks for the help anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.