ESP8266 Fail to read input pin (no error message)

Hello,

Summary
I am having an issue with my board esp8266 reading if a switch is on/off. I am simulating the switch with a wire at the moment.

Context
I can connect to my network, but I cannot read an input pin.

What I have tried
I remove the switch for a button. I tried many different ports numerically and with D1, D2... format. I tried to use a second esp8266. I used the multimeter and I can see the voltage around the resistance. I tried the 3v and 5v for the power source.

Code
My last attempt looks like this:

#include <ESP8266WiFi.h>

const char* ssid     = "";      // SSID of local network
const char* password = "";      // Password on network

int switch_pin = D1;
WiFiClientSecure client;

void setup() {
  Serial.println("Setup");
  Serial.begin(115200);
  Serial.println("Connecting WiFi ");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("Connected: ");
  Serial.println(WiFi.localIP());
  client.setInsecure();
  pinMode(switch_pin, INPUT);
}

void loop() {
  int buttonState = digitalRead(switch_pin);
  Serial.println(buttonState);
  delay(1000);  
}

Wires
5v to switch (white cable in the picture)
switch to 10k resistance
10k resistance to ground
the switch is also connected to the D1 pin

Full size: https://i.imgur.com/o9Fvs8L.jpg

Component:
Board: Amazon.com

Question:
Is my issue with my wiring or the code? I want to know when the switch (cable) is on/off.

@mrdesjardins

Your topic was Moved to it's current location / section as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Hi,
OPs image, resized to 1200 x 1600, I think.

Hi,
Does the Serial.print on the IDE monitor show the change in button state?
Do you have a DMM?

Tom.. :slight_smile:

mrdesjardins:
5v to switch (white cable in the picture)
switch to 10k resistance
10k resistance to ground
the switch is also connected to the D1 pin

Well, for a start, connecting switches to the power rail ("Vcc") is a generally bad idea, they should connect to ground and you either use INPUT_PULLUP to enable the internal pullup, or provide a pullup to Vcc - or in fact, do both if the internal pullup is insufficient.

But you did worse than that - you have connected an ESP input to 5 V.

We understand the ESP will probably not be damaged by this, pulling an input to 5 V when its supply voltage is only 3.3 V; this has been a point of tremendous discussion over the years since they were first available, but it is strictly not recommended. :astonished:

The pullup resistor - if you need it - should go to the 3.3 V terminal, not 5 V. :roll_eyes:

TomGeorge:
Hi,
Does the Serial.print on the IDE monitor show the change in button state?
Do you have a DMM?

Tom.. :slight_smile:

The ide shows "1" in repeat
I have a multimeter, I see 3.29 volt (when I tried with the source of 3.3v)

Well, for a start, connecting switches to the power rail ("Vcc") is a generally bad idea, they should connect to ground and you either use INPUT_PULLUP to enable the internal pullup, or provide a pullup to Vcc - or in fact, do both if the internal pullup is insufficient.

Okay, so I need to move the resistor between the red wire (VCC) and the white wire (my "fake switch"). I will give a try.

I wasn't aware of INPUT_PULLUP
I will check it out!

you have connected an ESP input to 5 V.

The ESP has a 3.3v and 5v output, why do they put a 5V is that is bad? I will use the 3.3v I used the 5v from an online tutorial which I used only once in the past. Might be a bad example :slight_smile:
So let me try tonight with changing the resistance and use 3.3v
If there is anything else, let me know. I'll post back my experiments later.

Thanks! It is working now.

Here is the final code:

// 1) The board need to be LoLin(Wesmo) D1 & R2 & Mini...for that you need:
// 1.1) Add in File>Preference the link: http://arduino.esp8266.com/stable/package_esp8266com_index.json
// 1.2) Under tool, select the ESP8226 and select the Lolin(wesmo) D1, R2 & Pro
// 2) Tools>Library management
#include <ESP8266WiFi.h>

const char* ssid     = "";      // SSID of local network
const char* password = "";                // Password on network

int switch_pin = D1;
WiFiClientSecure client; //For ESP8266 boards

void setup() {
  Serial.println("Setup");
  Serial.begin(115200);
  Serial.println("Connecting WiFi ");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("."); delay(500);
  }
  Serial.println("Connected: ");
  Serial.println(WiFi.localIP());
  client.setInsecure();
  pinMode(switch_pin, INPUT_PULLUP);
}

void loop() {
  int buttonState = digitalRead(switch_pin);
  Serial.println(buttonState);
  delay(1000);  
}

I also changed the resistance position to be before the button and also use the 3.3v!
Instead of using INPUT using INPUT_PULLUP too :slight_smile:

Thanks!

mrdesjardins:
The ESP has a 3.3v and 5v output, why do they put a 5V is that is bad? I will use the 3.3v

The ESP8266 operates at 3.3 V. The WeMOS D1 Mini (which most of us here feels is the most generally useful version of the ESP8266) is usually powered from 5 V because that is what the default "standard" is, what USB provides (though the actual USB signalling is in fact 3.3. V) and is the readily available voltage from USB "phone chargers".

So it incorporates a regulator to drop the 5 V from the USB jack - or the "5V" pin if you wish to power it that way - to 3.3 V. Both supply voltages are brought out to pins on the D1 Mini board as both are available. But the voltage used by the actual ESP chip is 3.3 V, so you should only apply voltages within this range to the chip (and only up to 1 V on the ADC pin).

mrdesjardins:
I used the 5v from an online tutorial which I used only once in the past. Might be a bad example :slight_smile:

Sounds like it! At least in respect of the ESP.