3 Replies Latest reply on Aug 5, 2015 4:58 PM by CMata_Intel

    Mraa I2C reading


      Hello guys,


      I am using a proximity sensor that has the following read/write protocol.


      This device has a command code (register address) and then you read two bytes from a register as you can see in the picture bellow.



      I am using python mraa to read/write the registers; however I am not having success, and the mraa documentation is really poor. How can I read the two bytes from a register? Can you guys help me with this? what might be wrong in my code?



      import mraa as m


      DEVICE_ID = 0x0C


      def setup():

           "Check the Product ID"

            vcnl = m.I2c(0)





            lbyte = vcnl.read(2)



        • 1. Re: Mraa I2C reading

          Hi Emanuel,


          I looked for the datasheet of the sensor you are using: VCNL4040-datasheet and also for some examples using MRAA and I2C, I found:


          Edison/I2Ctest.py at master

          mraa/rgblcd.py at master

          mraa/bmp85.py at master

          Example — mraa v0.7.3-13


          mraa/Bmp85.java at master


          mraa/I2c-compass.cpp at master

          I also found an example for this language using the same sensor: C_softwarecode.pdf


          mraa/rgblcd.js at master

          mraa/bmp85.js at master


          mraa/i2c_HMC5883L.c at master


          Looking at the protocol and the code you have entered. You need to first initialize the I2C interface [vcnl = m.I2c(0)], then write the address [vcnl.address(0x60)], then write to the register [vcnl.writeReg(register,data)] and finally the address again [vcnl.address(0x60)] (according to the Receive Byte Protocol). Finally you should receive the bytes requested. Basically, you will need to write the specific command to the register to start receiving data.


          More information:  API MRAA - Python


          Are you testing the lines to see the data in the SDA and SCL pins?




          • 2. Re: Mraa I2C reading

            Hi CMata_Intel,


            Thanks for replying. Based on what you wrote I have the following code:


            import mraa


            VCNL_4040 = 0X60

            DEVICE_ID = 0x0C


            _send = [0,0]

            _receive = [0,0]


            vcnl = mraa.I2c(0)


            vcnl.writeReg(DEVICE_ID, 0x00)



            _receive[0] = vcnl.readReg(DEVICE_ID)

            _receive[1] = vcnl.readReg(DEVICE_ID)


            I'm note sure what data should I write in this command [vcnl.writeReg(register,data)]. As you can see in the datasheet, it doesn't provide a register address and a command code, just a command code that I am assuming is the register address.


            • 3. Re: Mraa I2C reading



              If you want to read or write to the board, the table 13 should work for you, the same you are using right now. You can see there the Command Codes from 0x08 - 0x0C that are linked to these registers.

              About the parameter data, in table 13 there is a column called Bit, you can see the specifications for these ones in the Description column. Also, if you go to table 2, you will find more information about the format of this parameter. I hope this helps you to understand the format. But if you still have doubts I suggest you to contact them Vishay - Contacts I think they we will have more information about this product.