#include <Wire.h>
// NTAG5 I2C address
#define NTAG5_I2C_ADDRESS 0x54
void setup() {
Serial.begin(9600); // Initialize serial communication for debugging
Wire.begin(); // Initialize the I2C communication
Wire.beginTransmission(NTAG5_I2C_ADDRESS);
int error = Wire.endTransmission();
Serial.println(error);
if(error == 0)
{
Serial.println( "The sensor is detected.");
}
else
{
Serial.println( "Error, no sensor found.");
}
}
void loop() {
// Reading from register 0x103D
readRegister(0x103D);
// Example of writing to register 0x103D
writeRegister(0x103D, 0x7D); //
delay(5000); // Delay for 5 seconds before repeating the process
}
void readRegister(uint16_t registerAddress) {
uint8_t regAddrHigh = (uint8_t)((registerAddress >> 8)& 0xFF);
uint8_t regAddrLow = (uint8_t)(registerAddress & 0xFF);
Wire.beginTransmission(NTAG5_I2C_ADDRESS);
Wire.write(regAddrHigh); // MSB of the register address
Wire.write(regAddrLow); // LSB of the register address
Wire.endTransmission(false); // End transmission but don't release the I2C bus
Wire.requestFrom(NTAG5_I2C_ADDRESS, 1);
while (Wire.available()) {
int readValue = Wire.read(); // Read the register data
uint8_t data = (uint8_t)readValue; // Cast to uint8_t to avoid sign extension issues
Serial.print("Read data: 0x");
Serial.println(data, HEX); // Print the read data in HEX format
}
}
void writeRegister(uint16_t registerAddress, uint8_t data) {
uint8_t regAddrHigh = (uint8_t)((registerAddress >> 8) & 0xFF);
uint8_t regAddrLow = (uint8_t)(registerAddress & 0xFF);
Wire.beginTransmission(NTAG5_I2C_ADDRESS);
Wire.write(regAddrHigh); // MSB of the register address
Wire.write(regAddrLow); // LSB of the register address
Wire.write(data); // Data to write
int error = Wire.endTransmission();
Serial.println(error);
Serial.println("Write operation completed.");
}
So on running this code, I can read the register value as 4D which I had set using the NFC tools command. But when I am trying to Write on that register and change the value from 4D to 7D, I can do it using NFC tools but cannot do it using the Arduino UNO using I2C communication. I can only read using the UNO but not write. Can someone please help me out? It is urgent and important for me.
Thanks