ESP32 digital inputs not changing state [SOLVED]

First of all let me qualify my post by saying I have not been drinking or
smoking any left handed cigarettes....lol

I have worked with ESP32s on numerous occassions and feel fairly comfortable programming them and wiring inputs/outputs.

My latest project has both analog and digital inputs. The analog inputs work fine. But the digital inputs are not changing state.
I have used two different esp32s to make sure I did not have a defective processor.
I have tried different sketches as well.
So I decided to revert all the way back to a simple sketch that looks at
a dig input and turns on the builtin LED as a function of the digital input being closed or open.
Nothing works and I am now throwing myself at the mercy of this forum to tell me what stupid code or wiring configuration I am trying to use.
.......otherwise I am stumped.
Note - Only GPOI inputs 6 -11 are unavailable for digital inputs as they are related to Flash.

#define ONBOARD_LED 2

const int switchPin = 22;
int switchState = 0;

void setup() {
  Serial.begin(115200);

  pinMode(ONBOARD_LED, OUTPUT);
  pinMode(switchPin, INPUT);
}

void loop() {

  switchState = digitalRead(switchPin);

  if (switchState == HIGH) {
  digitalWrite(ONBOARD_LED, HIGH);
  } else {
  digitalWrite(ONBOARD_LED, LOW);
  }
  Serial.println("Sketch running....");
  Serial.println();
  delay(1000);
}

Wiring is using a 10Kohm resistor from the input (22) to ground. I have metered all connections. The input goes to 1 ohm with the alligator clips connected.
wiring pic:

Try pinMode(switchPin, INPUT_PULLUP);

Wire the switch from the input pin to GND, a LOW means switch is closed.

so you have a 10k between 22 and GND,
and your aligator clips switch is also 22 and GND??
if my understanding is correct, then the state will definitely not change.

connect the 10k to 3v3 instead, or remove it and use internal pull up as LarryD suggests.

Be sure that your wiring agrees with the following:

1. Screw one end of R1 directly with GND.
2. Twist together the other end of R1 and male header of alligator-1 and screw them into P22.
3. Connect allegator-1's mouth with one pin of K1.
4. Screw male header of allegator-2 with 3V3. Connect allegator-2's mouth with other pin of K1.
5. With an AVM, check the continuity.
6. Upload sketch of post #1.
7. Gently press K1.
8. Check that onboard blue LED of ESP2 has turned ON.

Suggest you always connect the switch from the pin to GND.

Connecting the switch to 3V3 volts is asking for trouble as you are taking the power lead out to a remote location.

1 Like

Your suggestion is safe and not clumsy. The connections stands as is shown in Fig-1 using internal pull-up resistor (Rip).


Figure-1

The Modified Sketch of post #1:

#define ONBOARD_LED 2

const int switchPin = 22;
int switchState = 0;

void setup() 
{
  Serial.begin(115200);

  pinMode(ONBOARD_LED, OUTPUT);
  pinMode(switchPin, INPUT_PULLUP);
}

void loop() 
{
   switchState = digitalRead(switchPin);

  if (switchState == LOW)
 {
      digitalWrite(ONBOARD_LED, HIGH);
  } 
  else 
  {
       digitalWrite(ONBOARD_LED, LOW);
  }
  Serial.println("Sketch running....");
  Serial.println();
  delay(1000);
}

Larry,

Your suggestion along with removing the 10K resistor works. But what is confusing the crap
out of me is why I have an ESP32 in the field right now that is monitoring a relay contact
with the 10 k resistor and the pinMode defined as 'INPUT' and it works perfectly.
The only difference being the one installed is a 32 pin ESP WROVER model while this newest one
is a 38 pin WROVER. Would there be a difference in the two on how they handle digital inputs?
Evidently something is not the same....lol.
Thanks for your suggestion!

Ed

how is it wired?
is the resistor between the pin and ground and the relay contact between the pin and V+?

1 Like

No.


You can use an internal or external 10k resistor (connected to 3V3).

The switch connected to GND presents a LOW when closed, HIGH when open.


We will need a plane ticket to check the actual circuit to which you are describing :wink: .

Do you serve Stroopwafels ?


Keep this near by when you are doing the next project.

VCC will be 5v for a 5v Arduino, 3v3 for an ESP32.


It had been over a year since I installed that project so I decided to break out my notes/drawings and sure enough you are absolutely correct in the wiring configuration. I thought I correctly remembered it but the onset of senility is now making me a tad concerned. Lol....

Your suspicions were spot on.... :+1: :+1:

Good stuff Larry. I will add your input drawings to my project notebook.

Thanks for keeping this geriatric patient moving along ..... :+1: :+1:

Larry,

Since the ESP32 has both 3.3v and 5v does it matter which you source for the digital inputs?

If the controller is 3v3, the inputs should transition between 0v and 3v3.

Do not use 5v.


On a 5v controller (example UNO Rev3), inputs can transition with either 5v or 3v3.


An ESP32 operates at 3v3, therefore the inputs are to use 3v3.


The 5v on an ESP32 board is used by a 3v3 regulator which generates the required 3v3 for the controller.

Well I now realize I am in violation of ESP wiring prootcol. I have been using VIN [+5V] on the 30 pin ESP32 to not only drive a digital input w/ external 10K resistor but also to energize one of those small black AGT icecube relays via a 2N2222. Are we supposed to find a 3v coil relay to use with an ESP. I know with a wall wart providing the power to an ESP through the USB port there is the +5v present at VIN. Is this something being done that is not "kosher?"

  • The ESP32 output drives a NPN transistor (2N2222) whose emitter is connected to GND.

  • A relay coil connects to the transistor collector then to your relay supply voltage, can be 5v, 12v, 24v …

We put a reverse biased kickback diode across the relay coil (1N4148).

When the ESP32 output goes HIGH (3V3), the transistor turns on and the relay picks (energizes).


The label 12v, can be 5v or 24v etc., but must match the relay coil rating.

hqdefault

When you say ESP32, which one are you using ?

Take a picture of the controller so we can see what it looks like.

:thinking:

It is the WROOM 38 pin model. I believe it is the ESP DROVER (sp?) option from the IDE processor select listing. I do not have the IDE up right now. That one seems to work for me. I used the shorter ESP WROOM (30 pin) on an earlier project.

Please zoom out so we can see the whole PCB module.

here ya go:

This is what we are looking for in connections to a relay and switch.

  • Your pin locations might be different from mine.



If it was me, I would connect the 5V wallwart to the modules 5V pin, and the wallwart 0V to the modules GND pin.

i.e. Don't use the wallwart into the USB plug

See schematic below: