Using a Photon 2 to monitor contact closure

With the very copious and gracious assistance from others in this forum - peekay123, gusgonet and jettonj - I just finished settng up a Photon 2 to measure the amount of water in a large storage tank. Working great!

The same tank is equipped with a Photon-based application to turn a pump on or off as a function of an in-tank float switch, i.e. to respond to contact closure. That has been working great for years.

I thought it would be nice if a single Photon could perform both functions. Apparently not. The new volume measurement app has the Photon’s VUSB pin connected to a current-to-voltage converter (to power it) and its ground pin connected to a common ground shared with a pressure transducer, the voltage converter and a 12-volt power supply. To test the ability for that Photon to also react to contact closure I connected one of the Photon’s digital input pins to ground, i.e. to simulate a closed circuit. Mistake! Now that Photon can no longer accurately report water volume. I don’t know a lot about electrons but I suspect that my actions caused excess voltage to be applied to the Photon (my brilliant guess: 12 volts) and it was not pleased. Luckily Photons are cheap!

Might there be another way for a Photon in this dual environment to respond to contact closure without danger of getting fried?

@blshaw45 connecting a GPIO pin, configured as an input, to GND should not cause any problems. Can you describe the connection to the switch in more details and why you think you may have connected the Photon2 input to 12v? Also, how far is the float switch from the Photon2?

Also, can you share the code you used for testing this extra functionallity.

There are a lot of ways to detect the contact but the choice is usually driven by distance and safety (e.g. you don't want potential arcs near combustible liquids!). Some require extra components, others don't.

Hi again peekay! I didn’t bother to embellish my post with a lot of underlying detail. The float switch application has actually been in operation for several years. It is connected (via 12 gauge Romex, but that’s another story) to an NCD relay board with an embedded Photon 2. The firmware running on that Photon sends an ‘on’ or ‘off’ command (a ‘1’ or a ‘0’) to a remote water well pump as a function of the float switch status. No problem there at all.

This most recent query is prompted by the desire to add the water level measurement feature that you just helped me complete to that the same existing Photon. Not only would success in that regard eliminate the need for a second Photon but I realized that I could get rid of the NCD board as well. Note: when I first started working with this technology for the first time in ‘18, I knew nothing; had I known then what I know now, I would not have included the NCD hardware.

Anyhow, after the dust settled on the volume measurement application’s development and testing a couple of days ago, I did an on line query about how to detect contact closure using a Photon (screenshot of search results attached). It seemed simple enough: connecting a digital pin on the Photon to ground would be recognized as a closed circuit while removing that connection would be recognized as an open circuit.

So strictly for testing, I added these two lines to the water volume code: "int contactPin = D10;" and "pinMode(contactPin, INPUT_PULLUP);"

After compiling and flashing the revised code, I connected D10 to the common ground. The pressure transducer was still connected and submerged in water. I noticed immediately that the water level measurements went way out of wack. I disconnected the D10 wire but the significant erroneous measurements did not go away. So I figured the Photon was damaged and I am awaiting the arrival tomorrow of a couple more Photons that I ordered a week or so ago.

Follow-up:

I just set up the Photon that I thought I'd damaged and it is now working OK. I don't understand why I got the bogus gallons readings when I previously connected D10 to ground, but it is reporting gallons accurately now.

Therefore, I again connected D10 to ground to see if my code could identify a closed or open circuit based upn that connection but whether it is connected or not the code returns a '0', i.e. no change whether there is a closed circuit or not.

I added the following code to my script that determines gallons:

In the header: const int contactPin = D10;

In setup:

pinMode(contactPin, INPUT_PULLUP);

In the loop:

 if (now - lastPublish >= publishInterval) {
        int state = digitalRead(contactPin);
        Particle.publish("Gallons", Gallons);
        if (state == 0){
            Particle.publish("State", "Open");
        }
        else{
            Particle.publish("State", "Closed");
        }
        
        lastPublish = now; 
    }

Any thoughts as to why 'state' always returns '0'?

Here is a diagram of my wiring. The measuring of water height and, thereby, the calculation of gallons, works perfectly. But the contact closure query continues to return a '0' regardless of the open or closed position of the float switch (simulated by a jumper wire between D10 and ground).

What happens if you connect D10 to 3V3? Does it go to 1 then?

Haven't tried that yet Rick. All the documentation I've read says that when using the 3.3 volt pin I need to connect a 10K resister between D10 and ground but I don't have a resistor (but can get one if required, of course). Will it be OK to try without the resistor?

Yes, try it without the resistor, just as a test. The button won't work and of course don't press the button if D10 is wired directly to 3V3.

If your INPUT_PULLUP is working properly you don't need the external pull-up resistor in most cases but if you did have a resistor that would be a good thing to try as well.

A tiny bit confused by your statement "don't press the button if D10 is wired directly to 3V3." When I press the button, then D10 and 3V3 will, of course, be directy connected. I suspect what you mean is if I wire the two pins directly before pressing the button, then a subsequent press will accomplish nothing. Is that what you meant?

In your picture, the button is connected between GND and D10, as is typical. It also uses INPUT_PULLUP, so it should be 1 when not pressed and 0 if pressed, but that is not working.

My suggestion was to connect D10 to 3V3 using a jumper wire, which should also cause 1, because you were not getting 1 when you should.

The instruction to not press the button with the jumper wire to 3V3 in place is that would cause 3V3 and GND to be directly shorted via the switch.

Gotcha! There is no button yet; just a jumper wire. So I will move the jumper wire from ground to 3V3. Stay tuned.

OK, with D10 connected to nothing, the variable returns a zero. When connected to 3V3, still a zero. Should I change to INPUT _PULLDOWN?

I can also try with a different digital pin. I also have another Photon I can test with in case this one is bad.

Just changed to PULLDOWN. Still zero regarless of jumper connection or not. Going to try pin 07 next.

Pin 7 = zeros as well. Tried with both PULLUP and PULLDOWN. Can't get a '1' for the life of me. Going to try another Photon next.

Brand new Photon 2, pin D7 to 3V3, PULLUP: zeros regardless of whether jumper connected or not. Same if PULLDOWN.

Only thing left that I can think of is a bad jumper wire (highly unlikely).

I'm wondering if there is something in my code associated with the pressure transducer monitoring that is hosing things up.

Not the jumper wire.

FOUND IT!! When James Jetton was helping to improve the accuracy of the measurements retrieved from the pressure transducer (by averaging several sequential values) he inserted a Serial.begin() line in setup(). I have never used that command - never needed it - but I left it in, not knowing that its presence would have any affect on anything else in my code. Well, it did!

I just removed it, recompiled and re-flashed the Photon and BINGO! Now everything works as it is supposed to. I have reverted to the pin 10 to ground connection (so I wouldn't have to redraw my diagram).

Thanks for your continued support of my learning journey.