Notifications
Clear all

[Solved] I can't find the info I need

63 Posts
6 Users
22 Likes
3,638 Views
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6896
Topic starter  

I am not sure if my google gene suddenly died, or maybe I had a mini stroke, but I am stumped.

In the included pictures is a simple NODE MCU ESP8266 connected to a push button and Led.

WHY is the Led ON when the button is NOT pressed?

I can deal with that in the code IF that is normal, but it just seems to be backwards to me.

I have read about pull ups and pull downs, and I think I tried every possibility but at this point I am not sure of my own name. I have spent the last 8 hours trying to get a circuit like this to work as part of a bigger project.

The pictures and code should give you all the information if you care to have a look and tell me where I am going wrong, but if not, just ask, I am not going anywhere for a while other than to pour a drink, I NEED it!

In the last 2 pictures hopefully you can see the RED wire connecting the PB to D4 and the green wire connecting the LED to D7.

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin 2 D4
const int ledPin =  13;      // the number of the LED pin 13 D7

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}
IMG 6773
IMG 6775
IMG 6776
IMG 6778
IMG 6779

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
Quote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2042
 

@zander

Does the button need a pull up resistor I can't see one?

You can use an internal pull up with,

pinMode(buttonPin, INPUT_PULLUP);

 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6896
Topic starter  

@robotbuilder I didn't see any mention of it but I did try using a real resistor. I will try as you suggest to see if it makes a difference.

Where does that statement go, I assume in setup but before or after the existing pinMode(buttonPin, INPUT)

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2507
 

@zander

It's a bit hard to distinguish the wire colours from your photos, but it appears that the button is connected to ground on one side and pin2 on the other side.

Thus, when the button is pressed, it ties pin2 to GND (i.e. low) and the LED should turn OFF.

However, there doesn't seem to be anything connected to the button to tie it to any state when the button isn't pressed. It appears that pin2 is just floating and interprets that state as HIGH, so it lights up the LED.

To get the effect that I think you want (assuming you're using a normally open button), you'll need to have the button tied to VCC when pushed and to GND otherwise.

So change your current connections

- move the button connection from GND to VCC

- add a 10K resistor from the other end of the button to GND

- leave the wire from pin2 to the button as-is

And see if that helps.

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2507
 

@robotbuilder 

Won't that turn the LED ON all the time and OFF only when the button is pushed ?

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6896
Topic starter  

@will Now the blue led comes on and pressing the button turns it off. The red led is dimly lit and never changes.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6896
Topic starter  

@will @robotbuilder I will wait until you two heavyweights sort it out. I tried Will's solution and it didn't work but am still waiting for clarification on the placement of the pinmode statement.

Sorry guys, I never encountered the terms pullup and pulldown in my past so this is all new to me.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2507
 

@zander 

Blue LED ?

Could you perhaps explain what you're trying to do ? I totally missed the blue LED (Thought it was part of the ESP).

Is the button just to control the red LED? Up for off and down for on ?

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6896
Topic starter  

@will The blue led is part of the 8266. I am simply trying to figure out how to wire up an 8266 to detect a button press and then turn on a led.

This is the first sketch I am writing from scratch (the one I posted is an example) and it took me hours to figure out the pins to use on the 8266. I am not joking, I have been trying since 8AM.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6896
Topic starter  

@robotbuilder I am guessing I just change the existing statement from INPUT to INPUT_PULLUP.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2507
 
Posted by: @zander

Sorry guys, I never encountered the terms pullup and pulldown in my past so this is all new to me.

No problem. The terms pull up and pull down refer to resistors used to prevent a pin from floating by tying it to a specific value. In this case those values are usually VCC or GND.

In your case (if I understand correctly) you want the red LED to stay off until you press the button. So, to tie the value low, you use a 10K resistor (so as to draw/waste minimal current) from one side of the button to GND. That will keep the pin LOW. That pin is then connected to one side of the switch as well. The other side of the switch is pinned into the VCC rail.

So, when the button is at rest, the 10K resistor keeps the pin from floating by tying it to the GND rail. However, while the button is pressed, the pin is connected to VCC. There is also a tiny current still going down the 10K resistor, but it is vastly overwhelmed by the direct connection to VCC, so the pin goes HIGH.

Take a look at this for a better explanation ...

https://www.seeedstudio.com/blog/2020/02/21/pull-up-resistor-vs-pull-down-differences-arduino-guide/

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6896
Topic starter  

@will Now what happens is the led starts off bright red then dims a little when I press the button. The blue led works as before.

Remember, it's a simple matter to fix this in code, just add a ! in front of the if comparison, but I want to know why it is the way it is.

Also, it's a 8266, not an arduino or an ESP32 if that makes a difference.

I did find in the documentation where it says the blue led is inverted.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2507
 
Posted by: @zander

Also, it's a 8266, not an arduino or an ESP32 if that makes a difference.

I've never used an ESP of any ilk, so I don't know if or how they differ from Arduino.

I don't understand what's happening, is your button connected as per the diagram for pull down circuit ? You don't need the 100 ohm resistor between the pin and the button.

What is the significance of the blue LED on the 8266 ? 

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1659
 

Hi @zander, @will & @robotbuilder,

  Before you get too mixed into the detail ... The ESP8266 is 3.3V chip ... but 'confusingly' the Vin pin is probably 5V  ... I think there is an AM117 for 5V to 3.3V regulation on the board. I haven't looked it up properly yet .. but if I am right, and your supply is set to 5V, it musn't connect to the switch, LED etc.

Unfortunately, its dificult to see all of your connections in the photos.

I suggest you slow down and work out the circuit..  (apologies if I have got this wrong .. just warning in the hope of reducing damage risk.)

Dave


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2507
 
Posted by: @davee

Hi @zander, @will & @robotbuilder,

  Before you get too mixed into the detail ... The ESP8266 is 3.3V chip ... but 'confusingly' the Vin pin is probably 5V  ... I think there is an AM117 for 5V to 3.3V regulation on the board. I haven't looked it up properly yet .. but if I am right, and your supply is set to 5V, it musn't connect to the switch, LED etc.

Yeah, I wasn't sure so I specified tying the button to VCC.

It's not that I'm smart, I just didn't know there was a Vin 🙂

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
Page 1 / 5