I2C samples for nRF9160

Hi,

I had previously asked about SPI interfacing of the RFID card reader and I was successful in creating a custom application using connect SDK 1.5.1.

But according to our latest upgrade, we plan to use RFID card readers using I2C communication. Are there any samples of I2C that I can make use of, to develop an application/project on top of it for interfacing RFID card readers? 

Can someone share basic I2C samples or header files for testing in nRF9160DK?

Parents
  • Hi 

    You can use the sample shared by Jupyter, I believe it is the same one shared by one of my colleagues here

    Setting up I2C is actually quite simple in the nRF Connect SDK, and can be summarized like this:

    1) Set up the config (in prj.conf):

    CONFIG_I2C=y

    2) Add the right include folder

    #include <drivers/i2c.h>

    3) Get a binding to your device

    #define MY_I2C "I2C_1"
    const struct device * i2c_dev;
    
    // Get the device binding to the I2C peripheral
    i2c_dev = device_get_binding(MY_I2C);
    if (i2c_dev == NULL) {
    	printk("Unable to bind %s!\n", MY_I2C);
    	return;
    }

    4) Use the driver

    // Scan the I2C bus for any connected slaves, and print out the address if found
    for(uint8_t i2c_addr = 1; i2c_addr <= 127; i2c_addr++){
    	uint8_t i2c_cmd = 1;
    	int error = i2c_write(i2c_dev, &i2c_cmd, 1, i2c_addr);
    	if(error == 0){
    		printk("I2C slave found on address %x\n", i2c_addr);
    	}
    }

    Best regards
    Torbjørn

  • Hi,

    Thanks for the reply. But I need to read the tag ID using the mfrc522 RFID scanner. I tried modifying the main code. But I am getting error messages and it says the header and includes files are missing. should I need to place the includes directory separately? Can open this sample which Jupyter had shared, directly as an existing application or create a new application?

  • Hi 

    Which header and include files are missing? 

    If you have added additional source files or include directories to the project you typically need to update the CMakeLists.txt file to include these additional files (unless it is a standard library, then you just need to enable it in the project configuration). 

    Finny Philip Biju said:
    Can open this sample which Jupyter had shared, directly as an existing application or create a new application?

    You can use the "Add an existing application" option in VSCode, yes. 

    Best regards
    Torbjørn

Reply
  • Hi 

    Which header and include files are missing? 

    If you have added additional source files or include directories to the project you typically need to update the CMakeLists.txt file to include these additional files (unless it is a standard library, then you just need to enable it in the project configuration). 

    Finny Philip Biju said:
    Can open this sample which Jupyter had shared, directly as an existing application or create a new application?

    You can use the "Add an existing application" option in VSCode, yes. 

    Best regards
    Torbjørn

Children
No Data
Related