I2C - 從 Arduino UNO 接收資料

材料準備

  • AmebaD [AMB21 / AMB22 / AMB23 / BW16] x 1

  • Arduino UNO x 1

範例説明

In the previous example “I2C – Communicate with Arduino UNO via I2C” , Ameba, the I2C master, transmits data to the Arduino UNO, the I2C slave. As to this example, Ameba is the I2C master, and receives data from the Arduino UNO, which is the I2C slave.

準備Arduino Uno為 I2C Slave

First, select Arduino in the Arduino IDE in “Tools” “Board” “Arduino Uno”: Open “Examples” “Wire” “slave_sender”

../../../../_images/image155.png

Then click “Sketch” → “Upload” to compile and upload the example to Arduino Uno.

準備Ameba 為 I2C Master

Next, open another window of Arduino IDE, make sure to choose your Ameba development board in the IDE: “Tools” → “Board” Open “File” “Examples” “AmebaWire” “MasterReader”

../../../../_images/image250.png

Click “Sketch” → “Upload” to compile and upload the example to Ameba.

接線

The Arduino example uses A4 as the I2C SDA and A5 as the I2C SCL. Another important thing is that the GND pins of Arduino and Ameba should be connected to each other.

AMB21 / AMB22 的接线图:

../../../../_images/image333.png

AMB23 的接线图:

../../../../_images/image3-14.png

BW16 的接线图:

../../../../_images/image3-34.png

BW16-TypeC Wiring Diagram:

../../../../_images/image3-41.png

Next, we will observe the data receive by Ameba in the Serial Monitor. (Note: If you do not know which port the Ameba development board is connected to, please find it in the Device Manager of Windows first. Ameba is connected as “mbed Serial Port”. For example, if you find mbed Serial Port (COM15) means Ameba is connected to port COM15.)

../../../../_images/image425.png

We select the port in “Tools” → “Port” → “COM15” (the port connected to Ameba) Open the Arduino IDE window of the Ameba, go to “Tools” → “Serial Monitor” to display the messages printed by Ameba. Press the reset button on Arduino Uno, Arduino Uno now waits for connection from I2C master. Then press the reset button on Ameba, Ameba will start to receive messages from Arduino Uno. And you can see the “hello ” message printed every half second in serial monitor. (NOTE: If the message does not show in the Serial Monitor of Ameba, please close and open the serial monitor again.)

../../../../_images/image518.png

程式碼説明

You can find detailed information of this example in the documentation of Arduino: https://www.arduino.cc/en/Tutorial/MasterReader

First use Wire.begin() / Wire.begin(address) to join the I2C bus as a master or slave, in the Master case the address is not required. https://www.arduino.cc/en/Reference/WireBegin

Next, the Master uses Wire.requestFrom() to specify from which Slave to request data. https://www.arduino.cc/en/Reference/WireRequestFrom