Trouble with digital Input from switch on Arduino Mega

Hi all. I'm new to the Arduino and any work with electronics beyond repairs. I'm having issues right now with a digital input to my mega from a Ulincos U16F1 push button switch.

Here is a wiring diagram for the switch though my setup has one difference I'll point out shortly.

Here is the wiring I have. The + 5v power on the mega. The grounding lead is hooked into the 5v ground on the Arduino as well is the ground for the 9v power source for the rest of the project. The NO lead is wired into pin 33 as an input and the + pin for the button LED is hooked up to pin 34 on the mega as an output pin to manually control when the LED comes on.

Here is the issue. As setup the switch only works at random and the the state will also change at random on it's own. However if I hook my multi-meter up to the input line leading to pin 33 the switch works perfectly. If I measure the voltage of the pin 33 input without it being hooked up to the switch it measures ~0.5v constantly. I don't know if that is relevant or not. The other input pins I have setup on the mega all measure ~0v. I have tried moving the input pin but that hasn't helped at all. I have tried adding a 100kohm pulldown resistor to the - line on the switch with no change in results.

I have also tried just removing the connection for the switch LED completely and also wiring exactly as in the setup diagram all with the same results. I also get the same results where I using debouncing on the input pin or not.

Any suggestions would be greatly appreciated.

Sounds like you need to read about pull-up / pull-down resistors.

Thanks, I’ll have to try implementing the pull up resister. I had read about the pull down resistor in the tutorial projects I did, there was no mention of the pull-up resistor I remember. Always good to find out something new.

The Arduino boards have an internal pull-up resistor that can be activated via pinMode(pin, INPUT_PULLUP).

For more information, see:

Well thanks for the ideas but that doesn't seem to be the issue.

I first tried adding a pull up resistor both to the power supply into the switch and then on the line leading from the switch to the input pin (up to 1.8M Ohm) with no results. I then tried playing with stronger pull down resistances on the ground for the switch and also the ground for the input pin on the mega (again up to 1.8M Ohm). I also just tried using the INPUT_PULLUP mode for the input pin and changing the switch logic around in the code. Again no change in behavior.

The only way I can get the switch to work is by holding the jumper to the input while I'm grounded, or grounding it by connecting the + lead on my multimeter to the input jumper and the - lead to ground.

The only other thing I have noticed is that if I'm not grounded just placing my fingers within 1.5 - 2 inches from the jumper leading to the input will cause it to start switching on and off rapidly.

Again thanks for the input. If you have any other ideas I'd love to hear them.

Thanks

seirsan: up to 1.8M Ohm

Higher resistor values means a weaker pull-up. A very high resistance like 1.8M Ohm will be very susceptible to interference. A common value is 10K. I think the internal pull-ups are something like 50K and are sometimes not strong enough for an electrically noisy environment or when you have a long switch wire that acts like an antenna.

seirsan: if I'm not grounded just placing my fingers within 1.5 - 2 inches from the jumper leading to the input will cause it to start switching on and off rapidly.

That's exactly the sort of behavior we expect from a floating pin. It sounds like you might not be connecting your external resistor correctly. You can see how it should be connected here: https://www.arduino.cc/en/Tutorial/Button

The diagram there is the same one I used as reference with the pull down resistor. I started with a 10k Ohm resistor then went lower, and then higher. The lowest I tried I think was around The lowest I tried was I think around 30 Ohm and several points between that and 10k. I just tried 230 Ohm and 10k as a pull down and pull up on both the switch 5v power and the jumper to the input pin.

The other frustration is that if I use one of the simple switches that came with the kit (like the one in the diagram you linked) it works fine. It's when I hook in the switch I want to use in the final project things start getting buggy.

Thanks for you patience. I'll see if I can get a diagram drawn up of the setup here, but the one you linked pretty much covers it exactly. The switch just has another pin on it connected to an output on the mega for an LED on the switch, but that seems to work just as it should.

I'll also take another look at resistances between 10k and 100k.

Hi, Can you draw a complete circuit diagram showing ALL your connection including ALL your power supplies and LED and the sensor switch you have shown above, including pin labels.

Please do this with pen and paper and post a picture of it please.

Don't try and use computer CAD unless you have a good one that will provide all the above requirement.

Thanks.. Tom.. :)

It would be informative to know what kind of meter you used (digital or analog) and the exact points you connected the probes to when the switch worked properly as seen by the Mega input.

Currently I’m using a Proster VC99 digital multimeter.

I’ve attached a diagram of the of the current circuit. Eventually everything will run off the same power supply but for the moment I’m running the Arduino off the USB to the computer and the 9v circuit is being run off a variable power supply.

In the schematic the issue is somewhere along the connection from pin 3 on the switch to D39 on the mega. The switch will work perfectly if I attach my multimeter anywhere along that connection. Pins D38 and D40 are set to OUTPUT while D39 is an INPUT. The function of this circuit right now is a simple full on/off for the LED strips and the switch LED.

If its is more usefull here a schematic with everything currently hooked up to the Arduino. Also there are more ground pins on the Mega than in the schematic right now those are all hooked up to a common ground with the 9vdc power supply.

Hi,
Ops ciruit;
Arduino Schematic.jpg
Tom… :slight_smile:

Hi,
I think you will find the BLUE edit will fix your switching problem;
fix1.jpg
I’m not sure how you want your switch LED to work?
If D40 is an output the switch LED is the wrong way around as it cathode is connected to +5V.

Tom… :slight_smile:

Thanks Tom, but unfortunately still the same results. The only difference I’m seeing now is that if I’m not grounded and I get within a foot of the arduino and breadboard the switch starts strobing. It started doing that today with the original configuration as well too (the air is a bit dryer).

However if I use the simple switch wired exactly like the arduino tutorial button as pictured in the attachment it works perfectly fine regardless if I’m grounded or not. Same resistance same connections and everything. I think I’m going to get a different model switch to try out and see if that makes any difference. I’ve already tried a different switch of the same model with the same results.

Thanks for the idea though. Using the other pin there as the ground is one of the few permutations I can think of I hadn’t tried yet.

Simple Switch.jpg

Well it looks like I found the solution. The pull up transistor was the right thing, but it turned out to also be only half the issue. It seems for some reason the line of DIO pins on the Arduino Mega also were not grounding properly. After playing around with the configuration of the grounding pins on the breadboard it started working. I'm not sure if also making a difference is that for these tests I was powering the Arduino from the same 9vDC power source as everything else instead of through the USB. Not sure what the issue totally was since in theory it should have worked.

It is still not perfect but I think the remaining issue is I need to modify the timing on the debounce code some. Turns out this switch really bounces bad based on the profiles of it I've gotten on the O-scope.

Thanks all for your great input!

Breadboards are notorious for having unreliable electrical connections. Sometimes you'll need to start jiggling wires around just to solve a problem. For this reason, I never use them for finished projects but I do still like them for prototyping circuits and just playing around.

Another common problem with breadboards is that some of the long ones have a split in the middle of the power rails on each side. This is indicated by a break in the red and blue lines.