Read a HIGH output pin with another input pin

int Opferstecker = 10; //declares pin 10 as sacrificial connector, which should be permanently HIGH/1

int EingangSensorplatz = 4; //Declares pin 4 as input sensor, which switches on LED connection and sets it to HIGH/1.

int LEDVerbindung1 = 7; //declares pin 7 as LEDConnection1


void setup() {

// put your setup code here, to run once:

pinMode(Opferstecker, OUTPUT); //Sacrificial connector set as OUTPUT

pinMode(LEDVerbindung1, OUTPUT); //LEDConnection1 set as OUTPUT

pinMode(EingangSensorplatz, INPUT); //sensor set as INPUT


}


void loop() {

// put your main code here, to run repeatedly:

digitalWrite(Opferstecker, 1); //sets sacrificial connector to HIGH

digitalRead(EingangSensorplatz); //Reads out input sensor

if (EingangSensorplatz == 1){ //if input sensor position is HIGH

digitalWrite(LEDVerbindung1, 1); //LEDConnection1 on HIGH

}

}

This is my program, theoretically everything should work like this. But the LED is not turned on when the sacrificial plug is connected to the sensor.

In this case simulated by a simple line with HIGH signal.

What am I doing wrong am I missing something?

Perhaps the LED polarity is wrong? Is it on when connected to 5V instead of pin 7?

This line merely reads the pin and discards the result:

Then this line compares 4 to 1 and finds them not equal:

2 Likes

No sadly the polarity isn't wrong. That would be a quick thing to fix.

so it's 4, not 1 as expected here:

You don't want to compare the sensor position but the sensor value!

1 Like

You're right! It reads the pin but why does it discard the result?

True 4 isn't equal to 1. I meant to say if EingangSensorplatz is HIGH. I can achieve this with just one = am I right?

Yes Pin 4 is EingangSensorplatz not Pin 1.

Yes I want to compare the value. I can achieve this with just = instead of == am I right?

It discards the result because you don't assign it to anything or use it. See digitalRead() - Arduino Reference

No, one = is an assignment operator. The trick you are looking for is using the result of the digitalRead(pin) for the comparison. Something like:

 if ( digitalRead(EingangSensorplatz) == HIGH){

... which reads the EingangSensorplatz-identified pin, and returns the logical state of the pin, and then compares that result to HIGH.

Gehirn einschalten! Wozu ist digitalRead() notwendig?


void loop() 
{
   digitalWrite(Opferstecker, HIGH); //sets sacrificial connector to HIGH

   byte status = digitalRead(EingangSensorplatz); //Reads out input sensor

   if (status == HIGH)
   { //if input sensor position is HIGH
      digitalWrite(LEDVerbindung1, HIGH); //LEDConnection1 on HIGH
   }
}

1 Like

Alright I'll see into it. Thank you!

Thank you! I still have a lot to learn about that.

1 Like

Um den Status eines undefinierten Pins auszulesen, etwa einen Taster oder Sensor. Danke für den Wink mit dem Zaunpfahl.

Thank you!

I wonder: why would you want to read an output pin using another pin?
If a pin is set as output in the DDR register, the input part still works, you can just read VIN register (and that’s what digitalRead() does).

Ok I did not know that.
I want to read the output pin, because I want to check with this pin if there is an electrical connection between the output and the input. Similar to a multimeter continuity tester

For a continuity tester, try the Uno's built-in op-amp LED logic probe: leave pin 13/LED_BUILTIN as a default input, and put your jumper between 13 and the other pins to see the LED light or not.

For testing purposes this is an option. But since the whole thing is to be installed in a housing, I need LEDs that are visible from the outside.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.