Wire.begin requirements

Hello,

New to PlatformIO and trying to get my bearings. Was wondering if someone might know what the int values the Wire.begin method is looking for for SDA and SCL. I have an OLED 128 x 32 screen and a Node MCU. I have the pins that the OLEd needs on pin D1 and D2 respectively.

I have tried swapping the numbers, I tried using the GPIO pin numbers (4,5) instead of D1 and D2 and neither work.

I have gotten it to work with normal C++ for Arduino, but PlatformIO is closer to my .NET knowledge and when I remove the begin method, it seems to work… for now.

Below is a screenshot of the method and intellisense from VSCode of the override method I’m looking at.

Thank you!

This is a misunderstanding!

There are 5 overloads (5 different versions) for Wire.begin that you can use.
You can view these overloads by pressing the arrows (in your screenshot).
And you can go directly to it’s declaration / implementation by CTRL+click on Wire.begin. ( ← this is the best feature to learn!)

There are
begin(int sda, int scl)
Let you specify the pins for SDA and SCL

begin(int sda, int scl, uint8_t address)
Let you speficy the pins for SDA and SCL and the address

begin(void)
Using the default pins (board specific) for SDA and SCL

begin(uint8_t address)
Using the default pins (board specific) for SDA and SCL and let you specify the address as an uint8_t

begin(int address)
Using the default pins (board specific) for SDA and SCL and let you specify the address as an int

In your screenshot simply a ‘;’ is missing after “Wire.begin()”.
So “Wire.begin();” would be correct if you use the default pins for SDA and SCL for your board.

Thank you for the info. Yes, thankfully I am familiar with C++,Java, C# etc, so I’m familiar with interfaces and navigating to the methods. Unfortunately, none of the methods have comments, so it doesn’t tell you exactly what it is looking for and I am missing something for sure.

I should have provided the screenshot from the video I was following. I would put the code in here, but there is no github for it from the author.
Here is the video as well. https://www.youtube.com/watch?v=jB0TxtKQVzw&t=8s

The author has it set to 13 and 12. As mentioned, I have tried swapping the numbers, to match Node MCU. I tried using the GPIO pin numbers (4,5) instead of 1 and 2 (D port numbers) and neither work. I also tried using D4 and D4 (GPIO port 2 and 14) which also didn’t work.

The Author of the video said that his board does not use the default SDA and SCL pins. That’s the reason why he specified the GPIO’s for SDA and SCL.

The default GPIO’s on an ESP32 are GPIO 21 (SDA) and GPIO 22 (SCL) - See pins_arduino.h

But due to the ESP32’s GPIO Matrix and Pin Mux these can be mapped to any GPIO. This is the reason for the existence of the begin function which allows you to specify the GPIO’s you want to use for SDA and SCL.

The questions are

  • Which board do you have exactly?
  • What’s the board = entry in your platformio.ini
  • How did you wire the display to your board?

Offtopic but to clarify the terminology and to avoid misunderstandings later on:

  • What you refer to as an “interface” is a “class”.
  • What you refer to as an “interface file (.h)” in the other post is a “header file”.

Yes, sorry in C++ they are called headers, sorry about that. Used to the other languages. Not sure what you were referring to me calling interfaces classes. But I must have misspoke.

To answer your questions:

  • The board is a Node MCU ESP-12E located here
  • The board entry in the ini file is espressif8266
  • Based on what you explained above I am using D1 (GPIO 5) and D2 (GPIO 4) and I am now using the overload method without values Wire.begin() so it defaults to what SDA/SCL are for these boards.
    What I’m confused about is when I used the method on my original post, I tried using the overload for SDA/SCL it didn’t like it.

Is the number supposed to use the digital number or the GPIO number?

Thanks for your help.

This contradicts your statements in the other thread. There you said that you use Espressif32. The NodeMCU is available with both ESP8266 and ESP32. But now we know that you are using the ESP8266.

So you’re platformio.ini should look something like

[env:NodeMCU]
platform = espressif8266
board = nodemcu
framework = arduino

Maybe you’re using nodemcuv2 which is also okay.

Yes, the numbers are GPIO’s. You can see the translation from Dx to GPIO numbers here

You should connect your display to SCL = D1 (GPIO 5) and SDA = D2 (GPIO 4) and use the standard Wire.begin(); without any arguments.

1 Like

Ok got it. Thank you for the help and the pointers .you are correct. I must have copied from the wrong platformio.ini. i had an example VSCode open as well. Sorry about that. I updated the other thread so anyone finding either don’t get confused.

Thanks again!