digitalRead flip flops between HIGH and LOW

I finally started using my Photon last week. I have been planning to create an IR/photodiode “fuel gauge” for my pellet furnace and was working on reading the pins. At first they seemed to be working just right, but today I noticed that the values are unreliable. If I use either 5V out or A5 high and connect it to D2, D2 does not automatically change to HIGH. Then, going further, I found that these pins will change their values without attempting any input or otherwise touching the board.

Here is the code I used to discover this:

And here is the output from curl:

If you follow something like d5off/d5on, you’ll see it changes from d5off to d5on after 20 seconds or so.

I’m not sure if my hardware is bad or my logic. When I flash/run the tinker app, it seems to respond as I would expect. If I turn A5 high and connect it to D2, then D2 will read high. When I disconnect the cable, D2 will read low.

I’ve also tried digitalWrite all pins to high or low in setup, or connecting A5 (HIGH) to pins to keep that at high, or A4 (LOW) to keep pins at low. I’ve also tried +5V and GND for the same purposes.

Looking at your code taking 10 seconds to change state is right .
You are reading d0 waiting 2 seconds reading d1 waiting 2 seconds and reading the next , so it takes 10 seconds for 1 loop.

But is it supposed to change state on it’s own? I would think that if nothing is connected to a pin (say D3 in the example below), it would stay LOW until someone connected +5V. And once +5V is connected, it should say HIGH for as long as +5V is applied (dropping to LOW when the input is removed).

event: d3on
data: {"data":"null","ttl":"60","published_at":"2015-12-21T18:34:44.353Z","coreid":"250028000347343337373739"}

event: d3off
data: {"data":"null","ttl":"60","published_at":"2015-12-21T18:34:54.858Z","coreid":"250028000347343337373739"}

If the input pins are not connected to anything, then they can be “floating”, and could be either LOW or HIGH. If you have nothing connected to them, you should try setting the pinMode to INPUT_PULLDOWN so the pins will be held at ground.

@Ric beat me to it! :stuck_out_tongue:

One word of caution: Not all pins are 5v tolerant and you could damage the Photon if not careful. Refer to the documentation for guidance.

Thanks @Ric and @peekay123. Found another post about the INPUT:

I did check the document and it looks like all pins except A3 and DAC are 5V tolerant (when not in analog mode).

I then tried switching from INPUT to INPUT_PULLDOWN (and INPUT_PULLUP for good measure). I still get the floating between HIGH/LOW with nothing connected.

Even when I connected a cable from A5 to D4 it still continues to float. D1 however, does seem to stay in a state with INPUT_PULLDOWN. Unattached or attached to GND, it stays LOW. Connected to A5, it goes HIGH. It takes a while to test each pin and let it cycle through several times, but D1 seems reliable while D2-D5 do not (they float between states).

The code just has some logic errors.

if (digitalRead(Dx == HIGH)) {


if (digitalRead(Dx) == HIGH ) {

That was it. Just bad code. I am so glad it was oversight on my part than damaged hardware.