Conflict with MFRC522 and ST7735 libraries

Status
Not open for further replies.

KBR

Active member
I am trying to get an LCD screen to work with an rfid reader using the MFRC522 lib with the ST7735 lib on Teensy 3.2.

Here are the LCD and RFID reader:
https://www.aliexpress.com/item/J34...32599685873.html?spm=2114.40010308.4.2.YM6Z0v
https://www.jaycar.co.nz/arduino-compatible-rfid-read-and-write-kit/p/XC4506

I can get each to work independently but when I initialize the LCD the rfid reader fails with this warning:
WARNING: Communication failure, is the MFRC522 properly connected?

I suspect it is a communication problem involving SPI but I am not advanced enough to solved this. Have tried for many hours.

I have assigned the pins so that there is no conflict and both libraries appear to utilize the SPI.beginTransaction() and SPI.endTransaction() functions. (I found a version of ST7735 with this but it did not solve the issue).

Here is the code I am using. If I comment out the line 'tft.initR(INITR_BLACKAB);' then the reader works. Screen works if I un-comment but not both at the same time.

Code:
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h> 
#include <SPI.h>
#include <MFRC522.h>

#define sclk 14  // SCLK can also use pin 14
#define mosi 7  // MOSI can also use pin 7
#define cs   20  // CS & DC can use pins 2, 6, 9, 10, 15, 20, 21, 22, 23
#define dc   21   //  but certain pairs must NOT be used: 2+10, 6+9, 20+23, 21+22
#define rst  8   // RST can use any pin
#define sdcs 4   // CS for SD card, can use any pin

#define RST_PIN    9   // 
#define SS_PIN    10    //

Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, mosi, sclk, rst);

MFRC522 mfrc522(SS_PIN, RST_PIN);


void setup(void) {
  pinMode(sdcs, INPUT_PULLUP);  // don't touch the SD card

  pinMode(SS_PIN, OUTPUT);
  
  Serial.begin(115200);

  while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)

  tft.initR(INITR_BLACKTAB);

  SPI.begin();
  mfrc522.PCD_Init();   // Init MFRC522
  mfrc522.PCD_DumpVersionToSerial();  // Show details of PCD - MFRC522 Card Reader details
  Serial.println(F("Scan PICC to see UID, SAK, type, and data blocks..."));
}


void loop() 
{
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  // Dump debug info about the card; PICC_HaltA() is automatically called
  mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
}

Any help would be most appreciated!
 
Last edited:
Might help to know for sure which library you are using:

Example is it this one? https://github.com/miguelbalboa/rfid

If so it looks like it support SPI transactions.

Might add after: pinMode(SS_PIN, OUTPUT);
Code:
 digitalWrite(SS_PIN, HIGH);
To make sure that the device does not think the init stuff for the display are for it.
 
Yes good point. Should have supplied that info.

I am using the one you mentioned for the rfid:
https://github.com/miguelbalboa/rfid

I am also using this version of the ST7735 lib as it apears to support the begin/end transaction feature:
https://github.com/adafruit/Adafruit-ST7735-Library

I tried the ST7735 on this page originally with the same issue:
https://www.pjrc.com/teensy/td_libs_ST7735.html

I tried adding the line you suggested but no apparent change.

Do I need to use pull-ups any where?

Here is my current configuration:

LCD --> Teensy 3.2
vcc --> vin (thought this was strange)
gnd --> gnd
cs --> 20
reset -- > 8
a0 (dc) --> 21
sda (mosi) --> 7
sck --> 14
led --> 100R --> vin (also strange?)

RFID-RC522 --> Teensy 3.2
vcc --> 3.3v (also had on vin at one stage)
gnd --> gnd
reset --> 9
miso --> 12
mosi --> 11
sck --> 13
nss (ss?) --> 10
 
UPDATE:

Seems I am not using the libraries that I thought I was. I thought I was using the libs in my documents->arduino->libraries folder but was probably using the default ones installed with the teensy in the arduino->hardware->libraries folder without realizing it. I have removed the ones in the hardware folder and now the screen doesn't seem to work with the library I thought I was using before. I don't see the begin/end transaction function being called anywhere in the default one. Is this the problem? If so which libs should I use?!?

So just to be clear neither of the ST7735 libraries that I mentioned above work I get compile errors for this lib:
https://www.pjrc.com/teensy/td_libs_ST7735.html

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
C:\Users\K\Documents\Arduino\libraries\Adafruit_ST7735\Adafruit_ST7735.cpp: In member function 'void Adafruit_ST7735::writecommand(uint8_t)':

