NodeMCU (ESP8266) (CP2102) more SCL/SDA

Hello people,
i do one project with ESP8266 (CP2102) and want to connect Ultrasound HC-SR04 and LCD 16x2 I2C.
I already connect my ultrasound and work just fine. Did my sketch and all good. Now I start to dig how to connect my LCD , but found that have to use some SCL/SDA pins on my ESP8226 (D1 & D2).
So my question is - can I use some other pins and which one?


I don't see the problem. So you use D1 and D2 for I2C. Your HC-SR04 and pushbuttons can connect to any other pins you choose.

Note that the pushbuttons should be connected to ground with pull-ups to 3.3 V, especially if you connect them to GPIO0 and GPIO2 (D3 and D4) - which you probably should. So you can use D5 and D6 for the HC-SR04. :grinning:

There is no problem at the moment.
I want to attach 16x2 LCD I2C and don’t know on which pins can i connect it.

If your I2C library uses D1 and D2, then those are the pins to which you attach the display.

You first alter your code so you use D5 and D6 for the HC-SR04.

Here is my code :

Now I want to add additional 16x2 LCD I2C
If i connect to D5&D6 how the code will know about that ?

If i connect to D5&D6 how the code will know about that ?

OK, read the forum instructions to post the code here, rather than just that website.

Nevertheless your pins for the HC-SR04 are defined at the start of the program as "trigPin" and "echoPin" so it is instantly easy to set them - and the buttonPins - to whatever you choose. Note what I said about wiring the buttons and you will need to exchange HIGH and LOW in the code where you read the buttons.

first of all I want to thank you, that you still take care for my problem.
But let me explain one more time , what is my aim.
I want to connect 2 devices , which are I2C to ESP8266. At the sketch , that I attached there is only one (Ultrasound sensor), but I want to add one more (16x2 LCD).
So in general my question is - how to do that.
Forget for the buttons, they are there just for other reason.

And I posted this link on external site , which is for code sharing, but now I will paste my code here :

const int buttonPin1 = 12;     // the number of the pushbutton pin
const int buttonPin2 = 14;     // the number of the pushbutton pin
const int trigPin = 5;  
const int echoPin = 4;  
char Str4[] = "Boris High";

int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;         // variable for reading the pushbutton status
long duration;
int distance;

void setup() {
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input

void loop() {
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);

  if (buttonState1 == HIGH && buttonState2 == LOW) {
    BorisHight ();
  } else if (buttonState2 == HIGH && buttonState1 == LOW) {
    Serial.println("B1111 OFF && B2222 ON");
    digitalWrite(LED_BUILTIN, HIGH); 

void BorisHight () {
    digitalWrite(trigPin, LOW);

    // Sets the trigPin on HIGH state for 10 micro seconds
    digitalWrite(trigPin, HIGH);
    digitalWrite(trigPin, LOW);

    // Reads the echoPin, returns the sound wave travel time in microseconds
    duration = pulseIn(echoPin, HIGH);

    // Calculating the distance
    distance= duration*0.034/2;
    // Prints the distance on the Serial Monitor
    Serial.print("Distance: ");

i read the datasheet of HC-SR04 and realized , that actually this device is NOT I2C.
So I need to move it to some other digital pins and use SCL/SDA pins of ESP8266 for my LCD.

As I have explained. :cold_sweat:

Note that if it was I2C you would of course, be connecting it to the same two pins as all other I2C devices.

Thank you Paul!

The i2c "standard" pins on the NodeMCU are

Wire.begin(SDA, SCL);            // NODEMCU/ESP default ist D2 = 4 = SDA ; D1 = 5 = SCL

you can use the GPIO naming

Wire.begin(4, 5);

or some other combination as long as it doesn't interfere with other restrictions. Just call the .begin() method with your I2C pins.
A working example:

Wire.begin(D5, D6);

Noiasca - which library have to use , to be able this naming ?

Nothing special. It is the “standard” wire.h from the esp8266 core.

This is my i2-scanner / test sketch which works fine for the Atmels and the NodeMCU

#include <Wire.h>

void setup()
  Serial.println("\nI2C Scanner");

  Serial.println(F("Init I2C for ESP8266_NODEMCU"));
  Wire.begin(SDA, SCL);            // NODEMCU/ESP default ist D2 = 4 = SDA ; D1 = 5 = SCL
  //Wire.begin(4, 5);              // NODEMCU geht
  //Wire.begin(D5, D6);           

  // some microprocessors support higher speeds
  //Wire.setClock(10000);      // low speed mode (default)
  //Wire.setClock(100000);     // standard mode
  //Wire.setClock(400000);     // fastmode
  //Wire.setClock(1000000);    // fast mode plus  // NOK on UNO
  //Wire.setClock(3400000);    // high speed mode


void loop()
  uint8_t error, address;
  uint8_t nDevices;
  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.
    error = Wire.endTransmission();
    if (error == 0)
      Serial.print(F("I2C device found at address 0x"));
      if (address < 16) Serial.print(F("0"));
      Serial.print(address, HEX);
      if (address <= 0x07) Serial.print(F(" - reserved"));
      if (address >= 0x20 && address <= 0x27) Serial.print(F(" MCP23008 MCP23017 PCF8574A")); // PCF8574A variant of some LCD/port expander
      if (address == 0x21 || address == 0x22) Serial.print(F(" SAA4700")); // VPS dataline processor
      if (address >= 0x30 && address <= 0x3F) Serial.print(F(" PCF8574")); // variant of some LCD/port expander
      if (address >= 0x3c && address <= 0x3d) Serial.print(F(" PCF8578")); // Clock/calendar
      if (address == 0x40) Serial.print(F(" SHT21/SI7021"));  // weather
      if (address >= 0x48 && address <= 0x4f) Serial.print(F(" PCF8591")); // A/D-D/A-Wandler
      if (address >= 0x50 && address <= 0x57) Serial.print(F(" AT24C32")); // on some DS3231 breakout boards there is an EEPROM IC at 0x57
      if (address == 0x68) Serial.print(F(" DS3231")); // RTC
      if (address >= 0x68 && address <= 0x6b) Serial.print(F(" PCF8573")); // RTC
      if (address >= 0x70 && address <= 0x77) Serial.print(F(" HT16K33 TCA9548")); // HT16K33 LED Backpack
      if (address >= 0x76 && address <= 0x77) Serial.print(F(" BME280 BME680 BMP280")); // weather

    else if (error == 4)
      Serial.print(F("Unknow error at address 0x"));
      if (address < 16) Serial.print(F("0"));
      Serial.println(address, HEX);
  if (nDevices == 0)
    Serial.println(F("No I2C devices found\n"));
  delay(5000);           // wait 5 seconds befor next scan

you can check the wire.h for its begin() methods:

you’ll find

    void begin(int sda, int scl);
    void begin(int sda, int scl, uint8_t address);
    void pins(int sda, int scl) __attribute__((deprecated)); // use begin(sda, scl) in new code
    void begin();
    void begin(uint8_t);
    void begin(int);

then check the wire.cpp

void TwoWire::begin(void)
    begin(default_sda_pin, default_scl_pin);

and default_sda_pin/default_scl_pin are set at the top of the wire.cpp as

static int default_sda_pin = SDA;
static int default_scl_pin = SCL;

Wire.begin(SDA, SCL);

does the same like


on the NodeMCU.

Noiasca , thank you.
I found this scanner a few days ago and used already.