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.


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 :slight_smile:


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):
def init(self, drvname, addr=I2CADDR, clk=I2CCLOCK):
self._addr = addr
except PeripheralError as e:

Where "dvrname" is the I2C driver name ("I2C0", &nbsp;"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).<br><br>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.<br><br>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.<br><br>To write data you can use the&nbsp;"write()" method of the i2c.I2C class remembering that you have to pass as argument a bytearray&nbsp;with first element the address register you want to modify and then data you want to write.<br>You can also use the "write_bytes()" method passing&nbsp;separately address and data (ex:&nbsp;self.write_bytes(ADDR, VAL)).<br><br>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.<br><br>Happy Coding!!!<br><br><br><br>