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 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.
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.
SHT20 temperature conversion formula
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 I2C class chapter.
Complete SHT20 example:
1from mpython import * # Import all mpython objects
2
3def sht20_temperature():
4 """Get the temperature value from the SHT20 module
5 Returns: temperature
6 """
7 i2c.writeto(0x40,b'\xf3') # Write byte "0xf3" to address 0x40 (SHT20)
8 sleep_ms(70) # SHT20 measurement takes time, must wait
9 t=i2c.readfrom(0x40, 2) # Read 2 bytes from address 0x40 (SHT20)
10 return -46.86+175.72*(t[0]*256+t[1])/65535 # Temperature conversion: T=-46.86+175.72*St/2^16
11
12def sht20_humidity():
13 """Get the humidity value from the SHT20 module
14 Returns: humidity
15 """
16 i2c.writeto(0x40,b'\xf5') # Write byte "0xf5" to address 0x40 (SHT20)
17 sleep_ms(25) # SHT20 measurement takes time, must wait
18 t=i2c.readfrom(0x40, 2) # Read 2 bytes from address 0x40 (SHT20)
19 return -6+125*(t[0]*256+t[1])/65535 # Humidity conversion: RH=-6+125*Srh/2^16
20
21while True:
22 temper=sht20_temperature()
23 humid=sht20_humidity()
24 print("sht20 temperature: %0.1fC sht20 humidity: %0.1f%%" %(temper,humid))
25 print("Temp:%0.1f C, Humi:%d%%" %(temper,humid))
26 sleep(1)