Buttons low. LEDs high. Or tis it other way round?

This comment will be confusing to a middle schooler. The purpose of the resistor isn't to invert things. It does in your program only because you're using a pullup resistor. I'd modify that comment to a "bonus" points for your students, maybe, if they want to go the extra mile and submit an assignment back with a filled in glossary of common electronics terms or something like that. Here's a specific "something like that", courtesy of my wife who is a middle school teacher with 25 years experience:
"Given that the resistor you are using inverts the logic, but is not true of all resistors, your statement may be less confusing by wording it as such: 'In our situation, why do we say LOW and not HIGH when using a pullup resistor?'
A bonus question could ask the students to explain the common purpose of all resistors."

2 Likes

also, super cool you're bringing Arduino to your classroom. So many reasons why.

Resistors invert things also confused me. It does not and they do not. The resistor has nothing to do with the logic being inverted (if that is even the correct term), the decision to use logic LOW as ON is a personal one. The fact that there is a handy internal resistor to facilitate that decision is neither here nor there other than pragmatism suggests you use it rather than an external resistor to facilitate logic HIGH = ON.

I think the problem is the human assumption and intuition that HIGH is better than LOW, and ON is better than OFF thus HIGH should equal ON. Resistors add resistance to current and that is their function. A high resistance pull UP or DOWN simply allows for the switch to provide a lower resistance alternative route and thus drive the pin to the opposite level and thus trigger your code

Seems to me the convention is that HIGH is on and vice versa. The basic projects almost always seem to use momentary on pushbuttons which connects the things through the button and uses HIGH logic to mean turning something on, usually an LED in response. It's more intuitive that way for learners ("push on, turn on") I think which is the whole reason Arduinos were invented, isn't it? To allow average Joes and Janes to knock things together without getting too particular about the low level goings on of the microcontroller. I don't think most middle school teachers are going to be teaching esoteric comp sci philosophy in Arduino. Maybe in high school or college?
So yes, you're technically correct. This is for a teacher trying in earnest to learn this stuff on their own time and likely their own dime, to teach to a room full of middle schoolers of arbitrary skill, interest and experience levels.
I doubt the OP is even going to read this. They are probably too busy.

I don’t think so. LOW equals ON is the most common setup because it is simple and also because most circuits are easier to design that way.

It is important to get across the concept that the microcontroller provides logic and not power. The human predilection for HIGH/ON logic is not shared by the microcontroller. The standard arduino design favours LOW/ON logic for push buttons.

For kids I would explain that, in this case HIGH = 5v and LOW =0V. To detect a button the voltage of the pin must flip from one to the other. That is the digital logic the arduino understands. So, we must start with the pin at one level and to do this we can connect it to the 5V and conveniently we have an internal resistor that will do this when we use the IMPUT_PULLUP code. Now we want our button to flip the pin to 0V so we connect it to 0V so when we press the button, because the resistor restricts current from the 5V the 0V wins and the pin goes LOW.
This is the reason that when the pin reads LOW it knows that the button is pushed.

Cribbed directly from "Basics" of Arduino example sketches, verbatim:

/*
  Button
 
 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 2. 
 
 
 The circuit:
 * LED attached from pin 13 to ground 
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 
 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.
 
 
 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
  // Pin 13: Arduino has an LED connected on pin 13
  // Pin 11: Teensy 2.0 has the LED on pin 11
  // Pin  6: Teensy++ 2.0 has the LED on pin 6
  // Pin 13: Teensy 3.0 has the LED on pin 13

// 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); 
  }
}

and for outputs:

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.
 */

// Pin 13 has an LED connected on most Arduino boards.
// Pin 11 has the LED on Teensy 2.0
// Pin 6  has the LED on Teensy++ 2.0
// Pin 13 has the LED on Teensy 3.0
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

So, no as you say:

That simply isn't true to the beginner in Arduino.

