Spark Core DigitalRead issue

Hi All,

Newbie here (first post) and excited to be part of this lively community.

I’ve noticed a strange behaviour with my Spark Core. I can perform a digitalWrite to it to turn on the “D” pins but when I try to read the values, it toggles from HIGH to LOW.

Does anyone have any idea what may be wrong? Many thanks in advance.

Best,

Prem

Hey there,

Glad to have you in the community!

It might have something to do with the fact that you’ve configured that pin to be an output, and you’re now using it as an input. I’m guessing it has to initialize again, thus setting the pin low.
I’m not sure of the above, so let’s wait for some more experienced folks to join in :slight_smile:

I think @moors7 is correct, since you want to read the pin it has to be low, reading it when you set it high would give incorrect results :wink:

1 Like

Could you please post your code here.

While @Moors7 is right that you have to configure the pin as INPUT to read any external signal, I’d like to add that you then actually have to provide some external circuitry to produce this signal. Otherwise you’re measuring a floating input.
One way to do this internally would be to use INPUT_PULLUP or INPUT_PULLDOWN instead.

But even if you set your pinMode(Dx, OUTPUT) you can still digitalRead() that pin, but you don’t actually “measure” the pins state, but only get back the state of the respective bit in the output register of the µC port being “home” of that pin.

1 Like

So i’m imagining you are doing the following (like what i love doing ;))

void setup(){
  Serial.begin(9600);
  pinMode(D7,OUTPUT);
  digitalWrite(D7,HIGH);
}

void loop(){
  Serial.println(digitalRead(D7));
  delay(1000);
}

You will probably see HIGH all the way printed since we set that pin to high in setup().

Essentially, what this does is it reads the current OUTPUT state that the pin is set to. :wink:

Another boiler plate one :wink:

void setup()
{
  pinMode(D7, OUTPUT);
}

void loop()
{
  digitalWrite(D7, !digitalRead(D7));  // toggle the LED
  delay(1000);
}

BTW: digitalWrite(D7,high); should be digitalWrite(D7, HIGH);

1 Like