How to connect/program a switch on a Wemos mini S2 to detect on/off signal?

This ESPHome stuff is very intriguing so I bought a Wemos mini S2 to get familiar.
I got it working in HA and found out how to control the LED :star_struck:
Now I would like to read the status from a switch, so 2 wires without current.

I need to use 2 pins which represent 2 GPIO’s, right?
Am I correct that I have to use the binary_sensor component?
I’m searching how to put this into code.


Suggest to start to read official pages:

Then You will find GPIO binary sensor component and much more.

Hi, thank you for the links: I did already search/read on the ESPHome site prior to posting.

EDIT: what puzzles me is 2 wires from the switch and 2 GPIO’s, whereas in the code I see 1 GPIO being used.

What puzzles me is what are you looking at? Link? YAML file?

You only need one GPIO for a switch:

The Pullup mode is the most common because you can enable an internal pullup resistor in the ESP, eliminating the external pullup resistor.


Ooowkay… so 1 lead connects with ground!?! :blush:

Normally. But until you post your YAML file, it’s only a guess.

Stephen, I don’t have any YAML yet, just the basic configuration to start.

My question/doubt is mostly on how to physically connect the wires. (note: inexperienced with ESP devices, and additionally electronics)
Now that you showed that image it seemed clear that I have to connect 1 lead to ground.

Is it also possible that I have to use 2 GPIO’s instead, as I thought initially?

Not normally. I’ve built dozens of IOT things using ESP’s and never connected two GPIO pins the way you describe. Not that you can’t, but I can’t imagine why you would ever need to. If you are looking at a tutorial that does connect two GPIO pins to an SPST switch, then the rest of that tutorial would be questionable.

For extreme laziness. If you don’t have any GND pins available and you don’t want to split wires. :sweat_smile:

You only need one.

I remember when I first started putting sensors on ESP32s I would run out of GND pins. It took quite a bit of looking about to see you can tie all grounds of your sensors to one pin GND.

1 Like

Exacty, smart guy ties all gnd wires properly to one pin, lazy guy use breadboard if available. If not, gpios for signal gnd. :wink:

Are you also willing to share how you do that?

I tend to make my own expanders out of pinheaders, veroboard, and Duponts. You can buy them too.

Depending on the project I expand gnd, 3v, 5v and i2c like that.

The ones in my photo are crappier ones from my scrap heap.

For some projects I may solder wires directly to say veroboard if I’m worried about forming solid connections with Duponts.

Which one? Gpio for signal gnd?
ps. I didn’t write that I’m one of them, smart or lazy…