C:\Users\K\Documents\Arduino\libraries\Adafruit_ST7735\Adafruit_ST7735.cpp:201:3: error: 'SPI0' was not declared in this scope

SPI0.PUSHR = c | (pcs_command << 16) | SPI_PUSHR_CTAS(0);

^

C:\Users\K\Documents\Arduino\libraries\Adafruit_ST7735\Adafruit_ST7735.cpp: In member function 'void Adafruit_ST7735::writedata(uint8_t)':

C:\Users\K\Documents\Arduino\libraries\Adafruit_ST7735\Adafruit_ST7735.cpp:214:3: error: 'SPI0' was not declared in this scope

SPI0.PUSHR = c | (pcs_data << 16) | SPI_PUSHR_CTAS(0);

^

C:\Users\K\Documents\Arduino\libraries\Adafruit_ST7735\Adafruit_ST7735.cpp: In member function 'void Adafruit_ST7735::writedata16(uint16_t)':

C:\Users\K\Documents\Arduino\libraries\Adafruit_ST7735\Adafruit_ST7735.cpp:227:3: error: 'SPI0' was not declared in this scope

SPI0.PUSHR = d | (pcs_data << 16) | SPI_PUSHR_CTAS(1);

^

C:\Users\K\Documents\Arduino\libraries\Adafruit_ST7735\Adafruit_ST7735.cpp: In member function 'void Adafruit_ST7735::setBitrate(uint32_t)':

C:\Users\K\Documents\Arduino\libraries\Adafruit_ST7735\Adafruit_ST7735.cpp:285:2: error: 'SPI0' was not declared in this scope

SPI0.MCR = SPI_MCR_MDIS | SPI_MCR_HALT;

^

C:\Users\K\Documents\Arduino\libraries\Adafruit_ST7735\Adafruit_ST7735.cpp: In member function 'void Adafruit_ST7735::commonInit(const uint8_t*)':

C:\Users\K\Documents\Arduino\libraries\Adafruit_ST7735\Adafruit_ST7735.cpp:549:3: error: 'SPI0' was not declared in this scope

SPI0.MCR = SPI_MCR_MDIS | SPI_MCR_HALT;

^

Error compiling for board Teensy 3.2 / 3.1.
-------------------------------------------------------------------------------------------------------------------------------------------------------

And this one just shows white lit screen (usually black when working have tested other ways as well without success):
https://github.com/adafruit/Adafruit-ST7735-Library

Only default ST7735 lib installed with teensy (arduino->hardware->libraries) works but then I get the original conflict issue.
 
Last edited:
Making some progress (I think).

I tried rewiring so that both the LCD and the RFID share the same mosi and sclk like so:

LCD --> Teensy 3.2
vcc --> vin
gnd --> gnd
cs --> 20
reset -- > 8
a0 (dc) --> 21
sda (mosi) --> 11
sck --> 13
led --> 100R --> vin

RFID-RC522 --> Teensy 3.2
vcc --> 3.3v
gnd --> gnd
reset --> 9
miso --> 12
mosi --> 11
sck --> 13
nss (cs?) --> 10

Now when I uncomment the screen init I don't get the warning any more but it still doesn't work. The address seems to be unknown/wrong:

Version: 0x80 = (unknown)
Scan PICC to see UID, SAK, type, and data blocks...

When I comment the screen init out it works and this is the message:

Firmware Version: 0x92 = v2.0
Scan PICC to see UID, SAK, type, and data blocks...


Must be close now! Any ideas anyone?..
 
Okay, some more progress. Didn't realize that the a0 (dc) pin on the LCD was the miso pin (still not sure about this). Now the a0 on the LCD and the miso pin on the RFID reader share pin 12.

The RFID reader now works but the screen no longer updates when I try to redraw. Ideas?.. anyone?..
 
Last edited:
Latest wiring:

LCD --> Teensy 3.2
vcc --> vin
gnd --> gnd
cs --> 15
reset -- > 8
a0 (dc) --> 12
sda (mosi) --> 11
sck --> 13
led --> 100R --> vin

RFID-RC522 --> Teensy 3.2
vcc --> 3.3v
gnd --> gnd
reset --> 9
miso --> 12
mosi --> 11
sck --> 13
nss (cs?) --> 10


Latest code:

Code:
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#include <MFRC522.h>

#define sclk 13  // SCLK can also use pin 14
#define mosi 11  // MOSI can also use pin 7
#define cs   15  // CS & DC can use pins 2, 6, 9, 10, 15, 20, 21, 22, 23
#define dc   12   //  but certain pairs must NOT be used: 2+10, 6+9, 20+23, 21+22
#define rst  8   // RST can use any pin
#define sdcs 4   // CS for SD card, can use any pin

