Check if digital pin is on or off?

Hey guys, I was wondering what the best method was to set a digital pin as an output but still check wether it is on or off.

I’m building a web interface to control relays and for example I change the state of a pin, close the browser but when I later open the browser I want my browser to check if it is still on.

Thanks

Just check the digital.read and it should return the status.

There was a patch and I believe it’s already available.

1 Like

Thanks, just realised it was kind of a stupid question xD!

No worries. It wasn’t behaving correctly before :slight_smile:

1 Like

Hello, very interesting topic for me, I want to avoid to take another pin of the spark core to know the state of an Output (A3)
Can you say me what I should add to my code? I don’t understand how an output can be at the same time an input…?

my code concerning A3:

    int Chaudiere = A3;
    
    void setup() {
    
    Spark.function("chaudiere", chaudControl);
    pinMode(A3, OUTPUT);
    
        // Output Initialization
    digitalWrite(Chaudiere, HIGH);   //(A3)
    }
    
    void loop() {
   }

    int chaudControl(String command){
        if(command.substring(3,7) == "HIGH") state = 1;
        else if(command.substring(3,6) == "LOW") state = 0;
        else return -1;
        digitalWrite(A3,state);
    }

You mean you want to check the state of an Output?

You can simply use digitalRead(pin); to do it.

The output pins doesn’t become and input. Doing so simply checks the pin status and see if they are set correctly according to what you are expecting :wink:

If you share more about what you are trying to achieve, we should be able to help you get it right :wink:

So you mean I just need to add digitalRead(A3); inside the void loop, and I will be able to check the state of my output (A3) with a POST request like other input of the Core?

@JP2M,

i don’t understand what you are trying to do?

Yes, digitalread(pin); will tell you the status (HIGH/LOW) of the digital pin. It works like analogRead(pin) as well :wink:

Your function is to write to change the pin status. Why do you want to read it instead?

I want to check with a Web interface the state of the digital output (A3 in my case) when this A3 pin is set as Output in my Spark Core code.

So if I’m right I will add digitalRead(A3); in my void loop and I will make a POST request through the web to know the state of A3 output when needed? correct?

https://api.spark.io/v1/devices/{{DeviceID}}/digitalread?access_token={{Token}} params=A3

   int Chaudiere = A3;

    void setup() {

    Spark.function("chaudiere", chaudControl);
    pinMode(A3, OUTPUT);

        // Output Initialization
    digitalWrite(Chaudiere, HIGH);   //(A3)
    }

    void loop() {
    digitalRead(A3);
    }

    int chaudControl(String command){
        if(command.substring(3,7) == "HIGH") state = 1;
        else if(command.substring(3,6) == "LOW") state = 0;
        else return -1;
        digitalWrite(A3,state);
    }

I'm in the same example as Yannis

I think I have find a way to do it... not sure it is the most simple, but that work :slight_smile:

I put the code here in case some other want to do it.

So my output A3 can be now controlled and readed through the web with a GET request:
I have add a variable (EtatChaud) to know the state of my output

int Chaudiere = A3;
int EtatChaud = 0;

void setup() {
Spark.function("chaudiere", chaudControl);
Spark.variable("etatchaud", &EtatChaud, INT);
pinMode(A3, OUTPUT);

    // Output Initialization
digitalWrite(Chaudiere, HIGH);   //(A3)
}

void loop() {
EtatChaud = digitalRead(A3);
}

int chaudControl(String command){
    if(command.substring(3,7) == "HIGH") state = 1;
    else if(command.substring(3,6) == "LOW") state = 0;
    else return -1;
    digitalWrite(A3,state);
}

web request to know the state is very simple: https://api.spark.io/v1/devices/{{Device}}/etatchaud?access_token={{Token}}
that will return 1 if output is HIGH and 0 is output is LOW.

1 Like