2

I am trying to control both RC522 and lcd st7920 (128x64) via a single SPI bus on the NodeMCU esp8266. I am using Arduino core for my esp8266. Now in this portion of the code, I only want to display the UID of the card on the lcd each time a card is scanned on the RC522. The code is as follows:

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

#define SPI_SCK_pin       14  // Clock pin of SPI
#define SPI_MOSI_pin      13  // MOSI pin of SPI

#define SPI_RST_pin       0   // RST-PIN for RC522 - RFID - SPI - Modul GPIO5 
#define SPI_rfid_SS_pin   15  // SDA-PIN for RC522 - RFID - SPI - Modul GPIO4 
MFRC522 mfrc522(SPI_rfid_SS_pin, SPI_RST_pin); // Create MFRC522 instance

#define SPI_lcd_SS_pin    5   // Slave Select pin of SPI for lcd<->RS
U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R2, SPI_SCK_pin, SPI_MOSI_pin, SPI_lcd_SS_pin, U8X8_PIN_NONE); 

void setup() {
  SPI.begin();           // Init SPI bus
  Serial.begin(115200);    // Initialize serial communications
  

  u8g2.setFont(u8g2_font_amstrad_cpc_extended_8f);
}

void loop() {
  
  mfrc522.PCD_Init();    // Init MFRC522
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    delay(50);
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    delay(50);
    return;
  }

  Serial.print(F("Card UID:"));
  String uid = dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  Serial.println();
  digitalWrite(SPI_rfid_SS_pin, HIGH);   //return rfid slave pin back to normal

  u8g2.begin();
  u8g2.clearBuffer();
  u8g2.setCursor(3,10);
  u8g2.print (uid);
  u8g2.sendBuffer();
  digitalWrite(SPI_lcd_SS_pin, HIGH);   //return lcd slave pin back to normal
//  delay (10);
  
}

// Helper routine to dump a byte array as hex values to Serial
String dump_byte_array(byte *buffer, byte bufferSize) {
  String userid;
  for (byte i = 0; i < bufferSize; i++) {
      userid += String(buffer[i]);
  }
  Serial.print(userid);
  return userid;
}

I am new to using the esp8266 and this is my first time using multiple slaves on a single SPI bus so I may not know the correct way to using multiple slaves. I tried connecting only the rfid rc522 with esp8266 via SPI and it worked flawlessly with the schematic. Similarly the lcd also works flawlessly individually on the SPI bus with the same schematic.

The issue only exists if I try to connect both via SPI. The problem is that only the first time a card is scanned, is it displayed on the lcd. After that no more cards are scanned and the only the first card scanned is displayed on the lcd.

What I am trying to do is that every time a card is scanned, its UID be displayed on the lcd. I've posted the schematic and my actual setup which you can check.

Schematic here My actual setup

5
  • with the display you use software spi on hardware spi pins. the reader uses the hardware spi. find out how to use the display with hardware spi – Juraj Oct 28 '20 at 10:21
  • @Juraj Fancy making that into a proper answer along with suggestions of how it may be "fixed"? – Majenko Oct 28 '20 at 21:53
  • @Majenko sorry, no. OP didn't give feedback so II would not write an answer without at least compiling the code. and of course I don't have the devices to test it. – Juraj Oct 29 '20 at 12:19
  • @Juraj the lcd uses software spi. There is only one spi bus available on the esp8266 – theMubashir Nov 5 '20 at 2:27
  • so you think it is a good idea to use software spi on the same pins as hardware spi? spi devices are selected by the CS pin to share the bus, but you can't share the pins for two different SPI libraries – Juraj Nov 5 '20 at 5:38

Your Answer

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

Browse other questions tagged or ask your own question.