I'm using a float switch from RS components, the website of purchase can be found here.
I'm trying to find an efficient way of testing if the float switch is connected to the arduino or not, as my final project will consist of a plug in connector on the enclosure which can be connected and disconnected. I've found a way to do this by adding a 100k ohm resistor between the float switches analogue pin and GND, which reads ~2050 if connected and the float switch is open (with the exception of a fluctuation reading of ~2600 once every 20 readings or so), <100 if the switch is closed, and ~2200 if the switch is disconnected.
I'll add up every 10 readings and if the sum of the values are greater than 20,000 the float switch is disconnected. If it is under 20,000 then the float switch is connected.
This gives me a way of finding if the float switch is connected or disconnected, but I'm sure there will be other more efficient ways, as the switch will only display as disconnected after the readings are taken. Could someone help? Or should I just keep it as how it is?
I was too quick in my previous post. I misread this bit and thought you meant 100k in series with the switch contact. I now see it's parallel across the switch. This should work, but I'd use a smaller value resistor so that you read around 0.5xVcc with the switch connected, but in open position.
It will help to use a knwon-value pullup on your analog input. E.g. use 10k for both resistors.
Try lowering that value, something in maybe the 18K range across the switch and another as a pull up you will get more stable reading. Open you will read about 1/2 supply closed about zero. I used this trick in alarm systems many years ago.
On a few more advanced models the input pins have the option of both INPUT_PULLUP and INPUT_PULLDOWN. If you are using one of these models, if the pin is floating, it should read HIGH in INPUT_PULLUP mode and LOW in INPUT_PULLDOWN mode.
If you don't have one of those models, only INPUT_PULLUP will be available. If you have a spare pin, you can add a 47K between that pin and the sensor pin. You can then set the spare pin to OUTPUT mode and LOW to simulate INPUT_PULLDOWN on the sensor pin.
So if I'm correct, connect a 10k pullup resistor to the analogue pin, and connect the float switch to GND in parallel.
If so, I tried that and it didn't work. The switch is NO, and the best result I've had so far is by using a single 100K pull down resistor as opposed to the pull up resistor I was originally using, and putting the pinmode of the float switch as input_pulldown. The readings I am getting are ~2000 or <100 when connected, and ~2800 when disconnected.
Edit: I changed the pin mode and it didn't change any of the values i was getting, so the 100k pulldown resistor is good enough on its own (I think!)
And 10k parallel to the switch on the sensor side. See the schematic I provided. So add two resistors in total. One on the Arduino side, one on the sensor/switch side.