I'm not arguing your circuit design points or anything else outside the scope of Arduino. You're probably right. It's also way outside the scope of (certainly Peel District, Ontario, Canada) most middle school comp sci learning objectives. My wife has taught thousands of kids over 25 years, all the block coding stuff etc and middle schools (at least here) are only just beginning to introduce coding beyond block coding at the general level and just beginning to introduce Arduino/embedded electronics into core curriculum. I've been trying to convince my kids' school to have Arduino in their program for years and they want nothing to do with it. Not enough materials for everybody and too few people who are able to teach it. The latter will continue to be a factor as long as folks such as yourself stand guard as gatekeepers to concepts that are inconsistent and frankly, overly picky.
I mean, in your hypothetical explanation to the kids alone

it isn't even true that the voltage "flips". It doesn't matter at that level but if you say it that way to 11 year olds, that's what many will literally assume you mean. These kids get a couple weeks worth of 45 minute or so lessons at best to try to wrap their heads around this stuff, and it MUST be taught in a way that ALL the kids can understand. Given that they've MAYBE had some snap circuits experience in simple DC circuits, pressing the button completes the circuit, hence turning the device ON which is 1 in boolean logic (since it's not zero).

Finally, it's my understanding in any case that HIGH and LOW aren't even standard C expressions anyway but I could be way wrong there since I only know Arduino-speak C and Processing-speak Java. You've got to address the correct audience, correctly. Think about it: every kid in that class probably has a couple billion transistors in their pocket, right? Betting money on it, how many would you bet could even tell you what a transistor does or even why it's such an important invention?

Sorry, posting some random code does nothing to change the fact that the standard way, and the way the OP was using, is to have LOW/ON logic for buttons. The other way is possible too but the logic and concepts remain the same.

The problem is that the suggestion that resistors invert logic is simply not true. The best that could be said in this case is that the resistor has been used to set the logic to LOW/ON and that you could do the opposite if you like but since you already have an internal, code activated resistor it makes little sense to do so just because of the human predilection for positive/maximal descriptors.

I don’t get your point re the use of the term flip.

All I can tell you is that the original description of a resistor would have significantly negatively affected my understanding as a child. I don’t mind things being simplified for understanding but being simply incorrect is not beneficial.

I do not really see your problem with that and I don’t see why teaching something incorrectly would be preferable when the actual point of the resistor is to resist current, which is far more intuitive. If the OP does not want to bring up the LOW/ON logic that is fine but highlighting it and then giving an incorrect answer is not really a good idea.

There are lots of ways to simplify how circuits and logic work. Some people use water as an example with a resistor acting like a narrow pipe restricting flow. Of course no analogy is “correct” but at least they are not unnecessarily wrong.

You seem to be conflating suggestions of how one might improve a lesson as some kind of negativity. Perhaps that is another human predilection for associating correction with condemnation. I have no such problem and am quite happy that someone who is putting extra effort to teach children and may be the most upstanding and virtuous person in the world may also learn and pass that learning on without bruising their ego.

The "random code" I posted is literally the Arduino IDE example code, by one of the Arduino inventors, Tom Igoe.

Here's some more random code, also in the Examples > Digital IDE space:

/* 
 Debounce
 
 ***********************************************************************
 **                                                                   **
 ** The "Bounce" library offers a much easier way to debounce inputs  **
 **                                                                   **
 ***********************************************************************

 Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
 press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
 a minimum delay between toggles to debounce the circuit (i.e. to ignore
 noise).  
 
 The circuit:
 * LED attached from pin 13 to ground
 * pushbutton attached from pin 2 to +5V
 * 10K resistor attached from pin 2 to ground
 
 * Note: On most Arduino boards, there is already an LED on the board
 connected to pin 13, so you don't need any extra components for this example.
 
 
 created 21 November 2006
 by David A. Mellis
 modified 30 Aug 2011
 by Limor Fried
 
This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/Debounce
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
  // Pin 13: Arduino has an LED connected on pin 13
  // Pin 11: Teensy 2.0 has the LED on pin 11
  // Pin  6: Teensy++ 2.0 has the LED on pin 6
  // Pin 13: Teensy 3.0 has the LED on pin 13

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button 
  // (i.e. the input went from LOW to HIGH),  and you've waited 
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    buttonState = reading;
  }
  
  // set the LED using the state of the button:
  digitalWrite(ledPin, buttonState);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
}

That random code was randomly written by David Mellis, another one of the inventors of Arduino and modified (how, I don't know) by Limor Fried, CEO of Adafruit Industries.
https://en.wikipedia.org/wiki/Arduino

I also already mentioned that "resistors invert things" isn't true which you noticed as well, which I already addressed in the suggestion to OP for their lesson plan.
Seriously, pursue other arguments. Also, and I didn't want to say it, but my 25 years, award winning middle school teacher wife read your explanation to the kids in the last post, and then the one I'm responding to. Um, she got halfway through, said "ok" as she shook her head and then "He wouldn't last a month" (and to be fair, full disclosure, neither would I - no shame, it's a tough job).
Edit: "They wouldn't..." not "He wouldn't..." (she doesn't know you so she wouldn't assume your gender; I mistyped what she literally said).

I’m not sure I care what you or your wife thinks about my explanation or your competitive appeals to authority. Of course you have no idea about my background but that is irrelevant. You seem to have some bee in your bonnet. Perhaps you read my post with some of your own negativity and this coloured your interpretation of my intentions.

Like you, I pointed out the flaw in the OPs explaination but I elaborated to provide understanding so that they might improve their explanations. I did not intend to suggest that I would teach their class for them and my audience is the OP, not your wife’s students.

I am not sure why you keep pulling random bits of code off the internet and appealing, once again, to the authority of their authors in an attempt to somehow counter the fact that LOW/ON is the standard approach to button logic and the one the OP was using. Perhaps you don’t understand that that does nothing to help your unnecessary argument .

Instead of valiantly defending the teaching profession, and this teacher in particular, from the imagined sleights that you seem so eager to see where none exist, perhaps you could reminisce some more with your wife about good times, award ceremonies and such like.

You brought it up, what is it?

False. No negativity here. I'm debating you. You are stating things as some authority yourself, which simply aren't true. I don't care. I'm waiting for some batteries to charge before going off to fly my drone with my son again.

These three examples aren't "random bits of code". All three are example sketches that come with the Arduino IDE. I guess you've never looked at them? Cool story, bro; you're smarter than than me, big man.

Guess we'd have to ask them.

Put it to a poll if you're so sure.

No sleights, only truth. What is it? If you tell me a saw is a hammer and insist on it, you'll forgive me if I insist you're wrong.

God himself could come down and write a bit of code for you but that does not change the fact that;
A) LOW/ON is the standard logic for push buttons (for the reasons suggested already)
B) The OP was using LOW/ON for his push button so nothing else is relevant

What point do you think you are making? That it CAN be HIGH/ON? I have not suggested otherwise.

My background is none of your business but you decided to bring out the “my dad is bigger than your dad” (or wife) ploy. If you think you are debating then you might be aware of the fallacies you are using.

I don’t understand your last paragraph because it doesn’t make sense. What have I said that is factually wrong? You seem to be proving my point. I have not told you a saw is a hammer. I told the OP a resistor is a resistor

Anyway, have fun flying your drone

Have it your way.
Anyway, cheers bud, good chat. Batteries are charged and I'm burning daylight.

You'd lose, if you actually polled people who've been interfacing for decades. Why? Because the TTL, and descendant, input transistor stages work better this way. It's not habit, it's good electrical practice.
Secondly, if you use button-to-ground wiring, you'd realize quickly that that means any wire in your button harness that inadvertently shorts to metal case doesn't take down your power supply, or more likely, fry your wiring, resulting in a hard-to-find failure.
But go ahead, wire your chassis with +5 buttons.
The preponderance of 5V wired buttons in the Arduino ecosphere says zero about whether it's a good thing to do. To me, all it says, loudly, is that "Arduino", and the development group thereof, had hardware realities as an afterthought.
C

3 Likes

I thought we were talking about the following circuit:
an off momentary(on) pushbutton >> Arduino digital pin set to INPUT >> Arduino chip >> Arduino digital pin set to OUTPUT >> LED

I don't. I wire buttons/switches according to typical automotive practice. Positive DC power leg through a fuse to switch pole, out the other switch pole to + on device. Device - to battery/power source ground or (and most often) chassis ground.
For relays (I like solid state) on North American 120VAC lines, I switch hot, the way you're supposed to, for safety reasons.
Maybe I'm not really sure what you're saying, so how would you connect the following components and what would your Arduino sketch look like?
You want to use a +5V signal (TTL or close enough) on a typical RC airplane receiver to switch a +12V accessory on your device (say a 12 volt electric pump). You decide on an Arduino UNO, a typical buck converter, 1000 ohm gate resistor, an FQP30N06L MOSFET, a 1N4007 flyback diode and a 12 volt automotive relay. Power source is an typical 12 volt SLA battery. If you prefer, swap out the RC Rx signal line for a common off momentary (on) pushbutton or even an automotive latching rocker switch.

Don't set too much store by the examples @hallowed31 : there are so many things wrong with so many of them, they should not be held up as any kind of authority, ever. (Like powering servos from the Arduino for example.) Strikes me they were just cobbled together years ago to get the ball rolling so to speak; many of them are crap and for whatever reasons have never been updated.

1 Like

Particularly as so many of them use delay(). Poor old newbie grabs an example, gets it working, starts to make changes, futz' it up, comes here looking for help, and immediately gets thumped for using delay. He/she licks their wounds, thinks a bit, decides they were set up, and walks away.
But, I'm not volunteering to replace examples, so I'll stand down now.
C

I hear ya. Two things:

  1. They are the resource most beginners will refer to first, unless they're specifically rebuilding
    someone else's project as duplicate/slight variation.
  2. Not to be picky (and you're right about the link in the servo knob/sweep examples, never
    noticed that before why is there a breadboard AT ALL in the sweep Fritzy?) but that's a
    hardware mistake, powering servos from Arduino, not exactly a software mistake.
  3. It isn't always wrong to use delay(). It is much easier for the beginner to understand and
    works fine if you know you'll never be adding additional complexity to simple projects. Also
    good for stabilizing analog reads in a simple sense (although I prefer the circular buffer
    technique that I learned from one of Simon Monk's books Programming Arduino: Next Steps
    Going Further With Sketches
    , 2014. But I digress). I think I am a fairly good rep for the Average
    Joes and Janes camp in that I have no prior education or experience with anything to do with
    computers, microcontrollers, circuits, any of it prior to Arduino. For me (and many others) it was like
    "I know I want to get from here to there, across town and a bicycle is a potentially efficient
    way to do that. Now, how do you ride a bicycle?"
    and went from there. When I first encountered the Adafruit webpage
    https://learn.adafruit.com/multi-tasking-the-arduino-part-1/a-classy-solution
    I knew this was the way, certainly better than delay() almost always. I admit though, I had a VERY hard time getting my head around it, particularly because I found the terms they used to be confusing as someone who was imagining playing with my Casio digital wristwatch in the 80s and trying to get stop the watch on exactly round numbers of seconds (admit it: you did that too :wink:

So if there's any point to this rebuttal, real quick, it's that yes, in practical terms, the examples ARE an authority on Arduino just because that's what we Average J/Js get. If you buy an all purpose kit from Elegoo (and presumably others), the example code they include on disc is pretty much the same thing. This creates a couple of problems for the Arduino forum: firstly, that it's very intimidating to the initiate and would-be contributor alike and secondly, building off the first issue, many potentially great projects probably never come to fruition for all the gatekeeping and adherence to traditional educational norms.

So finally, I'd like to apologize to anyone here who finds my style off-putting or aggressive in tone at times. What can I say but I'm a fighter and IRL I'm constantly surrounded by uber-macho highly aggressive alpha types (think 1970s era NHL hockey) and sometimes I forget I can come off as aggressive myself. In terms of building projects, I'm far from elegant in my approach, which is why I like Arduino so much. It has a real rock 'n roller spirit built in to it that I think is a positive part of the spirit of today's age. It makes me think of the world of aviation in the interwar years, as described by Malcolm Gladwell in The Bomber Mafia. (If you haven't read it and you're into aviation/war history, I highly recommend it).

If you're reading this, cheers. I'm hallowed31, and these are just my opinions.

For those about to build, I salute you! :vulcan_salute:

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