XIAO SAMD21 I2C master/slave

Hello

I'm trying to connect two seeed XIAO SAMD21 with I2C. One would be the master and the other the slave.

I came across this github with Wire library examples on how to do it for the SAMD21 CPU's:

I programmed one XIAO with master code:

// Wire Master Writer
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Writes data to an I2C/TWI slave device
// Refer to the "Wire Slave Receiver" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup()
{
  Wire.begin(); // join i2c bus (address optional for master)
}

byte x = 0;

void loop()
{
  Wire.beginTransmission(4); // transmit to device #4
  Wire.write("x is ");        // sends five bytes
  Wire.write(x);              // sends one byte  
  Wire.endTransmission();    // stop transmitting

  x++;
  delay(500);
}

And one as slave:

// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup()
{
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
}

void loop()
{
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  while(1 < Wire.available()) // loop through all but the last
  {
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
}

The slave should receive data from master and print on the serial one character, but nothing happens, Would you be able to help me pinpoint where the error might be?

This is my wiring

EDIT: I added pullup resistors to the SDA/SCL

Have you accidentally confused I2c with serial?

Why do you think that slave should print one symbol if you send a whole line?

On the master, the

should return a status of data transfer. The good idea is to test the status after sending.

Have you installed the Seeed-Studio /ArduinoCore-samd

Hello Jim

How do I install the support package? I downloaded everything as .zip, tried to install as library but got an error

Hi

No, I'm working with I2C, not serial.

In this case we can ignore the "x is" line sent, as the value of X is the one that is read by the slave, as in the code:

while(1 < Wire.available()) // loop through all but the last
  {
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }

Do you have any code suggestions on how to do it? I haven't worked with I2C in a while and got a bit rusty on it.

Where do you see that you ignored the "x is" line sent??? According to the code above the line will be received and printed.
Don't you understand even the line in that code?

For what i understood the message is sent but the slave loops through the data received and only prints the last character, if I misingterpreting please let me know.

Just an FYI, this is not my code, I got it from here:

From my understanding the slave should print the value of x sent by the master over the serial, but when I've tried it doesn't print anything

Not surprising. Board package is not a library.

Open this link and scroll to the section "Installing the SAMD core"

I see, I followed instructions and I've it already installed.

It better to use the board manager

Follow these instructions:

Add this URL:
https://files.seeedstudio.com/arduino/package_seeeduino_boards_index.json

Nothing will work until you install the correct board package.

Do you see a Serial.print statement inside a while loop?

Why do you think that the line will not be printed?

It is a bad idea to use the code if you don't understand it.

That is not for your board.
See post #11

Hello Jim

Thank you for the link!

I checked and I already have the url from before included as shown in the instructions you sent me. I'm a bit out of ideas.

So which board have you selected from board manager?

Hi

This is what I have selected

Like this?
One 10K pull-up from SCL to 3.3V
One 10K pull-up from SDA to 3.3V

That is the correct board selected.

Yes like that :slight_smile:

All looks good.
Let's see if the master can find the slave.
Download this scanner sketch to the master.
Make sure the slave is running before you start the master.
The master should find slave a address 4

#include <Wire.h>

void setup() {
  Wire.begin();

  Serial.begin(9600);
  Serial.println("\nI2C Scanner");
}

void loop() {
  int nDevices = 0;

  Serial.println("Scanning...");

  for (byte address = 1; address < 127; ++address) {
    // The i2c_scanner uses the return value of
    // the Wire.endTransmission to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    byte error = Wire.endTransmission();

    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address < 16) {
        Serial.print("0");
      }
      Serial.print(address, HEX);
      Serial.println("  !");

      ++nDevices;
    } else if (error == 4) {
      Serial.print("Unknown error at address 0x");
      if (address < 16) {
        Serial.print("0");
      }
      Serial.println(address, HEX);
    }
  }
  if (nDevices == 0) {
    Serial.println("No I2C devices found\n");
  } else {
    Serial.println("done\n");
  }
  delay(5000); // Wait 5 seconds for next scan
}

Hi Jim

Thank you so much for your support and the scanner. It does find the slave on address 4.

I rechecked my wiring and the gnd to gnd jumper wire was broken right on the pin entering the breadboard, :persevere: that's where the problem was!!! :scream_cat:

Thank you so much for the support and the scanner code, I've used I2C in the past and the scanner would have been very helpful.