6
\$\begingroup\$

The other day I wrote a simple arduino script to switch on an LED when a pushbutton is pressed. Without connecting the pushbutton, the circuit created a make-shift analogue sensor that varies the LED brightness according to the distance you move your hand towards the input pin.

Dumbfounded, I rewired the circuit several times (also with different arduinos), and got the same results.

The LED was plugged in (no resistor), assigned as a digital output (13). A loose cable was plugged into a digital pwm pin (7) as input on the arduino mega 2560.

My understanding of electronics doesn't stretch far into electric/magnetic fields, was wondering if there was a rational explanation for this?

\$\endgroup\$
4
  • \$\begingroup\$ Can you exactly post what you made? Circuit schematic and good quality photos would be welcome. Code too. \$\endgroup\$
    – AndrejaKo
    Mar 4, 2013 at 20:38
  • \$\begingroup\$ Adding on to what AndrejaKo said, a video demonstrating the LED brightness change could be helpful, too... \$\endgroup\$
    – apnorton
    Mar 4, 2013 at 21:34
  • \$\begingroup\$ Unpredictable things happen when digital inputs are left floating, and that would be my first suspicion. Also, you could be changing your viewing angle on the LED when you reach in with your hand, and it just looks dimmer or brighter according to your viewing angle. In any case, we don't have near enough info to answer the question, which would require at least the code running on the unit, probably a diagram, and even more helpful would be an oscilloscope screen capture on the LED output pin. \$\endgroup\$ Mar 4, 2013 at 22:09
  • \$\begingroup\$ Certainly, I'll remake it and add photos+code in due course. Definitely not the viewing angle, I called a few people around the office - were just as perplexed as I was. \$\endgroup\$
    – rom
    Mar 4, 2013 at 22:16

1 Answer 1

12
\$\begingroup\$

There's a rational explanation - no ghost in the machine here ;-)

My guess from your description is that you don't have a weak pulldown resistor on your button input pin, so when it's not pushed the pin is left floating. Since a CMOS input is incredibly high impedance it will easily pick up stray electric fields (a MOSFET or JFET with a floating gate and LED on the drain makes a good static electricity detector) and randomly hover between on and off.

When you bring your hand near, it will make the field stronger and the LED will oscillate on and off, spending more time on than off the closer you get, probably due to some rectification effects with the protection diodes, and higher portion of time with input spent above threshold. It will probably be around the mains frequency if you check it with a scope whilst you move your hand near.

An interesting thing to try is to grab a mains cable with one hand (a normal insulated one) and put your other hand near the pin. Let go of the mains cable (or move your hand closer/further away) whist keeping the other hand static and watch the effect, it should be similar.

To fix it (assuming I'm right) put a 100kΩ (or similar high value) resistor from button pin to ground. Or if your microcontroller has internal pulldown options, then activate this on the button input pin.
The thing to learn from this is never leave an input floating. Even the unused pins should really be set to output and driven to the positive rail or ground. Although it's unlikely they will cause the micro to crash, the main reason for doing this is due to the fact that a floating input can consume unnecessary extra current if it's continually switching between states.


Experiment

Based on the comments below, I just ran a quick test using a floating input (with a wire connected to it to touch/put hand near) and an LED. I got the symptoms you mention, an the output oscillated at the mains frequency here (50Hz) as predicted.
Depending on how close my hand was (or how hard I pressed the wire when probing - even the 10MΩ input impedance of the probe turned the LED off easily when the wire was not touched, compared to without the probe) the duty cycle varied a bit and also I got some flickering. So the LED went from off/dim/on as I pressed the wire harder (or moved hand closer without probe)

Just in case anyone wants to try it themselves, I used a PIC16F690, with this code (in MPLABX with the XC8 compiler) but any microcontroller with a floating CMOS input should do the same:

 #include <xc.h>
// Turn the Watchdog Timer off
#pragma config WDTE = OFF

int main(int argc, char** argv) {

    TRISBbits.TRISB6 = 0; // LED Output pin
    TRISBbits.TRISB7 = 1; // Floating pin input

    while(1) // Infinite loop
    {
        if(PORTBbits.RB7 == 1)
        {
            PORTBbits.RB6 = 1;  // Set RB6 pin to logic high (5V)
        }
        else        
        {
            PORTBbits.RB6 = 0;  // Set RB6 pin to logic low (0V)
        }
    }

}

Anyway, here are the scope clips, the yellow trace is the LED output, and the red trace the 10x probed input.

LED Bright:

LED bright

We can see the output is almost 50% duty cycle and the input appears to be a rectified mains frequency sine like waveform (the protection diodes clip the negative swing)

LED Dim:

LED Dim

Less of the input waveform is above the input threshold here, so the duty cycle on the output decreases and the LED appears dimmer.

\$\endgroup\$
7
  • \$\begingroup\$ A slight comment, I would expect that the LED is not oscillating on and off, but rather the floating gate voltage on the CMOS is changing, due to the electric field changing when you put your hand (a giant body of water, and an insulator) near the wire. On a different day, with different humidity, I would expect that your results would change, in that the light is always on, or always off. Either way, the fix is still the same. \$\endgroup\$
    – Reid
    Mar 5, 2013 at 3:00
  • \$\begingroup\$ @Reid - yes, you're probably right - I did infer the floating gate voltage at this with "input impedance... randomly hover..." and "more time on than off... rectification...", the mention of the MOSFET static detector, etc. I'm not suggesting there is any "real" oscillation (of the positive feedback type) taking place. It depends on what the field is doing whether the output is turning on and off or just on - but e.g. if it is switching on and off the most likely frequency is the mains, and 50/60Hz will look continuously on to the eye anyway... \$\endgroup\$
    – Oli Glaser
    Mar 5, 2013 at 3:19
  • \$\begingroup\$ ...and if the light is getting brighter as the hand moves nearer, it does suggest some fluctuation (a better term maybe than oscillation) over/under the threshold of the gate voltage, with the on time getting longer. Either way the fix is the same as you say, but I might do a quick test here for interests sake. \$\endgroup\$
    – Oli Glaser
    Mar 5, 2013 at 3:21
  • \$\begingroup\$ I was simply trying to say that the gate may not be fully on, rather than oscilating between on and off. I may be mistaken, but FETs have a middle zone, between the on and off voltage where the FET is not fully conducting, but not off either. \$\endgroup\$
    – Reid
    Mar 5, 2013 at 3:26
  • 2
    \$\begingroup\$ @Abdullah: nope, I guess not - I just try and make sure there aren't any in my electronics :-) \$\endgroup\$
    – Oli Glaser
    Mar 5, 2013 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.