.. _tutorials_i2c: I2C =============== I2C (Inter-Integrated Circuit) literally means "between integrated circuits" - it is actually short for I2C Bus. The I2C bus type was designed by Philips Semiconductor in the early 1980s as a simple, bidirectional, two-wire, synchronous serial bus, mainly used to connect integrated circuits (ICs). The I2C protocol is a way for multiple devices to communicate with each other using only two wires (clock and data). Any device can be the master device that controls the I2C clock and data lines to communicate with other devices. Each I2C device is assigned a unique address used to identify it during read/write operations. When a device sees its address sent on the I2C bus, it responds to the request; when it sees a different address, it ignores it. Using unique addresses, many devices can share the same I2C bus without interference. --------------------------------------- When using the mPython Board, you can use the :ref:`I2C class` functions to interact with devices on the I2C bus. In most cases, you will act as the I2C "master" device, able to read and write data with other I2C devices. You can also act as an I2C "slave" or peripheral, which is assigned an address and can listen for and respond to requests from other I2C devices. Master ------ Most I2C communication modules have similar operation methods. The mPython Board acts as the I2C master device, and the module acts as the slave device responding to master requests. The following SHT20 module is used as a demonstration of how to read slave device data. .. figure:: /_static/products/ledong-v2/_static/image/example/dht11.png :align: center :width: 250 blue:bit Temperature and Humidity Module (SHT20) Read SHT20 temperature function:: def sht20_temperature(): i2c.writeto(0x40,b'\xf3') sleep_ms(70) t=i2c.readfrom(0x40, 2) return -46.86+175.72*(t[0]*256+t[1])/65535 ``i2c.writeto(addr, buf)`` is the I2C write operation function, sending ``buf`` bytes to the I2C device at address ``addr``. The mPython Board needs to send the ``0xf3`` byte to SHT20, telling it to read temperature data. After a 70ms delay, it then reads 2 bytes of data from SHT20. The read operation uses ``i2c.readfrom(addr, nbytes)``, where ``nbytes`` is the number of bytes to read. After reading 2 bytes of data, data processing is still needed according to the SHT20 datasheet to convert the temperature unit. The conversion formula is shown below. .. figure:: /_static/products/ledong-v2/_static/image/tutorials/sht20_temper.png :align: center :width: 400 SHT20 temperature conversion formula .. figure:: /_static/products/ledong-v2/_static/image/tutorials/sht20_humidi.png :align: center :width: 400 SHT20 humidity conversion formula Humidity reading works similarly. First send the "0xf5" byte to tell SHT20 to read humidity data, then convert the humidity unit according to the formula:: def sht20_humidity(): i2c.writeto(0x40,b'\xf5') sleep_ms(25) t=i2c.readfrom(0x40, 2) return -6+125*(t[0]*256+t[1])/65535 .. Hint:: For more I2C operation methods, please refer to the :ref:`I2C class` chapter. Complete SHT20 example: .. code-block:: python :linenos: from mpython import * # Import all mpython objects def sht20_temperature(): """Get the temperature value from the SHT20 module Returns: temperature """ i2c.writeto(0x40,b'\xf3') # Write byte "0xf3" to address 0x40 (SHT20) sleep_ms(70) # SHT20 measurement takes time, must wait t=i2c.readfrom(0x40, 2) # Read 2 bytes from address 0x40 (SHT20) return -46.86+175.72*(t[0]*256+t[1])/65535 # Temperature conversion: T=-46.86+175.72*St/2^16 def sht20_humidity(): """Get the humidity value from the SHT20 module Returns: humidity """ i2c.writeto(0x40,b'\xf5') # Write byte "0xf5" to address 0x40 (SHT20) sleep_ms(25) # SHT20 measurement takes time, must wait t=i2c.readfrom(0x40, 2) # Read 2 bytes from address 0x40 (SHT20) return -6+125*(t[0]*256+t[1])/65535 # Humidity conversion: RH=-6+125*Srh/2^16 while True: temper=sht20_temperature() humid=sht20_humidity() print("sht20 temperature: %0.1fC sht20 humidity: %0.1f%%" %(temper,humid)) print("Temp:%0.1f C, Humi:%d%%" %(temper,humid)) sleep(1)