False positive signals uisng INPUT-PULLUP mode

Hello, I've been looking for a solution on this one but did not see anyone having this (exact) problem - only similar problems but not comparable. Using Arduino Uno (or Nano - both show the same behaviour) I have the following phenomenon:

I am using 4 digital inputs (D2 to D5) Configured as INPUT_PULLUP so internal resistors are used and I can directly connect GND and D2-5 to create a signal (according to documentation when using INPUT_PULLUP then LOW=ON, HIGH=OFF) So far nothing special... Now, when I create a signal on D2 and D3 in short time distances (by directly putting together the cables GND-D2 resp. GND-D3 - no external instruments or sensors used!!) I sometimes get a LOW value on D4 or D5 as if I had created a signal on those pins. But they are isolated! Nothing at all is connected to them! I can see this happen on the serial monitor of the IDE by the serial.print commands I added in the appropriate IF sections. So I expected to see only lines like TAZnew1234.56 or TBZnew2345.67 from D2 and D3. But sometime I also get TAS1234.56 or TBS2345.67 from D4 and D5

Here is the simple code snippet:

void setup() { //================= pinMode(1,OUTPUT); pinMode(2,INPUT_PULLUP); //TAZ pinMode(3,INPUT_PULLUP); //TBZ pinMode(4,INPUT_PULLUP); //TAS pinMode(5,INPUT_PULLUP); //TBS pinMode(6,OUTPUT); // reserved pinMode(7,OUTPUT); // reserved pinMode(8,OUTPUT); //reserved RED F1 pinMode(9,OUTPUT); //reserved RED F1 pinMode(10,OUTPUT); // LED1 pinMode(11,OUTPUT); // LED2 pinMode(12,OUTPUT); // LED3 pinMode(13,OUTPUT); //onboard LED //================== digitalWrite(1,LOW); //digitalWrite(2,LOW); //digitalWrite(3,LOW); //digitalWrite(4,LOW); //digitalWrite(5,LOW); digitalWrite(6,LOW); digitalWrite(7,LOW); digitalWrite(8,LOW); digitalWrite(9,LOW); digitalWrite(10,LOW); //red digitalWrite(11,LOW); //yellow digitalWrite(12,LOW); //green digitalWrite(13,LOW); //======================

}

