7 years, 3 months ago.

DigitalIn displaying a 1 when the pin is not connected to anything

Hi,

I am trying to write the code for a tilt switch, using the DigitalIn function. I have connected 3.3V to the tilt switch and the other pin to pin A0 on my Nucleo F401RE microcontroller. For some reason i am reading random 1's even when A0 is connected to nothing. To set up the pin i used:

DigitalIn capsize_input (A0);

then in my code i used:

if (capsize_input.read() ==1) {

meas = capsize_input.read();

printf ("Kayak has capsized, digital reading: %i\r\n", meas);

}

Is there something i am doing wrong, i am reading mostly zeros, but the occasional 1 at random, which is frustrating as i only want it to read a one when the tilt switch enables the 3.3v to flow to the pin.

Thanks Lewis

1 Answer

7 years, 3 months ago.

You have to use a pulldown resistor to prevent pin floating.

Connect the pin to ground using a resisitor? Thanks for your response

posted by Lewis Daw 11 Jan 2017