Arduino input seems to be randomly toggling?

Hi there, I'm new to Arduino and the software so please don't assume any knowledge at this end :-)

I have a 5VDC across a normally-open switch to a terminal. The terminal is linked via a 10K Ohm resistor to the negative of the 5VDC supply and to the input on the Arduino. (The negative of the 5VDC supply is also linked to the GND pin on the Arduino).

I have programmed the pinMode to be "input" in the setup function.

The input is meant to toggle a relay when it is High, however I've found it is toggling the input randomly. Even when there isn't 5VDC on the actual switch?!

I've tested the resistor and it's working.

I've put a meter across the input and the -5VDC and I get around 0.01VDC. This seems to stay the same even when the relay is toggling so it is like the Arduino is switching itself?

I have a bit of code like this.....

   if(digitalRead(XButton) == HIGH){
Serial.print("Read an input from XButton");
     Xmode();
   }

Even without 5V on the switch I am still getting random "Read an input..." text appearing in my serial monitor.

My Arduino has both a 12VDC supply via the DC jack and the USB connected but I read that didn't matter?

For some reason, if I unplug the 12VDC supply the problem seems a bit worse? But this could be just a coincidence?

Anyone any ideas on what could be happening here? The obvious thought is that it's noise but there are no mains cables or anything near it?

Any ideas would be very gratefully received. Thanks :-)

uptown47: The input is meant to toggle a relay when it is High, however I've found it is toggling the input randomly. Even when there isn't 5VDC on the actual switch?!

if the NO switch is between the Arduino pin and ground it will make the pin go LOW when you press the button.

When an input pin is floating it can easily switch to either state by picking up electrical interference. This is why you want to use a pull up or pull down resistor, which will put the pin in a known state, rather than floating. Helpfully, there are pull-up resistors built into the microcontroller on your Arduino. To activate them you just need to change:

pinMode(XButton, INPUT);

to:

pinMode(XButton, INPUT_PULLUP);

Then you know the pin will always be HIGH when the button isn't pressed and LOW when the button is pressed. The internal pull-up resistor is 20-50 kOhm so your 10 kOhm resistor to ground should overpower it but that may be a little bit higher resistance than ideal.

Hi Pert

Thanks for the post. The NO switch is between the Arduino pin and +5V.

I did try adding INPUT_PULLUP to the button but it made it go crazy! It was constantly on/off.

Any other ideas?

Thanks again. Didn't know about the pullup functionality. It's a good thing to know about. :-)

OK, if you want the switch between the pin and 5V then you would need to either use an external pull-down resistor or change your wiring so the switch is between the pin and ground and then change your code to reflect the logic inversion. It may seem strange to have an on state associated with LOW but this is actually quite common in electronics, as evidenced by the microcontroller having internal pull-ups but no internal pull-downs. Maybe I misunderstood your post and you’re already using the 10 kOhm resistor as an external pull-down resistor. In this case setting the pin mode to INPUT instead of INPUT_PULLUP is indeed correct and 10K is a reasonable resistor value.

How long are the wires to your switch?

pert: OK, if you want the switch between the pin and 5V then you would need to either use an external pull-down resistor or change your wiring so the switch is between the pin and ground and then change your code to reflect the logic inversion. It may seem strange to have an on state associated with LOW but this is actually quite common in electronics, as evidenced by the microcontroller having internal pull-ups but no internal pull-downs. Maybe I misunderstood your post and you're already using the 10 kOhm resistor as an external pull-down resistor. In this case setting the pin mode to INPUT instead of INPUT_PULLUP is indeed correct and 10K is a reasonable resistor value.

How long are the wires to your switch?

Hi Pert, thanks for getting back to me.

I think I've managed to suss the problem but not got a solution yet.

The issue is indeed 'noise'. The system is a CNC machine that I'm building.

I thought I'd ruled out 'mains' noise by switching off the mains at my control panel but, it transpires, that the relay that feeds mains to the stepper drivers was stuck in an 'on' position. Therefore there was mains to the stepper drivers.

If I run the stepper motors the Arduino senses inputs like crazy (I've set up the software to serially output which inputs are detected). However, if I set all the motors in an "off" position the spurious inputs are very seldom.

If I kill the mains to the stepper drivers then the spurious inputs cease.

I just need to work out now how to disperse the noise?

Thanks again for all your help :-)

You can use shielded cable with the shield grounded to attach the Arduino to the switch. You can use a lower value pull-down resistor.

A bit off topic, but you may find this of interest if your input is picking up that much noise: https://www.digikey.com/en/articles/techzone/2012/apr/protecting-inputs-in-digital-electronics

Hi,

I'm still not sure how you have your switches wired.

Can you post a circuit of how they are wired to the Arduino please, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :)

Doing what you can in hardware to reduce the noise and eliminate the random switching is important. Beyond that, you can use software techniques which require a stable input reading for a period of time before accepting the input.

Depending on the application, there are very simple blocking techniques which require two sequential readings separated by an appropriate delay, or more complex non blocking forms which allow the program to do other things while checking if an input is stable for some number of milliseconds.

This is somewhat different than the more common button debouncing techniques which immediately accept a state change, but then use a lock out period before accepting another input.