-->
Page 1 of 1

I2C scan failed for i2c us on Tx & Rx pin -ESP8266(Node MCU)

PostPosted: Sat Apr 04, 2020 6:54 pm
by Jones20
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.

Re: I2C scan failed for i2c us on Tx & Rx pin -ESP8266(Node

PostPosted: Mon Apr 06, 2020 3:21 pm
by schufti
Hi,
where do you get the impression that
pinMode(1, FUNCTION_3);
pinMode(3, FUNCTION_3);
will work?

try using
Serial.swap()
instead and ditch softserial

Re: I2C scan failed for i2c us on Tx & Rx pin -ESP8266(Node

PostPosted: Tue Apr 07, 2020 12:40 am
by Pablo2048
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
    }
}

Re: I2C scan failed for i2c us on Tx & Rx pin -ESP8266(Node

PostPosted: Tue Apr 07, 2020 6:53 am
by schufti
ok, I see. would be nice to also find it in the "official" documentation...
https://arduino-esp8266.readthedocs.io/ ... digital-io