Post topics, source code that relate to the Arduino Platform

User avatar
By Jones20
#86353 I am using this code to scan two no. of i2c bus - one is default (4,5) and other is on Tx & Rx pin(1,3) by enabling them through FUNCTION_3. Please see the code:

Code: Select all#include <Wire.h>
#include <SoftwareSerial.h>
#include <SSD1306Wire.h>
TwoWire Wire1;
SoftwareSerial swSer(14, 12, false, 128);
SSD1306Wire *display;
void setup(void) {
  swSer.begin(115200);

  pinMode(1, FUNCTION_3);
  pinMode(3, FUNCTION_3);
  delay(5);
  Wire.begin();
  display = new SSD1306Wire(0x3c , 1, 3);
 Wire1.begin(4,5);
     
}

void loop()
{  swSer.println("I2C scanner. Scanning bus 0...");
    for (byte i = 8; i < 127; i++)
        {
            Wire.beginTransmission(i);
            if (Wire.endTransmission() == 0)
            {
                  swSer.println("Found address: " + String(i));
                // check address
                if ((i == 32) or (i == 39))
                {
                    // found device, do something..
                }

                delay(1);
            }
        }
        Wire.endTransmission();

      swSer.println("I2C scanner. Scanning bus 1...");
        for (byte i = 8; i < 127; i++)
        {
            Wire1.beginTransmission(i);
            if (Wire1.endTransmission() == 0)
            {
                  swSer.println("Found address: " + String(i));
                // check address
                if (i == 64)
                {
                    // found device, do something..
                }

                delay(1);
            }
        }
        Wire1.endTransmission();

          swSer.println("I2C device check DONE.");
          delay(5000);
}



But output on software serial port is:

05:21:36.888 -> I2C scanner. Scanning bus 0...
05:21:36.888 -> Found address: 57
05:21:36.943 -> Found address: 58
05:21:36.943 -> I2C scanner. Scanning bus 1...
05:21:36.943 -> Found address: 57
05:21:36.943 -> Found address: 58
05:21:36.943 -> I2C device check DONE.
05:21:41.939 -> I2C scanner. Scanning bus 0...
05:21:41.939 -> Found address: 57
05:21:41.975 -> Found address: 58
05:21:41.975 -> I2C scanner. Scanning bus 1...
05:21:41.975 -> Found address: 57
05:21:41.975 -> Found address: 58
05:21:42.021 -> I2C device check DONE.

It is unable to detect devices on i2c bus enabled on pin (1,3) i.e. Tx & Rx. Please find the mistake in my i2c scan code.
User avatar
By Pablo2048
#86383 It will work - please see wiki/doku.php?id=esp8266_gpio_pin_allocations#pin_functions - I'm using this when I need RX pin as another I2C pin like this:
Code: Select allstatic void checkSet13(uint8_t gpio)
{

    if (1 == gpio) {
        pinMode(1, FUNCTION_3); // TX->GPIO
    } else if (3 == gpio) {
        pinMode(3, FUNCTION_3); // RX->GPIO
    }
}