using optocouplers to give pwm signal

Hello All!

Not sure if this is the correct spot for my question, but here it comes;

I need to give error or OK messages without any connection between two arduinos, but could i do it using a optocoupler?

I mean, running a pwm signal of 200 into the optocoupler, would it pwm out 200? So that if something went wrong on one board, it could just run a pwm of 200, and the other board then would read the 200 pwm and give an error message of "low voltage" for example?

If this where not such a good idea, do you have something else to suggest?



Not connected together? How far apart? Or just electrically isolated?

What're you intending to use for an "optocoupler"? A DIP device? A discrete LED shining through "free space" on a photocell?

Usually this would be set up so that when the LED/light turns on/off then a phototransistor (or transducer du jour) turns on/off, like a switch.

Its 2 different earthpoints, that cannot be connected together, so they should be electronically isolated from each other.

For an optocoupler i meant a DIP device yes...

OK – See dwg attached.


PWM seems overkill to me, why not just have one device output an optical hearbeat every 100ms or whatever is suitable to your project ?

If you have the other device interrupt and set a flag whenever the heart beat is detected your main code can simply check if more than 120ms have passed since the last heart beat.

Duane B

Runaway Pancake:

Yes, thats what i had in mind, but how would it be to code this?


Yes, that could be useful also.

Could it be done like this; On the board that should send the message; digitalWrite(failout,HIGH); delay(10); digitalWrite(failout,LOW);

and then on the receiver: duration = pulseIn(failin, HIGH); if (duration = 10) { do this and that }


if (failout == HIGH) { duration = pulseIn(failin, HIGH); if (duration = 10) { do this and that } }

What do you think? :)


No I would not do it like that.

I would use an interrupt, this is a short sample of code from my lap timer project that looks for a pulse or 10ms. If it finds one it sets bIRPulseFlags to a value which indicates to the main code in look that we have seen the start and end of a pulse that is within our limits for a transponder pulse or in your case a heart beat from your other device.

To use this code or something similar you need to use attachInterrupt(0,captureLap,CHANGE); in your set up function.

You would then need to test bIRFlags to see if a new pulse has been received and if so record the time. If no pulse is received, compare the previously recorded time to the current time and if the interval is greater than your maximum tolerance you know there is a problem with the other device.

Sample taken from the Personal Lap Timer project, it isnt going to work if you cut and paste it without the rest of the project, but it should give you the right idea -

void captureLap()
  // A little bit of direct port access here - the code is using INT0 so we
  // know its pin 3 of port B so to save a little time we are accessing the 
  // pin directly
  if((PIND & B00000100) && (bIRPulseFlags == IR_PULSE_START_SET))
    // high means end
    ulEndPulse = micros();
    ulPulseDuration = ulEndPulse - ulStartPulse;
    if(ulPulseDuration > MIN_PULSE_DURATION && ulPulseDuration < MAX_PULSE_DURATION)
      ulNewLapStartTime = millis();
      bIRPulseFlags |= IR_PULSE_END_SET;
      // this pulse is not good, clear flags and try for a good one
      bIRPulseFlags = 0;
  else if(!(PIND & B00000100) && (bIRPulseFlags == 0))
    // low means start
    ulStartPulse = micros();
    bIRPulseFlags |= IR_PULSE_START_SET;

Duane B

Aha. Nice, ill try that :-)