11 years, 6 months ago.

Why do connecting either pin light up both leds?

#include "mbed.h"

AnalogIn ain15(p15);
AnalogIn ain20(p20);

DigitalOut led1(LED1);
DigitalOut led4(LED4);
 
int main() {
    while (1){
        if(ain15 > 0.3) {
            led1 = 1;
        } else {
            led1 = 0;
        }
        if(ain20 > 0.3) {
            led4 = 1;
        } else {
            led4 = 0;
        }
        
    }
}

When I connect either ain15 or ain20 both led1 & led4 light up, why?

3 Answers

11 years, 6 months ago.

I agree with Erik, what are you connecting? A piece of wire will act as a signal pickup for mains hum through to RF, so the input could be high or oscillating an appearing as a high level. If the unused Ain pin is left floating it will also pick up any signals around by stray capacitance. Connect the unused pin to ground and try the test again.

Also when you post next let us know what you were connecting to Ain

Accepted Answer
11 years, 6 months ago.

Using <<code>> <</code>> around your code makes it easier readable for us.

But you connect then only one pin, and leave the other one floating? Then it is simply a case that you cannot tell what happens when you keep an input floating. It probably keeps a large enough charge on a capacitor to get also an analog signal above 0.3 if it is floating.

11 years, 6 months ago.

Thanks for the clarification! I now get it. I tested again and it worked!