docs

a slatepencil documentail site

View on GitHub

Inter-Integrated Circuit(I2C)

i2c

The Inter-Integrated Circuit (I2C) Protocol is a protocol intended to allow multiple “peripheral” digital integrated circuits (“chips”) to communicate with one or more “controller” chips. Like the Serial Peripheral Interface (SPI), it is only intended for short distance communications within a single device. Like Asynchronous Serial Interfaces (such as RS-232 or UARTs), it only requires two signal wires to exchange information.

i2c

from mpython import *
import time

def temperature():
    i2c.writeto(0x40, b'\xf3')          # request temperature data
    time.sleep_ms(70)
    t = i2c.readfrom(0x40, 2)           # read 2 bytes temp data
    return -46.86 + 175.72 * (t[0] * 256 + t[1])/65535

def humidity():
    i2c.writeto(0x40, b'\xf5', False)   # request humidity data
    time.sleep_ms(25)
    t = i2c.readfrom(0x40, 2)
    return -6 + 125 * (t[0]*256 + t[1]) / 65535

while True:
    oled.fill(0)                        # clear oled screen
    oled.DisplayChar((str('Temp: ') + str(round(temperature(), 2))), 0, 16)
    oled.DsiplayChar((str('Humi: ') + str(round(humidity(), 2))), 0, 32)
    oled.show()
    time.sleep_ms(1000)