input_pullup problem

Hi - I am a wrinkly newbie, used to only writing very basic sketches for the last few years. Now I am trying to write a sketch which activates external devices when input is received through 4 reed switches running 5v current. However, my test sketch is not producing the result required as the output pins light up the test LED (in place of the external device) with the input pins disconnected.

So, I looked this up and used “input_pullup” in the sketch but this still gave a lit up LED. In fact, if anything, the problem looked deeper as with the code written in original form (as below) when I connected the input pin to ground, the test LED went out but with “input” replaced with “input_pullup”, connecting the input to ground did not put out the LED.

I’ve also tried the older “digitalWrite inputPin” instead of “input_pullup” but the result was the same. As I’m not using any external devices - just a LED - and a bog standard Arduino clone, I guess I’ve done something silly in the code! I’d be really chuffed if someone wiser than me could tell me what I’ve done wrong…my code is below:

int Outpin3 = 13;
int Outpin2 = 12;
int Outpin1 = 11;
int Outpin0 = 10;
int inputPin2 = 2;
int inputPin3 = 3;
int inputPin4 = 4;
int inputPin5 = 5;
int val = 0;

void setup() {
pinMode(Outpin3, OUTPUT);
pinMode(Outpin2, OUTPUT);
pinMode(Outpin1, OUTPUT);
pinMode(Outpin0, OUTPUT);
pinMode(inputPin2, INPUT);
pinMode(inputPin3, INPUT);
pinMode(inputPin4, INPUT);
pinMode(inputPin5, INPUT);
}

void loop(){
if (val = digitalRead(inputPin2) == HIGH) {
digitalWrite (Outpin3, HIGH);
}
else if (val = digitalRead(inputPin3) == HIGH) {
digitalWrite (Outpin2, HIGH);
}
else if (val = digitalRead(inputPin4) == HIGH) {
digitalWrite (Outpin1, HIGH);
}
else if (val = digitalRead(inputPin5) == HIGH) {
digitalWrite (Outpin0, HIGH);

}
else {
digitalWrite (Outpin3, LOW);
digitalWrite (Outpin2, LOW);
digitalWrite (Outpin1, LOW);
digitalWrite (Outpin0, LOW);
}}

Thanks!

Andrew

How were your inputs wired when you tried using INPUT_PULLUP ? A schematic would be helpful

Post the code you used when trying INPUT_PULLUP

Instead of this:

if (val = digitalRead(inputPin2) == HIGH) {
    digitalWrite (Outpin3, HIGH);
}

try:

if (digitalRead(inputPin2) == HIGH) {
  digitalWrite (Outpin3, HIGH);
}

Thanks to both of you. Re: UkHeliBob questions...I had a single input wire connected to the Arduino at pin 2 with pin 13 connected to the LED. The input wire was left disconnected but tried connecting to earth which made no difference. The code I used was identical to the code posted with pinMode(inputPin2, INPUT_PULLUP); - repeated for pins 3,4 and 5.
Re: 6V6gt - thanks, I will go away and try using your code and report back...

Ok, 6v6gt, I've tried that revision but I still have the test LED lit (through pin 13 with pin 2 as input). LED remains lit unless pin 2 is earthed and lights again if I remove the earth connection.

You use INPUT_PULLUP on an input if you want to make the pin HIGH when nothing is connected (ie floating). So if you use INPUT_PULLUP, when you disconnect input, it becomes HIGH. Is that what you want ?

Sorry if I was unclear - I have a quirk in my head which often leads me to get confused with polarities so please let me explain the aim in different terms.

The intention is that when a 5v signal HIGH is supplied to an input pin, this causes the related output pin to also supply a HIGH. With the input pin disconnected (through a reed switch, in the final assembly) the pin should be LOW and the relevant output pin also LOW.

Did I misunderstand "INPUT_PULLUP" and how do I keep the input pins in a LOW state unless triggered? Thanks again.

Without something external, not.

But just move the read switch to be between GND and a pin. That way it pulls the line low when it's activated.

For a lot of newbies that sounds counter intuitive but it's just flipping the logic. And if you progress in electronics you find out why the "counter intuitive" to pull an input low to active it is actually a great idea :wink:

Thanks Septillion - I think you've hit the nail on the head and the counter intuitive answer follows so much I've seen in "external life"! I'll carry on learning slowly - all best, Andrew

It works perfectly - again, thanks. A lesson learnt.

The key is in the name. It’s called a pull up resistor. When the input is disconnected, it pulls them up to HIGH.

The opposite of that is a pull down resistor, that would hold a disconnected input to LOW.

Banish from your mind any notion of HIGH = ON and LOW = OFF. HIGH and LOW are merely voltage levels, any additional meaning they have in your circuit is arbitrary. I recommend not using HIGH and LOW directly sprinkled around in your code, and use them as const variables just like you use for your pin numbers.

const byte LED_ON = HIGH;
const byte LED_OFF = !LED_ON;

const byte SWITCH_PRESSED = LOW;
const byte SWITCH_UNPRESSED = !SWITCH_PRESSED;

This way, instead of being confused by what HIGH and LOW mean in your circuit, you know exactly what SWITCH_PRESSED represents. Also if you make a mistake like you did here, you only have to change the value in one place instead of every time you read a switch’s value.

Many peripheral chips use what is called active-low logic, meaning the LOW voltage level turns them on or activates some feature of theirs. LOW = ON is an extremely common thing that you will have to get used to if you create more complicated projects. The reason for this is performance: N-channel transistors (which are used to drive a signal LOW) have significantly higher specs than a P-channel transistor (which drives the signal HIGH) of the same size.

Arduinos (and many other types of microcontroller) happen to be designed with a symmetric output driver that can push out just as much current as it can pull in. Not all chips are designed that way though, and if there is asymmetry it’s 99% certain that LOW will be stronger than HIGH.