Issues with arduino switch and mystery interference/how to filter interference? - Page 1
Forum section: electronics / beginners

author: XOIIO
author_rank: Super Contributor
timestamp: March 24, 2016, 06:54:38 pm
content: Hey all, so I'm having a small issue with some of the code for my temperature sensor (which lives on after a full re-wiring, and is looking better too)Now, I had this issue before, which made me change from a momentary button to a toggle, and then re-wire it. For testing I am using the script as basic as it comes, basically if the toggle is high, set that to the led output pin, if low, the same.int val = 0; // variable to store the read valuevoid setup(){pinMode(A1, OUTPUT); // sets the digital pin 13 as outputpinMode(2, INPUT); // sets the digital pin 7 as input}void loop(){val = digitalRead(2); // read the input pindigitalWrite(A1, val); // sets the LED to the button's value}Now, the issue is, it doesn't work... on battery, mostly.When plugged into my laptop or a grounded power supply it works, sort of, but if my hand gets near the right wire, or near the switch body (or sometimes touching the metal on the switch itself) it will randomly turn the light on or off, or make it flicker/dim.This only happens when it's on laptop or psu power, but when I have it on battery power, it doesn't work at all. Nada. No switch, no wire touching, no magic invisible barrier around certain parts to trigger it.Except, as I wrote this and tried it again I did get a little bit of activity out of it, but it doesn't seem to react to the toggle at all. I can't figure out for the life of me what's causing this.Circuit is basically like soGND ---/\/\/\--/\/\/\---| |--- arduino pin 2GND----/\/\/\--/\/\/\---LED---arduino pin A1The /\/\/\'s are resistors, the switch being -| |-. The switch has two 5.1k resistors since I didn't have a 10k handy (and they are around 10.1 combined), the LED has whatever values it needs, can't recall, but it works fine when turned on/off without depending on the switch.I could really use some insight on this, perhaps I could use .1uf ceramic caps to ground to get rid of the noise that is causing this, although I don't think that would explain why it pretty much ignores the switch entirely.I mean sure, I have made a super low cost proximity sensor, but that's the opposite of what I need.edit: sitting down with other wires being the problem eliminated (I figured it was the lcd wires), I realize it only happens with the switch open, and I need to flip the variable to turn the LED on, be back after some quick tweaking. Hopefully it should turn the LED on when I have the switch on, and then when it's off it will act up, then I just need to know how to stop that noise from getting to the arduino.

author: XOIIO
author_rank: Super Contributor
timestamp: March 24, 2016, 07:18:24 pm
content: You know, it was as simple as using digitalwrite to set the input pin to high as a default state, I guess pulling it down doesn't cause any damage, problem solved.Finally about to finish this thing, I've been going crazy.

author: alsetalokin4017
author_rank: Super Contributor
timestamp: March 25, 2016, 12:18:13 am
content: 1. Your comments don't correspond with the actual pin assignments in the code.2. Why are you using an Analog pin for a digital output? While this is "legal", it's a bit strange.3. You are not implementing the button (or toggle) switch correctly in the hardware.Please seehttps://www.arduino.cc/en/Tutorial/Buttonfor the correct wiring for a button or switch.

author: Rick Law
author_rank: Super Contributor
timestamp: March 25, 2016, 12:36:47 am
content: Quote from: XOIIO on March 24, 2016, 07:18:24 pmOr, you can add a pull down resistor to make sure it is not floating.Quote from: alsetalokin4017 on March 25, 2016, 12:18:13 amI beg to differ...As I understand it, there no reason to avoid using an analog pin for digital. They are "analog" just because you can tie an ADC to it. If you don't need another ADC and you don't switch the ADC for that pin ON, that "analog" pin is just another run of the mill digital IO pin.

author: XOIIO
author_rank: Super Contributor
timestamp: March 25, 2016, 07:06:49 am
content: Quote from: alsetalokin4017 on March 25, 2016, 12:18:13 amI'm using analog pins because I have run out of digital pins, and I have seen previous examples where you can simply use pull down/pull up resistors, probably because they were using two button switches as I did.Quote from: Rick Law on March 25, 2016, 12:36:47 amIt would have to be a pull up resistor since the pin is looking for a low signal, and I may do that, but for now the issue is resolved.I've actually used switches like this before without floating issues, so I'm not really sure why it wasn't an issue then, at least I know now.

author: Seekonk
author_rank: Super Contributor
timestamp: March 25, 2016, 07:58:34 am
content: A floating pin is just on the edge of either state. When your project is grounded, the input wire becomes a better antenna and the noise/voltage of power lines and radio signals is. Any input pin should have a pull up or pull down resistor to clearly define the state. A small capacitor to ground is also suggested for switches to get rid of some point bounce issues. Just adding a capacitor to the pin would likely make the noise problem go away as it would shunt the pin. But it would add a new problem. The pin would stay in the last state till the cap either charged or discharged. With low leakage currents that could take a while. Bottom line is an input pin always has to have a pull up or down to define it for reliable operation.Another bug/feature is with outputs. If you don't define an output pin, you can still output while in that clock cycle. Drive a FET with no pull down gate resistor and the micro will charge the capacitance of the ET gate and it will stay on after being told to turn off.