I2C - Slave Send Data to Arduino UNO

Materials

  • AmebaD [AMB21 / AMB22 / AMB23 / AMB25 / AMB26 / BW16 / AW-CU488 Thing Plus] x 1

  • Arduino UNO x 1

Example

Introduction

There are two roles in the operation of I2C, one is “master”, the other is “slave”. Only one master is allowed and can be connected to many slaves. Each slave has its unique address, which is used in the communication between master and the slave. I2C uses two pins, one is for data transmission (SDA), the other is for the clock (SCL). Master uses the SCL to inform slave of the upcoming data transmission, and the data is transmitted through SDA. The I2C example was named “Wire” in the Arduino example.

Procedure

In this example, we use Ameba as the I2C slave writer, and Arduino as the I2C master receiver. When the I2C slave receives a string sent from I2C master, it prints the received string.

Setting up Arduino Uno to be I2C Master

First, select Arduino in the Arduino IDE in “Tools” → “Board” → “Arduino Uno” Open the “Master Reader” example in “Examples” → “Wire” → “master_reader”:

image01

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

Setting up Ameba to be I2C Slave

Next, open another window of Arduino IDE, making sure to choose your Ameba development board in the IDE: “Tools” → “Board”

Then open the “Master Writer” example in “File” → “Examples” → “AmebaWire” → “SlaveWriter”

image02

Wiring

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

AMB23 Wiring Diagram:

image04

Open the Arduino IDE of the Arduino Uno and open the serial monitor (“Tools” → “Serial Monitor”). Next, press the reset button on Arduino Uno. Now the Arduino Uno (Master) is waiting for the connection from Ameba (Slave). In the Serial Monitor, you can see the messages printed from Arduino Uno. We press the reset button on Ameba to start transmitting messages to Arduino UNO. Then observe the serial monitor, you can see the messages show up every 0.5 second.

image10

Code Reference

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

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.beginTransmission(address) to begin a transmission to the I2C slave with the given address: https://www.arduino.cc/en/Reference/WireBeginTransmission

Uses Wire.write() to send data, and finally use Wire.endTransmission() to end a transmission to a Slave and transmits the bytes that were queued: https://www.arduino.cc/en/Reference/WireEndTransmission