How to read led status directly from power led

Hi.
I have a project to read the status of the LED off or on from different circuits and want to display it on the LCD
in the circuit the main current is 12V and is changed using a 1K resistor to the LED in the circuit and the LED is set on or off by the Ground in the circuit.
Then I removed the LED and replaced it with a PC817 to enter the arduino on Pin D2, and I tried to make a simple program to read the status of the LED but it was messed up, which should be the LED off but in the arduino it is unstable off and on and so on.
Sorry if my question is silly, I'm still a beginner in this field, this is the PC817 schematic and the code I used.

I appreciate all the answers, thank you.

Code:

int pinD2 = 2;

void setup() {
  Serial.begin(9600);
  pinMode(pinD2, INPUT); 
}

void loop() {
  int test = digitalRead(pinD2);
  if (test == HIGH)
  {
    Serial.println("ON"); 
  }
  else if (test == LOW)
  {
    Serial.println("OFF");
  }
  delay(500);
}

What are you using for Vcc on the output of the PC817? A schematic rather than words, please. Did you read the data sheet for the PC817 and see their schematic for testing the device? Use the identical circuit to interface to your Arduino.

above I have included a photo of the scema that I use, I don't use the vcc to go to the arduino, I only read whether the gnd is off or on because the circuit controls the led off or on with GND, is my method wrong?
please correct if it is wrong

From the Arduino.cc documentation. A digital read

You are ignoring the transistor in the output of the PC817.

So what should I do?

Connect the opto LED with a 10k series resistor to the 12volt source (1k is not needed).
Connect opto transistor collector to pin2 and emitter to Arduino ground.
(there is no electrical connection between the 12volt source and the Arduino)

Try this sketch (untested), which should print only once with every change.
Leo..

const byte optoPin = 2; // opto collector to pin, emitter to ground
bool state, prevState; // holds states

void setup() {
  Serial.begin(9600);
  pinMode(optoPin, INPUT_PULLUP); // internal pull up
}

void loop() {
  state = digitalRead(optoPin); // read
  if (state != prevState) // if changed
  {
    prevState = state; // remember
    if (state) Serial.println("OFF"); // if pin is HIGH
    else Serial.println("ON"); // if LOW
  }
}
1 Like

So you mean the 1K resistor is replaced with 10K?

Can you give an example of the schematic? Because here I jumpered the cathode with the emitter, it might be different from what you mean.

Yes. No harm done when a 1k resistor is used, but that's like cracking an egg with a sledge hammer. The pin needs less than 0.2mA to be pulled down, and 1mA opto current (10k resistor) is plenty for that.

Don't jump the cathode with the emitter. That will defeat the use of an opto coupler, which has the primary task of isolation between the two circuits.
Leo..

in the circuit that turns on and off the LED is the Ground current, because in the circuit the Vcc is stanby and the ground is controlled by the circuit controller.

what should I do

Sorry if this seems stupid I'm not very good at electronics.

Your use of an opto-isolator means you do no care how the circuit is turned off or on because it is completely isolated from your circuit. When the LED is on, your circuit will read low. When the LED is off your circuit will read high.

Sorry for just replying, I will try it later

The LED is turned on with a ground line by the circuit board microchip, so whether this is a signal or a voltage, I'm a little confused about what to do next.

The project I'm working on is almost similar to this article Reading the LED status of an external circuit? , but if in my circuit the LED is controlled from the ground cable line and I don't know if this is a signal or voltage,

sorry because I'm not very good at electronics

Actually, both are true! A signal is a voltage change, unless it is a current change. All have the same signalling function!

The opto LED is turned ON/OFF with the 12volt device,
Which can be a completely independent circuit from the Arduino.
So two wires from the device to the opto LED/resistor.
The resistor is there to limit the current for the LED to a safe value.

The opto transistor acts as a switch,
and is connected to Arduino pin (opto collector) and Arduino ground (opto emitter).

The pin is internally pulled up (HIGH), with pinMode(optoPin, INPUT_PULLUP);,
and pulled down (LOW) with the opto transistor acting as a switch.

Hope this makes it clear.
Leo..

Looks like your analysis is correct, it's working fine, I'll see if there are any errors or not, I'll let you know later what the results are.

I can also learn here, opto does not care which controls the positive or negative led, I thought it was very impossible, and I tried it successfully, but I do not know if in the future this will be an error like the led should be off but the Arduino reads on and off like my circuit before.

None of us can really predict the future, that is why software is easily changed.

state = digitalRead(optoPin); // read
state = !digitalRead(optoPin); // read

Note the exclamation mark.
Result is now the opposite.
Leo..