I have an adafruit TCA9548A I2C multiplexer and a PN532 NFC reader. I can't get them to work.
I'm running off an Arduino Uno, I hooked up the multiplexer with the guide and tested it out with a I2C 16x2 LCD and I'm able to call to it and write stuff to it. Code below.
//------TCA9548A I2C multiplexer----------------------
#include <Wire.h>
#define TCAADDR 0x70
void tcaselect(uint8_t i)
{
if (i > 7) return;
Wire.beginTransmission(TCAADDR);
Wire.write(1 << i);
Wire.endTransmission();
}
//--------------LCD-----------------------------------------------
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27 ,16,2); // set the LCD address to 0x3F for a 16 chars and 2 line display
//-------------------------------------------------------------
int output_pin=10;
void setup(void)
{
pinMode(output_pin,OUTPUT);
Serial.begin(9600);
Serial.println("TCA9548A Test");
/* Initialise the 1st sensor */
tcaselect(7);
lcd.init();
lcd.clear();
lcd.backlight(); // Make sure backlight is on
}
void loop(void)
{
// Print a message on both lines of the LCD.
lcd.setCursor(0,0); //Set cursor to character 0 on line 0
lcd.print("Hello my world!");
lcd.setCursor(0,1); //Move cursor to character 0 on line 1
lcd.print("LCD Tutorial");
}
I used the I2C scanner sketch they used in the guide to see if the device is recognized and the PN532 is there, but when I try to run a sketch with the multiplex code with the PN532 the code stalls at the first serial print and only prints out "I2I2I2" on the serial monitor. I tried asking Adafruit tech support and they said that it was
"The Wire library is prone to hanging when there is a problem on the i2c bus. Since it only hangs when the PN532 is active, the PN532 is the primary suspect. I explained in my earlier post how the PN532 can be a major source of electromagnetic interference."
I added twisted pairs of cables to the connections to the PN532 and the problem persists. I can get the PN532 working independently so i know my connections and PN532 sketch works. I'm not sure how to diagnose stuff that happens in the background of Arduino. Thank you for any help in advance.
//------TCA9548A I2C multiplexer----------------------
#include <Wire.h>
#define TCAADDR 0x70
void tcaselect(uint8_t i)
{
if (i > 7) return;
Wire.beginTransmission(TCAADDR);
Wire.write(1 << i);
Wire.endTransmission();
}
//--------------PN532-----------------------------------------------
#include <DFRobot_PN532.h>
#define BLOCK_SIZE 16
#define PN532_IRQ (2)
#define INTERRUPT (1)
#define POLLING (0)
DFRobot_PN532_IIC nfc1(PN532_IRQ, POLLING);
DFRobot_PN532:: sCard_t NFC1card;
//-------------------------------------------------------------
int output_pin=10;
void setup()
{
pinMode(output_pin,OUTPUT);
Serial.begin(115200);
Serial.println("I2C mult Test");
/* Initialise the 1st I2c Device */
tcaselect(1);
if(!nfc1.begin())
{
/* There was a problem starting the PN532 ... check your connections */
Serial.println("NFC 1 not connected");
while(1);
}
Serial.println("Please place the NFC card/tag on module..... ");
}
void loop()
{
tcaselect(1);
/* Call I2C 1 *///-------------------------
if (nfc1.scan())
{
//Read the basic information of the card
NFC1card = nfc1.getInformation();
Serial.println("----------------NFC1 card/tag information-------------------");
Serial.print("UID Lenght: "); Serial.println(NFC1card.uidlenght);
Serial.print("UID: ");
for (int i = 0; i < NFC1card.uidlenght; i++) {
Serial.print(NFC1card.uid[i], HEX);
Serial.print(" ");
//call here
if (readRFID1()) {
// Access granted.
digitalWrite(output_pin, LOW);
digitalWrite(LED_BUILTIN, LOW);
Serial.println("Valid");
}
else {
// Access denied
digitalWrite(output_pin, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("Invalid");
}
}
Serial.println("");
}
else
{
//Serial.println("no card!");
digitalWrite(output_pin, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
}
//----------------------------------------
delay(500);
}
boolean readRFID1()
{
byte card_ID[4]; // card UID size 4byte
const int numOfCards = 1; // the number of cards used. this can change as you want
byte cards[numOfCards][7] = {{0x93, 0xDB, 0x1B, 0xFB}}; // array of UIDs of rfid cards
//1 0xFD, 0xF8, 0x92, 0xDA
//2 0x3D, 0xD2, 0x93, 0xDA
//3 0xCD, 0xE7, 0x94, 0xDA
//4 0x0D, 0x74, 0x94, 0xDA
//5 0xCD, 0x73, 0x94, 0xDA
//6 0x7D, 0xD2, 0x93, 0xDA
for (byte i = 0; i < NFC1card.uidlenght; i++)
{ card_ID[i] = NFC1card.uid[i]; }
for (int i = 0; i < numOfCards; i++)
{
if ((card_ID[0] == cards[i][0]) && (card_ID[1] == cards[i][1]) && (card_ID[2] == cards[i][2]) && (card_ID[3] == cards[i][3]))
{ return true; }
}
return false;
}




