• @d0773d, thanks for doing the leg work on details about the relays board / module - http://www.espruino.com/Relays you use. It is what I expected when you sent me a pic of it for the first time.

    Optocouplers are used to drive the relay in order to electrically (galvanic) separate the input to the board and the driving of the relay itself (control circuit electrically separate from activation circuitry). The relay type used on the board requires 5V to work reliably. Some mc though operate on 3.3V (or less) and therefore cannot handle or create 5V signals and often also (some or all) pins do not have the strength to source w/ high/1 or sink w/ low/0 enough current (mA) for directly driving a relay. Voltage difference is mentioned in the Espruino Relays page by '...If you must use a non 5v tolerant pin, you can sometimes remove a jumper from the relay and power the relay from 5v while powering the optoisolator [optocoupler] from 3.3v - however this will depend on your relay module.)

    An optocoupler is a device that has two isolated circuits or sides - hence also called optoisolator: the input side is an LED that shines light on a switching transistor that control the output side (sorry for the crappy inline char graphics):

    o-H--------.                   .------H-o
                |                  |
     opto       |    LED          /  opto
     coupler  .---.  light      |/   coupler
     input     \ / ----------> -|    output
     side:      V    switches   |\   side:
     LED      -----  transistor   >. NPN
                |    on            | transistor
                |                  |
    o-L---------'                  '------L-o
    

    Relays boards / modules solve these power - voltage and current - and protection issues using multiple approaches:

    1. use optocoupler t0 separately powering of input from mc and output to control relay with different voltages
    2. use an optocoupler to drive the relay coil directly or indirectly
      • directly: optocoupler's output is strong enough to drive relay (using Darlington circuitry)
      • indirectly: optocoupler's output is not strong enough to drive relays directly but strong enough to drive an amplifying transistor which then drives the relay (as on board at hand)
    3. wires input side of optocoupler the way that pulling the input of the board to ground activates the relay coil (directly or indirectly).

    Pulling to ground allows to use the open collector type of output circuitry on the mc to driving the board input. Pulling to ground - sinking current - is usually simpler (in a 'n-type silicon/semi-conductor context'), more powerful and more predictable. But with pulling to ground / sinking comes negative 'negative logic' / output 0 / sink. With negative logic, output of logic LOW / 0 means 'on', and output floating (disconnected) / logic HIGH / output 1 - same voltage as input side of optocoupler is sourced - means 'off'.

    Output floating is like output disconnected or tri-state - neither LOW nor HIGH - which works for sinking (or sourcing) of LED, but not controlling the base or gate of a bipolar transistor / FET. To control a bipolar transistor or FET, a pulling resistor - either to Ground or power (VCC 3.3V or 5V) is needed on the Basis or Gate to have a stable condition. Espruino has pin-mode that can provide such a resistor - just very weak: 30k..40k Ohm. If this is sufficient, no external resistor is needed.

    The logic can be reversed (again) how the relay (output) pins are used - when the relay exposes three (3) output pins:

    • normally closed
      • connected to pole when relay coil is not activated / not powered
      • disconnected from pole when relay coil is activated / powered
    • pole
    • normally open
      • disconnected from pole when relay coil is not activated / not powered
      • connected to pole when relay coil is activated / powered

    Which way to use the relay pins depends on the duty cycle of the (regular, non-latching) relays:

    The input side is controlled by the mc with connecting the L(side) of the LED to Ground. The H(igh) side is powered by 5V (or 3.3V). Usually a current limiting resistor is in the circuit either in on the H(igh) or L(low) side.

    The output side is controlled from the (infrared) light of the LED. The H(igh) side is powered by 5V - as the relay in use requires - and does not have a resistor in the circuit, but a Diode in blocking direction (regarding current flow) across the relay coil pins in order to protect the optocoupler from Back EMF spikes on relay coil deactivation.

    Therefore, the trigger code logic with setTimeout(... and digitalWrite(... (or .set() and .reset()) and related initialization has to be negative logic. setTimeout(... with digitalWrite(... (or .set() and .reset()) for controlling the ***pulse end / duration*** is preferred overdigitalWrite(...``` as documented in Espruino reference for digitalPulse():

    digitalPulse is for SHORT pulses that need to be very accurate. If you're doing anything over a few milliseconds, use setTimeout instead.

    The code using setTimeout(... and digitalWrite(... (or .set() and .reset()) looks like as you pointed out (with some changes in the initialization):

    / some defs/inits
    var inProcess =  false;
    var shortPress = 1000; // ms 1  sec
    var longPress = 5000; // ms 5 secs
    var pressPin = B3;
    pinMode(pressPin,"opendrain"); // output like 'open collector' see reference
    pressPin.set(); // output in tree state / float
    
    // ...other defs/inits as needed
    
    function press(short) {
      if (inProcess) return; // ignore press function when one is still going on
      inProcess = true;
      // Relays (board) act in an inverse manner
      // reset() will latch the relay
      pressPin.reset();
      setTimeout(function(){
          // Relays (board) act in an inverse manner
          // set() will unlatch the relay
          pressPin.set();
          inProcess = false;
        },(short) ? shortPress : longPress);
    }
    setWatch(function(e) {
      digitalWrite(LED1, e.state);
      press(shortPress);
    }, BTN, { repeat: true });
    
  • Fri 2018.08.03

    re: sorry for the crappy inline char graphics ):

    @allObjects don't sell yourself short on the effort, I felt the graphic was quite good and detail explanatory. Nicely done!

    re: "but a Diode in blocking direction (regarding current flow) across
    the relay coil pins in order to protect the optocoupler from Back EMF
    spikes on relay coil deactivation."

    I couldn't remember the diode direction, but remembered I had a link to a diagram:

    https://www.electronics-tutorials.ws/blog/relay-switch-circuit.html

    and for those that might be diode direction impared, the diode polarity:

    https://learn.sparkfun.com/tutorials/polarity/diode-and-led-polarity

    All in all I felt the explanation could be used as a separate tutorial, something like:

    Home >> Tutorials >> Driving a relay using an opto-isolator

    Is there a way to block copy from one post to another or would just a link to this thread be sufficient?

About

Avatar for allObjects @allObjects started