#define RST_PIN    9   // 
#define SS_PIN    10    //

Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, mosi, sclk, rst);

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance


void setup(void) 
{
  //pinMode(sdcs, INPUT_PULLUP);  // don't touch the SD card

  pinMode(SS_PIN, OUTPUT);
  pinMode(cs, OUTPUT);

  digitalWrite(SS_PIN, HIGH);   
  //digitalWrite(cs, HIGH);   
  
  Serial.begin(115200);
  
  while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)

  tft.initR(INITR_BLACKTAB);
  tft.fillScreen(ST7735_BLACK);
  testdrawtext("I think therefore I am", ST7735_WHITE);

  SPI.begin();
  mfrc522.PCD_Init();   // Init MFRC522
  mfrc522.PCD_DumpVersionToSerial();  // Show details of PCD - MFRC522 Card Reader details

  tft.fillScreen(ST7735_BLUE);
  testdrawtext("Redraw", ST7735_WHITE);
}

void loop() 
{
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  // Dump debug info about the card; PICC_HaltA() is automatically called
  mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
}


Screen shows black with text "I think therefore I am" and RFID reader works. But the blue screen with "Redraw" never shows.

Any guesses are welcome!
 
Solved!

Finally got it working with this configuration:


LCD --> Teensy 3.2
vcc --> vin
gnd --> gnd
cs --> 10
reset -- > 7
a0 (dc) --> 8
sda (mosi) --> 11
sck --> 13
led --> 100R --> vin

RFID-RC522 --> Teensy 3.2
vcc --> 3.3v
gnd --> gnd
reset --> 9
miso --> 12
mosi --> 11
sck --> 13
nss (cs) --> 20

Also had to remove the ST7735 lib from the teensy install and use this one instead:
https://github.com/adafruit/Adafruit-ST7735-Library

Final code:

Code:
#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#include <MFRC522.h>

#define sclk 13  // SCLK can also use pin 14
#define mosi 11  // MOSI can also use pin 7
#define cs   10  // CS & DC can use pins 2, 6, 9, 10, 15, 20, 21, 22, 23
#define dc   8   //  but certain pairs must NOT be used: 2+10, 6+9, 20+23, 21+22
#define rst  7   // RST can use any pin
#define sdcs 4   // CS for SD card, can use any pin

#define RST_PIN    9   // 
#define SS_PIN    20    //

Adafruit_ST7735 tft = Adafruit_ST7735(cs,  dc, rst);

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance


void setup(void) 
{
  pinMode(sdcs, INPUT_PULLUP);  // don't touch the SD card

  pinMode(SS_PIN, OUTPUT);
  pinMode(cs, OUTPUT); 
  
  Serial.begin(115200);
  while (!Serial);    // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)

  Serial.println("init lcd");
  tft.initR(INITR_BLACKTAB);
  tft.fillScreen(ST7735_BLACK);
  testdrawtext("I think therefore I am", ST7735_WHITE);

  //SPI.begin();

  mfrc522.PCD_Init();   // Init MFRC522
  mfrc522.PCD_DumpVersionToSerial();  // Show details of PCD - MFRC522 Card Reader details
  
  drawSpeechBubbleWithText("Helloo!");
}

void drawSpeechBubbleWithText(const char *text)
{
  tft.fillScreen(ST7735_BLUE);
  tft.fillRoundRect(10, 10, 100, 30, 5, ST7735_WHITE);
  tft.setCursor(20, 20);
  tft.setTextColor(ST7735_BLACK);
  tft.setTextWrap(true);
  tft.print(text);
}

void loop() 
{
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }

  // Dump debug info about the card; PICC_HaltA() is automatically called
  mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
}

void testdrawtext(const char *text, uint16_t color) {
  tft.setCursor(0, 0);
  tft.setTextColor(color);
  tft.setTextWrap(true);
  tft.print(text);
}

Life is now a wonderful thing again. All is flowers and double rainbows ;)
 
Maybe try changing this:

Code:
Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, mosi, sclk, rst);

to this:

Code:
Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);
 
Yes, thanks Paul. This was part of the solution, as I mentioned in my previous reply. But it is also important to note that it will not work with the Adafruit_ST7735 library that is (optionally) installed with Teensyduino v1.35 for windows. I had to delete that one and use this one instead:
https://github.com/adafruit/Adafruit-ST7735-Library

I don't think the installed version supports begin/end transactions? You would know better than I!
 
Status
Not open for further replies.
Back
Top