Something strange happening with the digital pins in my Arduino UNO

Thread Starter

Lucky-Luka

Joined Mar 28, 2019
101
I have noticed something strange happening in my simple circuit: a pushbutton with a 10kohm pull down resistor that turns on an LED when it's pressed.
My Arduino loop cycles at 100Hz more or less.
Yellow probe connected through the yellow cable to the 10 kOhm pulldown resistor.
Green probe connected through the green cable to the dgt output pin 4. This output signal has a freq of 50Hz. I had this pin toggling just to understand how fast my loop is.
10X probes
Trigger on the yellow signal.
The 50Hz yellow signal disappears when I disconnect the green probe or I cancel the last two instructions in the code loop.
I can't understand what's going on.
Cheers
 

Attachments

sagor

Joined Mar 10, 2019
512
Not sure, but it looks more like a "ringing" of a signal line. Your code shows you are writing to the LED every 10ms, which is 100Hz, regardless if the button is pressed or not. I would suggest your code save the last state of the button, and if it has not changed, do not write to the LED. Only write to the LED when the button state changes (and you should include a button debounce). Your button may be "bouncing" the contacts, causing extra pulses to the LED.
 

djsfantasi

Joined Apr 11, 2010
7,921
When you setup the LOOP pin, it defaults to a low state.

When you remove the marked code, it will always be low or at ground. if you remove the cable, it will always be at ground.

Doesn’t this explain your issue?
 

sagor

Joined Mar 10, 2019
512
Looking at the code, when the button is always low (not pressed), you are still writing to the LED pin every 10ms, all the time. Your routine for digitalwrite LOOP seems to flip the state on and off every 10ms, meaning the LED is on for 10ms, off for 10ms. That = 50Hz for a full square wave cycle. I may be reading that wrong, I'm not a C expert.
But like I said before, store the previous state of the button, and if it has not changed with the next read, skip any I/O writes. Only if the button state changes from previous state do you write anything to the LED. LOOP indicator is flipping state too fast to see it anyway and no need for it...
 
Top