Author Topic: Swap I2C SDA and SCL Pins On The Fly?  (Read 306 times)

biomurph

  • RFduino Newbie
  • *
  • Posts: 15
  • Karma: +0/-0
    • View Profile
Swap I2C SDA and SCL Pins On The Fly?
« on: May 09, 2017, 01:32:25 PM »
That's the question... It looks like it's possible by simply doing
Code: [Select]
Wire.beginOnPins(sclPin1, sdaPin1);and then
Code: [Select]
Wire.beginOnPins(sclPin2, sdaPin2);at some other part of the code.

I have an I2C bus that will be primary, so the secondary will switch to it's bus and then back to the primary when it's done.

is this possible? I will try, and let you know.

tolson

  • Global Moderator
  • *****
  • Posts: 844
  • Karma: +19/-0
    • View Profile
    • Thomas Olson Consulting
Re: Swap I2C SDA and SCL Pins On The Fly?
« Reply #1 on: May 09, 2017, 09:48:38 PM »
That's the question... It looks like it's possible by simply doing
Code: [Select]
Wire.beginOnPins(sclPin1, sdaPin1);and then
Code: [Select]
Wire.beginOnPins(sclPin2, sdaPin2);at some other part of the code.

I have an I2C bus that will be primary, so the secondary will switch to it's bus and then back to the primary when it's done.

is this possible? I will try, and let you know.

Hi biomurph,

You should probably do a Wire.end() before redefining the pins.
BUT, if you are using SImblee there are two hardware I2C busses, so you could do both at the same time.
It is not documented in any company documents. But it is in the source code.

So you could do.
Wire.begin();  //for default pins for I2C1 5,6
Wire2.begin();  //for default pins for I2C2  13,14

or do define customs for both...
Wire.beginOnPins(sclPin1,sdaPin1);
Wire2.beginOnPins(sclPin2,sdaPin2);

Here is my modification to i2c_scanner_dual...

Code: [Select]
// --------------------------------------
// i2c_scanner_dual
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//
// Version ThomasOlson teo20140527.01 works with RFduino with mods.
//
// Version ThomasOlson teo20160612.01 Modified to test both I2C
// busses on 3rd generation nRF51822 as in Simblee

#include <Wire.h>


void setup()
{

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

  Wire.begin(); // default SCL = 5, SDA = 6
  //Wire.beginOnPins(5,6);
  Wire2.begin(); // default SCL2 = 13, SDA = 14
  Wire2.beginOnPins(2,3);
 
}


void loop()
{
  byte error, address;
  int nDevices;

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

  nDevices = 0;

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

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

      nDevices++;
    }else{
//      Serial.print("Error ");
//      Serial.print(error);
//      Serial.print(" at address 0x");
//      if (address<16)
//        Serial.print("0");
//      Serial.println(address,HEX);
    }   
  }
 
  if (nDevices == 0){
    Serial.println("No I2C devices found on bus 1\n");
  }else{
    Serial.print(nDevices);
    Serial.println(" found\n");
  }



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

  nDevices = 0;

  for(address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire2.beginTransmission(address);
    Wire2.write(0x00);
    error = Wire2.endTransmission();

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

      nDevices++;
    }else{
//      Serial.print("Error ");
//      Serial.print(error);
//      Serial.print(" at address 0x");
//      if (address<16)
//        Serial.print("0");
//      Serial.println(address,HEX);
    }   
  }
 
  if (nDevices == 0){
    Serial.println("No I2C devices found on bus 2\n");
  }else{
    Serial.print(nDevices);
    Serial.println(" found\n");
  }

  delay(5000);           // wait 5 seconds for next scan
}
« Last Edit: May 09, 2017, 09:51:29 PM by tolson »

biomurph

  • RFduino Newbie
  • *
  • Posts: 15
  • Karma: +0/-0
    • View Profile
Re: Swap I2C SDA and SCL Pins On The Fly?
« Reply #2 on: May 10, 2017, 07:33:18 AM »
Hi tolson,

You got a secret squirrel I2C bus on the Simblee!? Hey that's great! Totally solves my problemo!

I was poking around in the board files yesterday and did see the Wire.end() and thought that would likely have to be employed for my original idea.

All good info to know.

Cheers!

biomurph

  • RFduino Newbie
  • *
  • Posts: 15
  • Karma: +0/-0
    • View Profile
Re: Swap I2C SDA and SCL Pins On The Fly?
« Reply #3 on: May 13, 2017, 10:52:12 AM »
Just hitting this to say that Wire2 works like a charm!