ThingPulse / esp8266-oled-ssd1306

Driver for the SSD1306 and SH1106 based 128x64, 128x32, 64x48 pixel OLED display running on ESP8266/ESP32
https://thingpulse.com
Other
1.98k stars 636 forks source link

Unexplained error in combination of RC522 and SSD1306 #330

Closed MeCK-Frog closed 3 years ago

MeCK-Frog commented 3 years ago

Dear developers

I have a question because an error of unknown cause has occurred when RC522 and SSD1306 are combined. If you don't show the display, the RC522 works fine, but if you show the display, the RC522 doesn't work. I believe SSD1306 is occupying SDA. Do you know the cause?

I will attach the source code. Thank you.

//共通ライブラリ
#include "Wire.h" //I2C通信用ライブラリを読み込み
#include <SPI.h>  //SPIライブラリ
//各センサーライブラリ
#include <SSD1306.h> //ディスプレイ用ライブラリを読み込み
#include <MFRC522.h> //RFIDモジュールライブラリ

// Pins
#define RC522_INT 32    // RC522の割り込みピン
#define RST_PIN 4       // RFIDのリセットピン
#define SS_PIN 21       // RC522のSDA指定
#define UID1 "04 0C C4 22 1E 70 81"  // 読み取ったUIDを入れる(空白も必ず保持)
#define UID2 "04 08 C4 22 1E 70 81"  // 読み取ったUIDを入れる(空白も必ず保持)

// Global variables
MFRC522 mfrc522(SS_PIN, RST_PIN);//MFRC522インスタンスの作成
SSD1306  display(0x3c, 21, 22); //SSD1306インスタンスの作成(I2Cアドレス,SDA,SCL)
uint16_t proximity_data = 0;

void setup(){
  Serial.begin(9600);    
  while (!Serial);   
  SPI.begin();      // Init SPI bus
  mfrc522.PCD_Init();   // Init MFRC522
  delay(4);       // Optional delay. Some board do need more time after init to be ready, see Readme
  mfrc522.PCD_DumpVersionToSerial();  // Show details of PCD - MFRC522 Card Reader details
  Serial.println("Ready to use RC522");
}

void loop(){
  Detection();
}

void Detection(){
  //マガジンのUID取得処理
  if ( ! mfrc522.PICC_IsNewCardPresent()) { //RFIDタグが検出されたかどうか
    Serial.println("getUID IsNewCard");
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) {//RFIDタグからデータを読み取れるかどうか
    Serial.println("ReadCardSerial PICC_ReadCardSerial");
    return;
  }
  String strBuf[mfrc522.uid.size];
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    strBuf[i] =  String(mfrc522.uid.uidByte[i], HEX);  // (E)using a constant integer
    if(strBuf[i].length() == 1){  // 1桁の場合は先頭に0を追加
        strBuf[i] = "0" + strBuf[i];
    }
  }
  String strUID = strBuf[0] + " " + strBuf[1] + " " + strBuf[2] + " " + strBuf[3]+ " " + strBuf[4]+ " " + strBuf[5]+ " " + strBuf[6];
  Serial.println(strUID);
  //UID長い時は下のコードみたいに伸ばしておけばOK

  if ( strUID.equalsIgnoreCase(UID1) ){  // 大文字小文字関係なく比較
    //UIDがUID1と一致した際の処理を書く
    Serial.print("This tag is UID1");
    Serial.println(UID1);
    Serial.println(strUID);
    display.init();
    display.drawString(20, 20, "This tag is UID1");
    display.display();
    return;
  }
  if ( strUID.equalsIgnoreCase(UID2) ){
    Serial.print("This tag is UID2");
    Serial.println(UID2);
    Serial.println(strUID);
    display.init();
    display.drawString(20, 20, "This tag is UID2");
    display.display();
    return;
  }
}
Rotzbua commented 3 years ago

The mfrc522 does not use i2c. This Chinese boards are wrong labeled for years.... For SS (spi select) you can chose any free pin. So just change line:

#define SS_PIN 21       // RC522のSDA指定

@marcelstoer Can be closed.