I2C - Send and Receive Data

Materials

  • AmebaD [ AMB23 / AMB21 / AMB22 / BW16 / BW16-TypeC ] x 1
  • Arduino UNO x 1

Example

I2C is a quite common module on microcontrollers, it only takes 2 wire and able to achieve data rate at up to 3.4Mbps. It works in master-slave model and a master can simultaneously connect to up to 128 slaves, making it a very versatile communication protocol between microcontroller and sensor.
Here we are going to use Ameba as an I2C master and Arduino UNO as a slave to achieve I2C send and receive.

For Sending Data
Before connection, make sure to upload the “Examples -> Wire -> Slave_receiver” example code to Arduino UNO.

For Receiving Data
Before connection, make sure to upload the “Examples -> Wire -> Slave_sender” example code to Arduino UNO.

AMB21/AMB22
Unit sda scl
0
PA_26
PA_25
AMB23
Unit sda scl
0
PB_0
PA_31
BW16/BW16-TypeC
Unit sda scl
0
PA_26
PA_25

Code

Then copy and paste the following code line by line into REPL to see their effects.

from machine import Pin, I2C
i2c = I2C(scl = "PA_25", sda = "PA_26", freq=100000) # configure I2C with pins and freq of 100KHz
i2c.writeto(8, bytes([123])) # send 1 byte to slave with address 8 with integer '123'

#After uploading Slave_sender example to Arduino UNO, copy this code to the
i2c.readfrom(8, 6) # Receive 6 bytes from slave
Please confirm that QQ communication software is installed