0
\$\begingroup\$

I have a situation where I tried setting pins on my Atmel Mega328P to INPUT_PULLUP. I was told to expect an inverted input: a +5V would result in LOW reading and a GND would result in HIGH reading. However, what I found is the exact opposite. I tried checking my +5V and GND with some LEDs and they are definitely +5V and GND.

Below is my arduino code; I know it's not very efficient, but that's besides my point. My IDE is Eclipse C++.

#include "Arduino.h"
int count = 0;
void setup() {
    Serial.begin(115200);
    pinMode(PIN2, INPUT);
    pinMode(PIN4, INPUT);
    pinMode(PIN5, INPUT_PULLUP);
    pinMode(PIN6, INPUT_PULLUP);
}

void loop() {
    byte i = 0;
    if(digitalRead(PIN2) == HIGH) i += 1;
    if(digitalRead(PIN4) == HIGH) i += 2;
    if(digitalRead(PIN5) == HIGH) i += 4;
    if(digitalRead(PIN6) == HIGH) i += 8;
    Serial.println(i, BIN);
    delay(1000);
}
\$\endgroup\$
  • 4
    \$\begingroup\$ Why are you expecting an inverted input? If I remember right, the input_pullup mode just connects an internal pullup resistor, no? \$\endgroup\$ – Hearth May 5 '17 at 1:10
  • \$\begingroup\$ Perhaps you are mis-remembering a comment about what would happen with a pullup and a swtich to ground - switch "off" is a "1" while switch "on" is a "0" \$\endgroup\$ – Chris Stratton May 5 '17 at 1:27
  • \$\begingroup\$ Sorry, I actually mis-remembered. Thanks for pointing out though. \$\endgroup\$ – krismath May 5 '17 at 1:56
  • \$\begingroup\$ Should I remove this question then? \$\endgroup\$ – krismath May 5 '17 at 2:18
  • 1
    \$\begingroup\$ Accepting an answer is generally a better way to mark it resolved; the available one isn't outstanding but it isn't wrong either. \$\endgroup\$ – Chris Stratton May 5 '17 at 2:53
2
\$\begingroup\$

It isn't possible with the Atmega328P to switch how the IO pins translate a voltage to a binary value in a register. (And the internal pull-up has nothing to do with this)

Just check for the opposite condition in the code:

if(digitalRead(PIN5) == LOW) i += 4;
if(digitalRead(PIN6) == LOW) i += 8;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.