Skip to content

A PLAY EXPERIENCE MAKER'S WORK LOG FOR FUTURE SELF©2001 – 2023 Kyle Li 李肅綱 All Rights Reserved.

PaHub – RFID 2 Unit

Posted on May 24, 2025May 24, 2025 by admin

My next book game idea is of course a miniature battle royal style game. I had a quick prototype setup and didn’t expect the most difficult part is getting M5Stack’s PaHub to work with multiple I2C devices at the same time. It seems there is not enough documentation and code samples available on the internet.

The two major challenges that I ran into were:

  • RFID 2 I2C unit requires initiation when creates the instance (MFRC522 mfrc522(0x28);), how do I work this into the channel selection while using PaHub?
  • I used the PaHub example from M5Stack, somehow 0x28 (RFID 2 default address) shows up in CH0 when RFID 2 unit was plugged into CH1.

It looks like other folks also ran into the same problem.
https://community.m5stack.com/topic/6995/rfid-2-unit-and-i2c-hub-1-to-6-expansion-unit/9

Interestingly, I used the I2C Scanner Example Code from Adafruit. It actually sort the 0x28 under the right channel which is 1 in this case.
…
PCA Port #1
Found I2C 0x10
Found I2C 0x28
Found I2C 0x68
Found I2C 0x75
…

#include "Wire.h"
#define PCAADDR 0x70

void pcaselect(uint8_t i) {
  if (i > 7) return; 
  Wire.beginTransmission(PCAADDR);
  Wire.write(1 << i);
  Wire.endTransmission();  
}

// standard Arduino setup()
void setup()
{
    while (!Serial);
    delay(1000);

    Wire.begin();
    
    Serial.begin(115200);
    Serial.println("\nPCAScanner ready!");
    
    for (uint8_t t=0; t<8; t++) {
      pcaselect(t);
      Serial.print("PCA Port #"); Serial.println(t);

      for (uint8_t addr = 0; addr<=127; addr++) {
        if (addr == PCAADDR) continue;

        Wire.beginTransmission(addr);
        if (!Wire.endTransmission()) {
          Serial.print("Found I2C 0x");  Serial.println(addr,HEX);
        }
      }
    }
    Serial.println("\ndone");
}

void loop() 
{
}

I continued to follow the Adafruit tutorial, with some modifications, I got it to work! Now on to the 2nd I2C device. Original code can be found here: Multi-Sensor Example Code

#include <M5Stack.h>

#include "MFRC522_I2C.h"
#include "ClosedCube_TCA9548A.h"

#define FRONT 2

#define X_LOCAL 100
#define Y_LOCAL 35
#define X_OFFSET 160
#define Y_OFFSET 34

#define PaHub_I2C_ADDRESS 0x70
//#define DEV_I2C Wire

ClosedCube::Wired::TCA9548A tca9548a;
MFRC522 mfrc522(0x28);

void pcaselect(uint8_t i) {
  if (i > 6) return;
  Wire.beginTransmission(PaHub_I2C_ADDRESS);
  Wire.write(1 << i);
  Wire.endTransmission();
}

void setup() {
  M5.begin();
  M5.Power.begin();
  Wire.begin();
  tca9548a.address(PaHub_I2C_ADDRESS);  // Set the I2C address.
  M5.Lcd.setTextFont(4);
  M5.Lcd.setCursor(70, 0, 4);
  M5.Lcd.setTextColor(YELLOW, TFT_BLACK);
  M5.Lcd.println(("PaHUB Sensors"));
  M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);

  Serial.println("CH1 MFRC522");
  // define the port on the PCA9548A for the first sensor
  pcaselect(1);
  mfrc522.PCD_Init();  // Init MFRC522
}

void loop() {
  // define port on the PCA9548A
  pcaselect(1);
  M5.Lcd.setCursor(40, 47);
  if (!mfrc522.PICC_IsNewCardPresent() || !mfrc522.PICC_ReadCardSerial()) {
    delay(200);
    return;
  }
  M5.Lcd.fillRect(42, 47, 320, 20, BLACK);
  for (byte i = 0; i < mfrc522.uid.size; i++) {  // Output the stored UID data
    M5.Lcd.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
    M5.Lcd.print(mfrc522.uid.uidByte[i], HEX);
  }
  M5.Lcd.println("");
}

Posted in Experiements

Post navigation

Chuck’s Fly School
PaHub – 1 Servo

Recent Posts

  • 臧式毅
  • Momotarō 桃太郎
  • PaHub – 1 Servo
  • PaHub – RFID 2 Unit
  • Chuck’s Fly School

Archives

Proudly powered by WordPress | Theme: MiniZen by Martin Stehle.