PlayingWithFusion.com Home


0 Items
View shopping cart itemsView Cart

Document: 1213
Posted: March 5, 2025
Modified: March 5, 2025

Home > Tech Article Categories > Interface and Communication > Controller/Target I2C Communication with R3aktor

Controller/Target I2C Communication with R3aktor

Communicate between two R3aktor devices using I2C. This article demonstrates use of the Arduino Wire library where one R3aktor is configured as the controller and the other as the target.

Introduction

I2C is a digital protocol commonly used to communicate between a host/controller and other ICs - generally sensors or memory.  The controller generally initiates communication and the target/slave device responds.  The SAMD21 micro used by R3aktor can act as either the controller or the target.

In this example, one R3aktor acts as the target and responds to read requests from a second R3aktor, the controller.

Parts List

For this project, you will need:

  • (x2) R3aktor M0 Logger Board
  • (x2) USB-C cables
  • (x1) Solderless breadboard
  • (x4) 10kΩ resistors
  • (x1) Female to female jumper cable
  • (x2) Male to male jumper cables
  • (x6) Male to female jumper cables

Hardware Setup

Create the circuit. Connect the GND pins on the R3aktor boards together. On one R3aktor board, connect the 3V3 pin to the red (+) rail on the breadboard. Connect both the SDA and SCL pins to different rows on the breadboard. Connect a resistor from the SDA row to the red (+) rail. Do the same for the SCL row. Repeat these steps for the other R3aktor board, but on the opposite side of the breadboard. Connect each R3aktor’s SDA and SCL rows together. The finished circuit should look like the diagram below.  Note that this example includes two resistors on the SDA and SCL lines, but generally only one is recommended for each line.

 

Open the Arduino IDE. For this project, you will need to utilize the Arduino Wire library which comes pre-installed with the IDE, so there is no library installation you need to do.  The Wire library is the primary library used to interact with the I2C peripheral on most Arduino devices.  Create two new sketches. One will be for the Controller code and the other will be for the Target code.

Controller Software

In the controller sketch, paste this code:

  1. #include <Wire.h>
  2.  
  3. int value = 0;
  4.  
  5. void setup() {
  6.   Wire.begin();
  7.   Serial.begin(9600);
  8. }
  9.  
  10. void loop() {
  11.  
  12.   Wire.beginTransmission(9);
  13.   Wire.write(value);
  14.   Wire.endTransmission();
  15.   Serial.println("Value written");
  16.   value++;
  17.   if (value > 2) value = 0;
  18.   delay(5000);
  19. }

 

Target Software

In the sketch for the target device, paste this:

  1. #include <Wire.h>
  2.  
  3. int value = 0;
  4.  
  5. void setup() {
  6.   pinMode(LED_BUILTIN, OUTPUT);
  7.   Wire.begin(9);
  8.   Wire.onReceive(receiveEvent); // method to run when an event is received
  9.   Serial.begin(9600);
  10.  
  11. }
  12.  
  13. void receiveEvent(int bytes) {
  14.   value = Wire.read();
  15.   Serial.println("event received");
  16. }
  17.  
  18. void loop() {
  19.   if (value == 0) {
  20.     digitalWrite(LED_BUILTIN, HIGH);
  21.     delay(200);
  22.     digitalWrite(LED_BUILTIN, LOW);
  23.     delay(200);
  24.   }
  25.   if (value == 1) {
  26.     digitalWrite(LED_BUILTIN, HIGH);
  27.     delay(500);
  28.     digitalWrite(LED_BUILTIN, LOW);
  29.     delay(500);
  30.   }
  31.   if (value == 2) {
  32.     digitalWrite(LED_BUILTIN, HIGH);
  33.     delay(1000);
  34.     digitalWrite(LED_BUILTIN, LOW);
  35.     delay(1000);
  36.   }
  37. }

Deploy Software to Both Devices

Connect both R3aktor boards to your computer via the USB-C cables. Designate one board as the Controller board. The other board will be the Target.

Upload the Controller code to the Controller R3aktor board. Upload the Target code to the Target board. If uploading to both boards from the same computer, ensure you have selected the correct COM port for the R3aktor board you wish to upload to. Once the code is uploaded, you will be able to see the target R3aktor’s built-in LED flash at a decreasing frequency every five seconds. This is because the slave board is receiving a value generated by the controller and sent via the I2Cprotocol.

Related Products