Change I2C SDA and SCL pin of Arduino and Trinket M0

Hey I am Working on project where I need two I2C pins or atleaste different SDA pins. I was looking at the source of Multi_BitBang, but I couldn't understand how to a GPIO pin get assigned as SDA and SCL. I am trying to change library source so that I can use GPIO pins as SDA or SCL. Can anyone guide me how I can do that.

Thanks!!

What Arduino board?

The hardware I2C pins are set on many boards and cannot be changed. You can install a library to have software I2C on those boards.

Why do you need another I2C bus? You are aware that many devices can share the I2C bus (if the board has native I2C) as long as each has a unique address, right?

Using the Multi_BitBang library the pins used for SDA and SCL are set in these 2 arrays (see the example).

uint8_t scl_list[NUM_BUSES] = {0xb5,0xb5,0xb5,0xb5}; //{9,9,9,9};
uint8_t sda_list[NUM_BUSES] = {0xc6, 0xd7, 0xe6, 0xb4}; //{5,6,7,8};

Hey!!
Thanks for the prompt reply.
Yes, I am aware of I2C protocol, and I know I can have many devices on the bus. To your question why I need different pins is, in my application I have one sensor that is communicating to Trinket M0 over I2C and the same Trinket M0 gets some reading value from Arduino Uno. There will be five such Trinket M0 board getting data from the Arduino Uno. So, when one Trinket M0 communicates with sensor, the other trinket boards cannot communicate with the sensor to their peripheral and I don't want this to happen. That's the reason I need separate SDA and SCL for Arduino to trinket M0.

I did see the example, I believe in that example all lines started simulteneously without address. I need address for Trinket M0. Please correct me if I am wrong in any way.

Hi, @ak474849
Welcome to the froum.

Are you aware that I2C consists of basically a master controller and slaves?

What ever controller you use as the master, even with more than one I2C bus, it can only communicate with one I2C bus at a time.

Having one I2C bus will not have crashes as the master will be addressing each slave on the I2C bus in turn.
If the Trinkets all get readings from the same Uno, then you will have to make the UNO the master and each Trinket the slave.

What is your application, what data is being collected by the UNO and disseminated to each Trinket?
How long is the I2C link (cm)?
What does each Trinket do with the data it receives?
Please read the post at the start of any forum , entitled "How to use this Forum".

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

So the Trinkets need to be I2C masters for their attached sensors and slaves for the Uno?

Draw, photograph and post a schematic diagram, please.

Hi,
Why are you using Trinkets, what other task, I/O are they doing?
Have you looked at a I2C Multiplexer?
Google;

i2c multiplexer arduino

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

Hey Tom!

Thanks for the response and the information about the Forum Guidelines.

I know that I2C is master controlling the slave and I was shocked as well when I saw my setup working.
As I explained I have a sensor that can communicate over I2C, a Trinket M0 board and Arduino Mega. So, what I did is I used Wire.h library and Initiated Trinket as a master which will take data from sensor. Although I not reading sensor values yet. Then in function I called Wire.begin(address) again and received value from the Arduino (Master). Then I started the reading values from sensor over Trinket M0. This system just works fine for one Trinket M0 board, but I need multiple Trinket boards. I will attach my code and schematic, please let me know if I am doing anything wrong.
So to avaoid this problem I think using another pins


on Trinket for I2C will help to solve my problem.
sketch_feb10a.ino (1.3 KB)

Uploaded the Schematic as well as code please check.

Hi,
Why do you need a Trinket to control each motor?
Can't the Mega do all that, it has enough I/O.
To add code please click this link;

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

Why do you need a Trinket to control each motor?
For my application the sensor, motor and Trinket is one system and therefore Motor is controlled by Trinket. Also, problem is not with the motor is with the I2C.

I really appreciate your patient response.

code:

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

// Demonstrates use of the Wire library
// Receives data as an I2C/TWI Peripheral 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>


int destination_val;  //Global Variables
byte dataFlag = 0;   //ack to master

void setup()
{
  Wire.begin();                // join i2c bus with address for bendlabs
  Serial.begin(115200);           // start serial for output

}

void loop()
{
  //Serial.println("ackTomaster");
  if (dataFlag == 0)
    getDataFromMaster();
  Wire.begin();
  while (dataFlag == 1)
  {

    // Reading sensor value.
    //}
  }
}

void getDataFromMaster()
{
  Wire.begin(0xA); //begin as slave with address 10
  delay(4000);
  Serial.println("Waitintg for data from master...");
  while (Wire.available())
    ;
  while (Wire.available())
  {
    Serial.println("Receiving Data...");
    for (int i = 0; i < 2; i++)
    {
      destination_val[i] = Wire.read();
      Serial.println(destination_val[i]);
    }

  }
  Serial.println("Data is received!!!");
  for (int i = 0; i < 2; i++)
  {
    Serial.print(destination_val);
    Serial.print(", ");
  }
  dataFlag = 1;
}

How far apart are the sensors from the Trinkets? How far are the Trinkets from the Uno?

Hi,
Sorry, but why must the Trinket and the sensor be connected?
If this is an object avoidance robot, then the Mega can do all you need.
Three controllers is a bit extreme, where one will do.

What are your motor drivers and what are your sensors?
What function does the Mega have?
What function do the Trinkets have?
Why can't one controller with one I2C bus do it all?

Looking at your existing diagram, have you got different addresses for each of the untis on the I2C bus?

Tom... :grinning: :+1: :coffee: :australia:
PS. I'm off to work, I'll be back later today, Friday.

I am sorry but I can't fully disclose my project here. The information I gave you is necessary as I mentioned. I can't remove any sensor or use one than multiple.

I just wondering if I can somehow set GPIO pin as SDA and SCL on Trinket M0.

I sincerely thank you for the patient reply.

They are close to the sensor. However, Arduino will be 10-20 cm apart. But distance will be reduced it that is the problem. Also, previously you mentioned about sofware I2C, can also get more information on that.

Thank you so much for your response and time.

That was before I knew that the I2C was on a Trinket. I do not know anything of the Trinket.

The SAMD21 processor on the Trinket M0 has 6 SERCOM modules. Each of them can be configured for a form of serial communication (UART, SPI, I2C, ...). Time to consult the Trinket's schematic and the SAMD21 datasheet :wink: Verify that at least 2 SERCOMs are broken out (not sure how that works).

I have no experience with the Trinket M0 or the SAMD21.

Hi @ak474849

As @sterretje mentions, it's possible to create additional hardware I2C ports on the SAMD21.

Here's an excellent tutorial by Adafruit:

1 Like

I knew there was a tutorial somewhere; just could not find it :frowning: Thanks @MartinL .

@TomGeorge @MartinL @groundFungus Thank you for the reply

@MartinL I will go through it Thanks!!