4

I want to use a switch for my Arduino using digitalRead(), but I keep receiving random inputs. Whenever I connect energy to that pin, it always returns HIGH, but otherwise it goes completely random.

I have tried using a resistor for the input pin, but that didn't seem to help. I am using an Elegoo UNO, and the input pin I am using is pin 2. Here's my code in case you need it:

const int switchPin = 3;

void setup() {
  // put your setup code here, to run once:
  pinMode(switchPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(switchPin) == HIGH){
    Serial.println("on");
  }

  else{
    Serial.println("off");
  }
}
4
  • 4
    No pulldown resistor on that pin then...?
    – Majenko
    Mar 4 at 20:48
  • 6
    Let me guess: You are not using an external pullup or pulldown resistor (relatively large value resistor from the pin to eiter Vcc or ground). When nothing is connected to a pin it is floating (basically an antenna). A pullup/down resistor pulls the pin to a fixed value, when nothing else is connected. You can use an external resistor, or you can use the internal pullup resistor by doing pinMode(switchPin, INPUT_PULLUP)
    – chrisl
    Mar 4 at 20:49
  • 3
    Does this answer your question? Digital Pin State - Some 1, Some 0
    – chrisl
    Mar 4 at 20:52
  • How did you connect the resistor for the input pin? Mar 6 at 16:46
24

You're getting noise pickup on the input pin when the button is not pressed. You need a pull-down resistor, about 10 kΩ to ground, to quiet the noise when the switch is open.
Or even easier, use the built-in pull-up resistor in the Arduino, which is ~10 kΩ to +5 V, wire your button to ground instead of +5 V, and reverse the if tests to test for logic LOW instead of HIGH, to detect a button press. You'd change your pinmode statement to:

pinMode(switchPin, INPUT_PULLUP);  // engage the input pullup

and your test to:

 if (digitalRead(switchPin) == LOW ){ // Negative logic

No additional (external) hardware is needed!

3

Your code mentions pin 3 while you describe using pin 2. Perhaps that has to do with your error.

Otherwise, check the following things:

  • do you use long cables or is it near strong magnetic or static objects.
  • for your code since you only read if it is high, see if the button is connected to 5 V and pin 2 or 3 whichever one you use

You can try adding a resistor between your I/O pin and the ground.

Another test I prefer to do is:

Take a working jumper cable, test it with a voltmeter, or see if you can make an LED light up when connected to it (use the Arduino 5 V, ground and a resistor for this (resistor to not let your LED explode like that zeppelin).

With the working jumper cable or breadboard cable, connect one end to the pin you plan to use. Then run the program and connect the other end to ground, then 5 V, and then ground and so forth to see if it responds well.

This is based on an Arduino Uno. If you use another type of Arduino, make sure to use the proper voltage pin. You can always add a resistor between the wire and the Arduino for extra safety.

3

Here is what you can do

  1. Check if the switch is connected to the defined input pin. The Arduino starts form pin 0 and sometimes we can misread while connecting.

  2. Check if the serial monitor has same baud rate as defined in the program.

  3. Check the voltages around switch using a multimeter.

  4. Still not getting a solution? Attach a pull-down resistor.

  5. Still not getting a solution? Invert the logic and current flow as suggested by JRobert.

0

Few things that can be checked and used.

  1. If you are using a normal push button then check the connections properly as two of the pins are internally connected two each other. So we need 2 pins that are not connected so check it with DMM.enter image description here

  2. You need a Pull-UP or pull-Down resisters attached to the input pin.

    The value of a resistor depends on your requirement like if you have long wires or more noise you need a strong pullup or pulldown resister like 1k. And the best way is to test it out first with an internal pull-up resistor which is considered a week pullup and Atmega328p has its value of around 20k. pinMode(switchPin, INPUT_PULLUP);

  3. As TeD has stayed check that you are using the same digital pin in your hardware and code.

  4. And last a simple counter type debouncing logic will help to denounce the button input signals. as shown in the below code.

#define switchPin 2
byte debounce_counter = 150;

void setup() {
  pinMode(switchPin, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  if (digitalRead(switchPin) == HIGH) {
    debounce_counter++;
  }
  else {
    debounce_counter--;
  }

  if (debounce_counter >= 240)
  {
    debounce_counter = 150;
    Serial.println("OFF");
  }
  else if (debounce_counter <= 60)
  {
    debounce_counter = 150;
    Serial.println("ON");
  }
}

You can also check other methods and libraries for debouncing the input signals which uses the millis function.

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.