0

I can use the MFRC522 using the following code:

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

MFRC522 mfrc522(10, 9);

void setup() {
    SPI.begin();
    mfrc522.PCD_Init(); 
}

void loop() {
    if (  mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial() ) {
         // Do stuff
    }
}

And it works great. I can also use the dot-matrix (8x8) using the following code:

#include "LedControl.h"

LedControl lc = LedControl(12,11,8,1);

void setup() {
    lc.shutdown(0,false);
    lc.setIntensity(0,3);
    lc.clearDisplay(0);

    lc.setLed(0,2,5,true);
    lc.setLed(0,5,5,true);

    lc.setLed(0,2,2,true);
    lc.setLed(0,3,1,true);
    lc.setLed(0,4,1,true);
    lc.setLed(0,5,2,true);
}

void loop() {
}

And it works just fine as well. However, when I try to use both of them using the following code:

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

LedControl lc = LedControl(12,11,8,1);
MFRC522 mfrc522(10, 9);

void setup() {
    SPI.begin();
    mfrc522.PCD_Init();

    lc.shutdown(0,false);
    lc.setIntensity(0,3);
    lc.clearDisplay(0);

    lc.setLed(0,2,5,true);
    lc.setLed(0,5,5,true);

    lc.setLed(0,2,2,true);
    lc.setLed(0,3,1,true);
    lc.setLed(0,4,1,true);
    lc.setLed(0,5,2,true); 
}

void loop() {
    if (  mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial() ) {
         // Do stuff
    }
}

In that case only one of them works (the MFRC522). I know that since they are connected in SPI mode they need to have different SS pins, so I used pin 10 for MFRC522 and pin 8 for dot-matrix. So, what's wrong? Why dot-matrix doesn't work at the same code with MFRC522??

0

The LedControl doesn't use SPI library but it's using shiftOut internally. So you can't use them both at the same pins as the SPI interface takes over them.

Basically SPI.begin() takes over the control on its pins and that means when you use digitalWrite or pinMode on those pins, it'll be ignored (at least on some of them).

If you add SPI.begin() into the second example (LedControl one) it'll stop working. However you can change pins for LedControl to other (free) pins and it should be working again.

And also there are mixed signals between both devices:

HW SPI wiring:
13  CLK    output
12  MISO   input
11  MOSI   output
9   SS     output

and LedControl wiring is:
12  MOSI   output (not really)
11  CLK    output

Something from the datasheet about alternate pin functions:

  • SCK/PCINT5 – Port B, Bit 5
    SCK: Master Clock output, Slave Clock input pin for SPI channel. When the SPI is enabled as a Slave, this pin is configured as an input regardless of the setting of DDB5. When the SPI is enabled as a Master, the data direction of this pin is controlled by DDB5. When the pin is forced by the SPI to be an input, the pull-up can still be controlled by the PORTB5 bit.
    PCINT5: Pin Change Interrupt source 5. The PB5 pin can serve as an external interrupt source.
  • MISO/PCINT4 – Port B, Bit 4
    MISO: Master Data input, Slave Data output pin for SPI channel. When the SPI is enabled as a Master, this pin is configured as an input regardless of the setting of DDB4. When the SPI is enabled as a Slave, the data direction of this pin is controlled by DDB4. When the pin is forced by the SPI to be an input, the pull-up can still be controlled by the PORTB4 bit.
    PCINT4: Pin Change Interrupt source 4. The PB4 pin can serve as an external interrupt source.
  • MOSI/OC2/PCINT3 – Port B, Bit 3
    MOSI: SPI Master Data output, Slave Data input for SPI channel. When the SPI is enabled as a Slave, this pin is configured as an input regardless of the setting of DDB3. When the SPI is enabled as a Master, the data direction of this pin is controlled by DDB3. When the pin is forced by the SPI to be an input, the pull-up can still be controlled by the PORTB3 bit.
    OC2, Output Compare Match Output: The PB3 pin can serve as an external output for the Timer/Counter2 Compare Match. The PB3 pin has to be configured as an output (DDB3 set (one)) to serve this function. The OC2 pin is also the output pin for the PWM mode timer function.
    PCINT3: Pin Change Interrupt source 3. The PB3 pin can serve as an external interrupt source.
  • SS/OC1B/PCINT2 – Port B, Bit 2
    SS: Slave Select input. When the SPI is enabled as a Slave, this pin is configured as an input regardless of the setting of DDB2. As a Slave, the SPI is activated when this pin is driven low. When the SPI is enabled as a Master, the data direction of this pin is controlled by DDB2. When the pin is forced by the SPI to be an input, the pull-up can still be controlled by the PORTB2 bit.
    OC1B, Output Compare Match output: The PB2 pin can serve as an external output for the Timer/Counter1 Compare Match B. The PB2 pin has to be configured as an output (DDB2 set (one)) to serve this function. The OC1B pin is also the output pin for the PWM mode timer function.
    PCINT2: Pin Change Interrupt source 2. The PB2 pin can serve as an external interrupt source.
  • "So you can't use them both at the same pins" But I don't use them both at them same SS pin. I use pin 8 and pin 10. – DimChtz May 25 '17 at 20:03
  • @DimChtz: You have configured HW SPI interface (on pins 10,11,12,13), so it just can't be used with software implementation of SPI (by LedControl int pins 11 and 12). Maybe just try to add SPI.begin() into the second code example (LedControl only one) – KIIV May 25 '17 at 20:12
  • I am confused. Does this mean I can't use 2 spi devices in the same code? That's weird. And if I add SPI.begin() into the ledcontrol only code leds won't work. – DimChtz May 25 '17 at 20:21
  • @DimChtz You can use more SPI devices with HARDWARE SPI (just with different SS). But you can't combine libraries that uses HARDWARE SPI with libraries that uses SOFTWARE SPI on same pins. – KIIV May 25 '17 at 20:25
  • @KIIV, you could edit info from comments into answer. – James Waldby - jwpat7 May 25 '17 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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