• sales

    +86-0755-88291180

【MicroPython】machine.I2C Function

  • machine.I2C(i2c_id,scl,sda,freq=400000)
    • The I2C object constructor is used to initialize the corresponding channel and pin.
    • i2c_id: Use the I2C channel, which can be 0 or 1;
    • scl: SCL pin, should be a Pin object (I2C0 defaults to 9, I2C1 defaults to 7)
    • sda: SDA pin, should be a Pin object (I2C0 defaults to 8, I2C1 defaults to 6)
    • freq: I2C clock frequency, the default is 400Kb/S
  • I2C.scan()
    • The scan slave function returns a list of all 7-bit addresses of slave devices mounted on the I2C bus.
  • I2C.readfrom(addr, nbytes, stop=True)
    • The function of readfrom is to read data from the device through the I2C bus and return a byte string.
    • addr: slave device address.
    • nbytes: read character length.
    • stop: Whether to send the end signal after receiving the completion data.
  • I2C.readfrom_into(addr, buf, stop=True)
    • The readfrom_into function is an upgraded version of the readfro function, which can specify the read data to be stored in a character array.
    • addr: slave device address
    • buf: character array, used to store data
    • stop: whether to send the end signal after receiving the completion data
  • I2C.writeto(addr, buf, stop=True)
    • The writeto function is used to write data to the slave device.
    • addr: slave device address
    • buf: send string
    • stop: whether to send the end signal after receiving the completion data

Memory Operation

  • I2C.readfrom_mem(addr, memaddr, nbytes, *, addrsize=8)
    • The readfrom_mem function is used to read data from the register of the device.
    • addr: slave device address
    • memaddr: register address
    • nbytes: read length in bytes
    • addrsize: register address length
  • I2C.readfrom_mem_into(addr, memaddr, buf, *, addrsize=8)
    • The readfrom_mem_into function is used to read data from the register of the device to the specified character array.
    • addr: slave device address
    • memaddr: register address
    • buf: character array, used to store data
    • addrsize: register address length
  • I2C.writeto_mem(addr, memaddr, buf, *, addrsize=8)
    • The writeto_mem function is used to write data to the register of the slave device
    • addr: slave device address
    • memaddr: register address
    • buf: send string
    • addrsize: register address length

Software I2C

  • I2C.start()
    • The start function is used to send a start signal in the I2C bus (SCL remains high, SDA turns low)
  • I2C.stop()
    • The start function is used to send a termination signal in the I2C bus (SCL remains high, SDA turns low)

I2C.readinto(buf, nack=True,)

    • The function of readinto is to read data from the I2C bus to the specified character array, and the read length is the length of the character array.
    • buf: character array to store data
    • nack: whether to send a nack signal after receiving the last data
    • I2C.write(buf)
    • The function of the write function is to write the data of the buf character array to the I2C bus.
    • buf: array of characters to send

Demo Address

This article is only for RP2040 MicroPython firmware, and the source code shall prevail. This article is written according to the official source code at the time of writing, which is used to provide convenience for beginners and is for reference only. Those who are capable are recommended to refer to MicroPython.

TAG: Raspberry Pi 4B/5 Industrial UART 2CH RS485 RS232 CAN FD HAT With Case For Installation of guide rails and hanging Raspberry Pi 5 Case MPUUART Raspberry Pi 5 PCIe to ISO 2-CH RS485 /2-CH RS232 / USB /UART / TTL Raspberry Pi USB Global Shutter Camera 1MP OV9281 120fps Black/White 120fps also For Jeston RDK Raspberry Pi Monitor Official Original DisplayScreen 15.6 inch LCD Full HD 1920x1080 15.6inch Horizontal Drag Instructions ESP32-S3 Industrial 8-Channel Controller with WiFi Bluetooth CAN IoT RS485 and RJ45 ETH POE Retail electronic Labe Milk-V Duo S 512MB SG2000 RISC-V Spotpear 5inch USB Monitor Luckfox Pico Argon Neo 5 Raspberry Pi Pico 2 P2350-Plus Smart Car Kit PicoGo2 Autonomous Driving Learning Car ESP32-P4-ETH RJ45 POE DSI/MIPI 10.1 inch LCD Display Touchscreen/CSI Camera/Audio Speaker AI Deepseek Intel BE200 Wireless WLAN Card Gigabit Tri-Band WiFi7 Bluetooth 5.4 M.2 MU-MIMO IEEE 802.11 DC-DC Step-Down Mini voltage Moudle Input 5~36V voltage Output 5V/3.3V 4A ESP32 ST7789 7.5 inch Passive NFC e-Paper V2 BW Display 7.5inch E-ink Screen No Need Battery Wireless Powered Data Transfer For Mobile APP Milk-V Duo S User Guide Total