Pull-Down Resistors & Mega2560

I'm using a Mega2560 board. Based on a tutorial, I connected this circuit, using a pull-down resistor. It works.
Pull-Down Connection
Then, as I'm trying to wrap my head around pull-down/up resistors, I am seeing this schematic:
Circuit - Pull-Down Resistor
I noted that I do not have the 10K resistor between the button and input pin (white wire). Is this extra 10K resistor necessary?

Also, I thought maybe the 2560 includes a built-in resistor. I looked at the datasheet, but could find no reference to a resistor that made sense.

No it doesn't. Some people add this for safety in case you make a mistake and antecedently make this pin an output.

The use of pull down resistors is not the best way to wire an input. You can do it all without any resistors at all by using the internal pull up resistors built into each Arduino.

For a full discussion please read this:
Looking at inputs

1 Like

That's an enlightening read! I suppose I'll need to invest in some "push-to-open" switches.

No not at all, I think you are missing the point here.
When a normal push to close button is used then HIGH is the un-pushed state and LOW is the pushed state. You just use these states in your code.

Many beginners might find this confusing but it is the way most people use it. If you are uncomfortable with this then add to the top of your sketch

#define PUSHED LOW
#define UN-PUSHED HIGH

and then use things like

if(digitalRead(buttonPin) == PUSHED) { // do the stuff when pushed
1 Like

Yep, I'm a beginner, but I think I'm getting the point. Just working out the logic in my head. Your input is helping.

I can logic it as "closed" = grounded = low.

The compiler will not really like the UN_PUSHED UN-PUSHED although code will compile.

I usually use

#define ISPRESSED LOW

and leave the opposite state out

// if button is pressed
if(digitalread(buttonPin) == ISPRESSED)
{
}

// if button is not pressed
if(digitalread(buttonPin) == !ISPRESSED)
{
}
1 Like

Unfortunately, the "tutorials" on the Arduino site generally show switches to +5 V and pull-down resistors. They are badly written in many ways.

One reason to wire the switches to ground, is that you really do not want the 5 V supply wire going out to any part of an assembly that it does not need to as there may be some risk of wires becoming damaged and shorting it to ground.

There are other reasons ...

2 Likes

Why not?
It will compile and it will work. I don't think the compiler is advanced enough to have feelings.

Yes that is what it is. CLOSED can replace PUSHED.

Y the way there is a tradition that goes if all the letters in a variable name, that variable is in effect a constant. You can totally ignore that if you like but it makes the code simpler to understand at a glance, and is considered good practice.

Got it all sorted in my head, and updated my latest project to use pull-ups, and it's working good. Thanks for the info!

Sorry, it will like UN_PUSHED but not UN-PUSHED. Typo, apologies.

C:\Users\serretje\AppData\Local\Temp\arduino_modified_sketch_250350\sketch_jul26a.ino:1:11: warning: ISO C++11 requires whitespace after the macro name

 #define UN-PUSHED HIGH

           ^
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.