frame

How to Use I2C protocol?

I have a NodeMcu which is compatible with the zerynth studio. It has a sensor connected with the I2C as per I2C0 (in zerynth pinmap). I am unable to find the function for initializing the i2c connection between them. The I2C class documentation is given, but i am unable to use it. The sensor i have connected with it is MPU 6050. Please Help.

Comments

  • edited March 17
    Hi ameersohail,

    To use the I2C protocol, you can create a subclass of the  Zerynth i2c class with customized functions related to your sensor.
    You can take as reference model one of the Zerynth libraries for i2c sensor (bmp180 for example).

    Let me know if this can help or if you need more advices :)
    Matteo Cipriani
    Zerynth Support Team
  • Hi guys,

    I re-open this thread to add more details about I2C protocol and how to use it with Zerynth.
    To create an I2C object you can use this sample code:

    import i2c
    
    class Your_Sensor(i2c.I2C):
        #Init
        def __init__(self, drvname, addr=I2CADDR, clk=I2CCLOCK):
            i2c.I2C.__init__(self,drvname,addr,clk)
            self._addr = addr
            try:
                self.start()
            except PeripheralError as e:
                print(e)
    Where "dvrname" is the I2C driver name ("I2C0",  "I2C1", etc.), addr is the I2C address of your sensor (you can find it in sensor datasheet) and clk is the clock frequency of the peripheral (usually 100 or 400 kHz).

    Calling the i2c.I2C.__init__() function the object is initialized and ready to be used with all i2c.I2C methods and to be customized with all methods you need to interrogate your i2c sensor.

    In your_home/zerynth2/dist/r2.0.10/libs/official (or ~/.zerynth2/dist/r2.0.10/libs/official in Linux or Mac platforms) you can find all official Zerynth library source code; an example of I2C sensor implemetation is the bosh/bmp180 Barometric Pressure sensor.

    To write data you can use the "write()" method of the i2c.I2C class remembering that you have to pass as argument a bytearray with first element the address register you want to modify and then data you want to write.
    You can also use the "write_bytes()" method passing separately address and data (ex: self.write_bytes(ADDR, VAL)).

    To read data from the I2C buffer, you can use the "read()" method if your sensor sends data for example when they are ready or you can use the "write_read()" method passing as argument the address register and the number of consecutive registers you want to read.

    Happy Coding!!!



    Matteo Cipriani
    Zerynth Support Team
Sign In or Register to comment.

ZERYNTH Community

@ 2016 Zerynth.com, all rights reserved.