void loop() {

if (digitalRead(2)== LOW){ TAZnew = millis(); Serial.print("TAZnew"); Serial.println(TAZnew); //some more code... }

if (digitalRead(3)== LOW){ TBZnew = millis(); Serial.print("TBZnew"); Serial.println(TBZnew); //some more code... }

if (digitalRead(4)== LOW){ TASnew = millis(); Serial.print("TAS"); Serial.println(TASnew); //some more code... }

if (digitalRead(5)== LOW){ TBSnew = millis(); Serial.print("TBS"); Serial.println(TBSnew); //some more code... } }

=================================================== anyone got any idea why or how this happens? It really messes up the whole system of measuring... :(

Any comments are appreciated.

Can it be that you damaged some input pins during previous experiments? Some conductive pieces or liquid on or under the board? Which voltage can you measure on the input pins, when you connect another one to Gnd? What if you connect external pullups to the input pins?

Hello DrDiettrich,

Can it be that you damaged some input pins during previous experiments?

maybe. I cannot completely exclude user stupidity... :roll_eyes: Some conductive pieces or liquid on or under the board? no, impossible. Got a wooden desk Which voltage can you measure on the input pins, when you connect another one to Gnd? not tested yet What if you connect external pullups to the input pins? what is external pullup? I do not know that pin mode??

What I have done is: I used a differend pin for pin 4 which seemed highly suspicious to be causing the error. Used the free pin 9 instead. Now it seems to work - so far no more "messages" from other unconnected pins. Will continue to monitor this. If I do not get further errors it simply was a damaged pin... which I really hope. Otherwise I am about to lose my mind and/or trust into the whole setup...

I will get back here again to confirm this or that state.

Thank you for your thoughts and reading!

So, here I go again…
I am having the same problem again - and most likely I am misunderstanding something really basic.
Guess it’s my fault and I damaged the Nano…

Starting from here: http://www.arduino.cc/en/Tutorial/InputPullupSerial
which is a very simple example of how to use the INPUT_PULLUP pin mode.
I understand this! :smiley:

But what happens if the push button of the tutorial above is replaced by some other mechanism to close the circuit? (you know tutorials are nice to learn… but reality is more complex, right?!)

Like in my case a contact that is itself a part of different (open) DC circiut. Since I am only touching one side of that different circiut I thought it will not influence my Arduino circuit. Am I wrong???

I attached a picture hoping it makes this more clear…

And if I am wrong: what is the solution? Is it to simply add a resistor? (and why??? is there an electrical current in that case??? From my understanding I could use a 12V car battery or a 100V battery contact to close the Arduino circuit - and it should not affect anything as long as I only touch one contact, either + or -)

Thanks again for any ideas - hopig this all doesn’t sound absolutely crazy.
Always ready to learn…!

If you connect the input pin to 12V you will damage it.

Your diagram does not show anything so it is hard to say if you are doing this. Any chance of a real diagram? That one does not make sense.

Connecting only one side of an external circuit will not work at all.

Electricity comes in various shapes. When a circuit shall work, it deserves 2 lines through which current can flow out and in again. Current can flow also between one contact and earth, when it finds an path of possibly high (but finite) resistance back into your device. You may notice this effect when you touch an electric fence or the wrong (hot) contact of a wall outlet. And AC or pulses can flow through non-conductive material, air or even vacuum, that's how capacitors and touchpads work.

But then there is wireless operation, where an electric field causes current flowing through a single line (antenna...). A cable connected to only one pin can catch such fields, most probably at 50 or 60 Hz, depending on your mains frequency.

And last not least there is static electricity, which can easily damage electronic devices. Simple movement of non-conductive material (plastic bags...) can cause a voltage of several kV, which finds its way as an electric spark as soon as you approach a differently charged device.

These effects can occur in any combination, and deserve different protection (grounding, isolation, shields...) to eliminate unwanted effects.

As G_M says that pic doesn't show anything, but...

I think we could help if you draw out the circuits properly. First draw that 12V circuit just by itself, then show exactly how and where you're touching it and the Arduino together.

Looks to me like you have that other circuit's -ve hooked to Arduino GND and then are touching its 12V to the Arduino digital pin? As G_M says, that's not good and is sure to release the magic smoke. Not sure though, from your pic.

You're configuring and writing to pin 1, but pin 1 is needed for serial communication to the PC.

Hello all,
first of all thank you for your help!

@dlloyd: I am not really writing to pin 1 in my program. I just tried to not have undefined states of pins and therefore configured all. But actually I am inputting only from 2-5 and outputting on 10-12. All others are unused.

@JimboZY and Grumpy_Mike
yes, I imagine it was hard to understand with that picture… all I wanted to say is that the pushbutton could be ANY kind of cable or metal, even one of a foreign circuit. In my case it is a piece of metal that is moving… and that metal itself again is part of another electrical circuit - but when it is contacting my pin2 and GND respectively pin4 and GND (and by that giving the signal in my Arduino) there is no electric current flowing in the foreign circuit - and that is why I think it must work!!

@DrDiettrich
indeed I had some of these thoughts in my head as well… but did not really want to accept this as an explanation… (because I thought this MUST work… damn it!

@all:
now, here is what I did and it is really NOT amusing me:

  • I unpacked a brand new(!) Nano

  • I soldered 5 cables to it: 1 for GND (left of D2) and 4 cables from D2 to D5

  • I stripped the insulation from the ends of the cables

  • I uploaded a simple test sketch which you can see below

  • I started the serial monitor

  • I connected the unisolated GND-cable in my right hand to the blank cables of D2 and D4 in my other hand (just running one blank over the other two, more or less randomly, expecting only signals from D2 and D4) - so I simulated the contact(s) in my real environment (I guess anyone did something like this before…)

  • and you can see from the attached screenshot of this experiment that the BRAND NEW NANO shows the same errors/problems that I had with the one that I already threw away because I really thought only a single pin wasn’t working fine. NO! It was not the pin - it is definitely the Arduino as this experiment clearly proves, doesn’t it?!!

  • The only 2 thoughts that are (currently) left in my head are:
    (and I always appreciate your comments on this)

  1. I did not tell yet (because I never that it might be of importance) that I use a cheap edition of Arduino Nano from ebay for 3$ - it says it is 100% compatible with the real one. Do you think this could be the reason?
    Anyone using cheap HW and ever experienced this (if recognized at all…)??
  2. I thought of the Input_pullup mode: could it be this is not the best way to go?
    Shall I rather use the INPUT pin mode and use external resistors? (it is more work…)
    Is there noone having issues with INPUT_PULLUP??

Well so far - pretty frustrating and frustrated…

  • Here is the code of my pin-testing-sketch -for anyone to reproduce if wanted… (again: I could omit some pin declarations but guess it’s not a bad idea to define all?)

/*
===== CREATED ON 2015, April 18 =====================================
===== VERSION 1.0 ====================================================
===== FUNCTION: Test sketch to check the pin functionality

*/

boolean pin2;
boolean pin3;
boolean pin4;
boolean pin5;
int n=0;
int i=0;
int aa=0;

int outputstyle = 1; //0=LEDs, 1=serial monitor

//==========================================================================================
//==========================================================================================
// the setup function runs once when you press reset or power the board
void setup() {

//=================
pinMode(1,OUTPUT);
pinMode(2,INPUT_PULLUP); //TAZ
pinMode(3,INPUT_PULLUP); //TAS
pinMode(4,INPUT_PULLUP); //TBZ
pinMode(5,INPUT_PULLUP); //TBS
pinMode(6,OUTPUT); // reserved TAZ lane2
pinMode(7,OUTPUT); // reserved TBZ lane2
pinMode(8,OUTPUT); //reserved RED F1
pinMode(9,OUTPUT); //reserved RED F1
pinMode(10,OUTPUT); //red LED
pinMode(11,OUTPUT); //yellow LED
pinMode(12,OUTPUT); //green LED
pinMode(13,OUTPUT); //onboard LED
//==================
digitalWrite(1,LOW);
//digitalWrite(2,LOW);
//digitalWrite(3,LOW);
//digitalWrite(4,LOW);
//digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW); //red
digitalWrite(11,LOW); //yellow
digitalWrite(12,LOW); //green
digitalWrite(13,LOW);
//======================

Serial.begin(57600);
}
//==========================================================================================
//==========================================================================================

void loop() {

if (digitalRead(2)== LOW){
pin2=true;
aa=0;
}

if (digitalRead(3)== LOW){
pin3=true;
aa=0;
}

if (digitalRead(4)== LOW){
pin4=true;
aa=0;
}

if (digitalRead(5)== LOW){
pin5=true;
aa=0;
}

aa = aa + 1;

if (aa>2000){
if (pin2==true){
n=2;
pin2=false;
}
if (pin3==true){
n=3;
pin3=false;
}
if (pin4==true){
n=4;
pin4=false;
}
if (pin5==true){
n=5;
pin5=false;
}

if (outputstyle=0){
//now blink n times
for (int i=1; i <= n; i++){
digitalWrite(10, HIGH);
delay(250);
digitalWrite(10, LOW);
delay(250);
}
}
else {
if (n != 0) {
Serial.print("Contact on pin no.: ");
Serial.println(n);
}
}
n=0;
}
}

The internal pullup resistors (mode INPUT_PULLUP, 20-50k) may be too high for your environment. Try connecting all input pins to Vcc by some lower resistors, down to e.g. 220 Ohm depending on your sensors connected later.

yes, I imagine it was hard to understand with that picture...

Yes and your explanation is equally unclear.

You must have the ground of your other circuit permanently connected to the ground of the Arduino. Then when this ground touches a pin with a pull up enabled it will signal on that and only that pin. The current flowing in the other circuit is irrelevant, it is the voltage that will kill you.

1) I did not tell yet (because I never that it might be of importance) that I use a cheap edition of Arduino Nano from ebay for 3$ - it says it is 100% compatible with the real one. Do you think this could be the reason?

No.

2) I thought of the Input_pullup mode: could it be this is not the best way to go? Shall I rather use the INPUT pin mode and use external resistors? (it is more work...) Is there noone having issues with INPUT_PULLUP??

No issue at all.

Well so far - pretty frustrating and frustrated...

Yes trying to help you is very frustrating. You do not explain what you are doing properly. Get it in your head that you are doing something wrong and we are trying to tell you what it is and are not listening.

You should use code tags when posting code. :stuck_out_tongue_closed_eyes:

Grumpy_Mike: You should use code tags when posting code. :stuck_out_tongue_closed_eyes:

Like this [code] code goes here [/code]

And draw a decent picture; I'm out until then.

  • I unpacked a brand new(!) Nano
  • I soldered 5 cables to it: 1 for GND (left of D2) and 4 cables from D2 to D5
  • I stripped the insulation from the ends of the cables
  • I uploaded a simple test sketch which you can see below
  • I started the serial monitor
  • I connected the unisolated GND-cable in my right hand to the blank cables of D2 and D4 in my other hand (just running one blank over the other two, more or less randomly, expecting only signals from D2 and D4)

I have run your test code on both an UNO and a Nano. I connected wires to pins 2,3,4,5 and ground. My jumper wire connections were either on a breadboard with the Nano which had male pins soldered, or into female headers on a UNO.

When I touched the ground wire repeatedly to the wires connected to pins 2,3,4 I could see the contact reported in the Serial Monitor. I never got a false report for pin 5. When I touched the ground wire to the pin 5 wire I could see it respond, but never when I was touching the other pins wires.

Can you proved a clear photo of your soldering to the 5 pins. I'd be suspicious of a poor connection and possible high impedance shorts to other pins or ground.

Also, try DrDeittrich's suggestion.

The internal pullup resistors (mode INPUT_PULLUP, 20-50k) may be too high for your environment. Try connecting all input pins to Vcc by some lower resistors, down to e.g. 220 Ohm depending on your sensors connected later.

Hello DrDiettrich, hello cattledog,

I have followed up on the idea of DrDiettrich: I switched to INPUT mode (instead of INPUT_PULLUP) and used external pullup resistors, starting at 10kΩ, then 5.6kΩ then 2.2k and finally 560Ω. With each level of lower resistor value I got less false positives using the the test sketch. So this was an improvement but still not right... as soon as I have the 200Ω resistors at hand I will also try them.

cattledog, I experienced the same behaviour (while making the tests with the resistors I just mentioned) that I NEVER got a false positive with pin 5. Pins number 2,3 and 4 were reporting other pins variously (i.e. they reported false positives). I will send a foto of my test setup in a few hours (I am not at home right now). But wouldn't it be an incredible coincidence that we both see the same behaviour on pin 5??

cattledog, I experienced the same behaviour (while making the tests with the resistors I just mentioned) that I NEVER got a false positive with pin 5. Pins number 2,3 and 4 were reporting other pins variously (i.e. they reported false positives). I will send a foto of my test setup in a few hours (I am not at home right now). But wouldn't it be an incredible coincidence that we both see the same behaviour on pin 5??

BVeyron-- To be clear, I never saw any false positives reported with any pin/wire that I contacted with the grounded wire. The INPUT_PULLUP mode worked reliably for me with your sketch.

so, I have added pictures of the test setup.

I have other news:
I have been playing around with the sketch and found out that if I reduce my “delay to display” (in the code it is the variable “aa”) it’s getting better. So, I mean this part here:

...    
aa = aa + 1;
   
    if (aa>2000){
      if (pin2==true){
        n=2;
        pin2=false;
      }
      if (....

When I replace “if (aa>2000){” with “if (aa>0){” it seems there are no more errors - at least I could not see any during the tests. This is completely unlogical to me.
Why can a switch be appearing “closed” simply because I remove a counter and let the sketch spam the serial monitor?

I then rewrote the pin test function and went back to 10k resistors.
On the weekend I will test it again with INPUT_PULLUP mode and the new test sketch which is copied below -if anyone can use it (??).

@cattledog
sorry, I misunderstood you. And thank you for testing it!

/*
  ===== CREATED BY  B. Veyron ==========================================
  ===== CREATED ON  2015, April 23 =====================================
  ===== VERSION 2.1 ====================================================
  ===== FUNCTION: Test sketch to check the pin functionality
  ======================================================================  
  ======================================================================
  ======================================================================
*/

int StatePin2;
int StatePin3;
int StatePin4;
int StatePin5;
int LastStatePin2;
int LastStatePin3;
int LastStatePin4;
int LastStatePin5;

int delay_ms = 2;

  
//==========================================================================================  
//==========================================================================================
void setup() {
  
  //=================
  pinMode(1,OUTPUT);
  pinMode(2,INPUT);  //TAZ  yellow
  pinMode(3,INPUT);  //TAS  white
  pinMode(4,INPUT);  //TBZ  brown
  pinMode(5,INPUT);  //TBS  green
  pinMode(6,OUTPUT);      // reserved TAZ
  pinMode(7,OUTPUT);      // reserved TBZ
  pinMode(8,OUTPUT);     //reserved RED F1
  pinMode(9,OUTPUT);     //reserved RED F1
  pinMode(10,OUTPUT);    //red LED
  pinMode(11,OUTPUT);    //yellow LED
  pinMode(12,OUTPUT);    //green LED
  pinMode(13,OUTPUT);    //onboard LED
  //==================
  digitalWrite(1,LOW); 
  //digitalWrite(2,LOW);
  //digitalWrite(3,LOW);
  //digitalWrite(4,LOW);
  //digitalWrite(5,LOW);
  digitalWrite(6,LOW);
  digitalWrite(7,LOW);
  digitalWrite(8,LOW);
  digitalWrite(9,LOW);
  digitalWrite(10,LOW);  
  digitalWrite(11,LOW);   
  digitalWrite(12,LOW);    
  digitalWrite(13,LOW);
  //======================
  
   Serial.begin(57600);
}
//==========================================================================================  
//==========================================================================================

void loop() {   
          
    StatePin2 = digitalRead(2);
    if (StatePin2 == 0 && StatePin2 != LastStatePin2){    
      Serial.print("State of pin no 2 (0 means contact): ");
      Serial.println(StatePin2);
      delay(delay_ms);
    }
    LastStatePin2 = digitalRead(2);
    
    StatePin3 = digitalRead(3);
    if (StatePin3 == 0 && StatePin3 != LastStatePin3){    
      Serial.print("State of pin no 3 (0 means contact): ");
      Serial.println(StatePin3);
      delay(delay_ms);
    }
    LastStatePin3 = digitalRead(3);
    
    StatePin4 = digitalRead(4);
    if (StatePin4 == 0 && StatePin4 != LastStatePin4){    
      Serial.print("State of pin no 4 (0 means contact): ");
      Serial.println(StatePin4);
      delay(delay_ms);
    }
    LastStatePin4 = digitalRead(4);
    
    StatePin5 = digitalRead(5);
    if (StatePin5 == 0 && StatePin5 != LastStatePin5){    
      Serial.print("State of pin no 5 (0 means contact): ");
      Serial.println(StatePin5);
      delay(delay_ms);
    }
    LastStatePin5 = digitalRead(5);
}

OK, I know this is an old post but - I have a momentary switch from pin 3 to ground, INPUT_PULLUP. This toggles a red/green bi-colour (on/off indicator) LED (PIN 7 & 6). A similar switch on pin 5 toggles 2 LEDS on pins 2 & 4 - I have no problem with the switch on pin 5.

If I bring a finger/screwdriver tip close (not even touching) to the pin 3 switch wire and either of the adjacent LED wires on pin 2 or 4 I get false switching.

If I bring a finger/screwdriver tip close (not even touching) to the pin 3 switch wire and either of the adjacent LED wires on pin 2 or 4 I get false switching.

Yes, and your point?

I suspect you think that should not happen, but the pull up resistors are only weak pull up resistors of between 30 to 50K, and you might have a strong interference environment.

Try it with an external pull up of 4K7 and if that still gives trouble try a 1K8.

In the old post the op writes:
pinMode(2,INPUT_PULLUP); // which enables the pullup
Then he writes:
digitalWrite(2,LOW); // which disables the pullup