Author Topic: Issues with arduino switch and mystery interference/how to filter interference?  (Read 4050 times)

0 Members and 1 Guest are viewing this topic.

Offline XOIIOTopic starter

  • Super Contributor
  • ***
  • Posts: 1625
  • Country: ca
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 value

void setup()
{
  pinMode(A1, OUTPUT);      // sets the digital pin 13 as output
  pinMode(2, INPUT);      // sets the digital pin 7 as input
}

void loop()
{
  val = digitalRead(2);   // read the input pin
  digitalWrite(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 so

GND  ---/\/\/\--/\/\/\---| |--- arduino pin 2
GND----/\/\/\--/\/\/\---LED---arduino pin A1

The /\/\/\'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.
« Last Edit: March 24, 2016, 07:03:03 pm by XOIIO »
 

Offline XOIIOTopic starter

  • Super Contributor
  • ***
  • Posts: 1625
  • Country: ca
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.

Offline alsetalokin4017

  • Super Contributor
  • ***
  • Posts: 2055
  • Country: us
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 see
https://www.arduino.cc/en/Tutorial/Button
for the correct wiring for a button or switch.
The easiest person to fool is yourself. -- Richard Feynman
 

Offline Rick Law

  • Super Contributor
  • ***
  • Posts: 3445
  • Country: us
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.

Or, you can add a pull down resistor to make sure it is not floating.

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 see
https://www.arduino.cc/en/Tutorial/Button
for the correct wiring for a button or switch.


I 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.
 

Offline XOIIOTopic starter

  • Super Contributor
  • ***
  • Posts: 1625
  • Country: ca
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 see
https://www.arduino.cc/en/Tutorial/Button
for the correct wiring for a button or switch.

I'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.

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.

Or, you can add a pull down resistor to make sure it is not floating.

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 see
https://www.arduino.cc/en/Tutorial/Button
for the correct wiring for a button or switch.


I 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.

It 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.

Offline Seekonk

  • Super Contributor
  • ***
  • Posts: 1938
  • Country: us
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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf