2
\$\begingroup\$

I have a SPST rocker switch with 3 terminals (Datasheet), which contains a dependent LED which is illuminated via 12V if the switch is on. I want to read the switch with an Teensy/Arduino.

After some research, I understand that there are many ways to accomplish that, but for me a simple and clean solution would be to use an opto-isolator, like the PC817 (Datasheet).

I'm thinking of a circuit like this:

schematic

simulate this circuit – Schematic created using CircuitLab

I thought about separating the switch's current from the microcontroller, because then I can use its native 12V LED and don't have to worry about damaging the board.

Is this correct, would that work? Or do I miss something here? Or are there easier/better solutions?

Thanks!

EDIT 2:

  • Added the datasheet for the switch: like in the commentaries said: often 12V Buttons comes with separate LED pins. But the switch linked above has an dependent LED.
  • By the way, in getting my post here clearer, I found a useful explanation (for me) about SPST or DPST switches are: Whats SPST switches?
  • Changed schematics: added switch wiring following it's datasheet. And discovered awesome circuitlab feature within stackexchange :)
\$\endgroup\$
7
  • 2
    \$\begingroup\$ Normally you’d connect the opto E to 0V and the C to the port pin and use the port pin pullup feature. Reason being you don’t want the input to float - either the pullup pulls it high or the opto pulls it low. \$\endgroup\$
    – Kartman
    Commented Mar 2, 2021 at 2:14
  • \$\begingroup\$ Yes, use an external pull up resistor between photo transistor and Vcc. \$\endgroup\$
    – tlfong01
    Commented Mar 2, 2021 at 3:15
  • \$\begingroup\$ Please provide the button type. Usually the contacts and light are completely separate so we need to know that if your circuit makes sense at all. \$\endgroup\$
    – Justme
    Commented Mar 2, 2021 at 7:45
  • \$\begingroup\$ @Kartman ahh, my bad. Late night brain error. Of course it's wired from Digital Pin, over Opto, to GND. I changed the circuit. Correct now? \$\endgroup\$
    – Eilee
    Commented Mar 2, 2021 at 15:21
  • \$\begingroup\$ @tlfong01 why use an external pullup? Is there any advantage over the internal pullup of Teensy/Arduino? \$\endgroup\$
    – Eilee
    Commented Mar 2, 2021 at 15:24

1 Answer 1

1
\$\begingroup\$

Assuming a 3 pin rocker switch with a common anode/+12v pin, a switch +, and a led ground pin, the only concern is that it already has a built in resistor for its target voltage. So adding another resistor and led will reduce the brightness on both leds if you don't factor that in.

Assuming a 20mA 3.4V led is inside the a nominal resistor of 430 ohms is being used. Add a 1.4V led like the opto listed in your schematic and the overall current drops from 20 to 16 mA. Basically a non issue. At that 2.2k resistor and you are down to 2.6 mA. The switch led and the opto won't be very bright.

But thats only assuming you have a 12V DC supply and not a 220V AC supply. We need your full wiring information.

Update Based on your new schematic, you are not putting the led in series with the opto. While this technically does not show you if the led is on, it does tell you when the switch is on, conducting 12V through to the switched output. For all intensive purposes you can assume the led is on. It will work without changes.

\$\endgroup\$
3
  • \$\begingroup\$ Thanks for you answer. Look at the schematics now, this is how the switch is wired according to it's datasheet. 1. How can I factor that in? Could you explain how to calculate that? And why does the two resistors affect each other? 2. I know what mA and V I need for the opto isolators LED to work properly. But how can I know what resistor and LED is inside the rocker switch? I can't find something in the datasheet? 3. Yes. I want to use 12V DC. \$\endgroup\$
    – Eilee
    Commented Mar 3, 2021 at 2:06
  • 1
    \$\begingroup\$ @eilee your new schematic would work well. 1. Since you are not putting the led and the opto in series then there is nothing to factor in. If you were, its because the resistors in series add up as do the voltage drop of the leds. 2. You can estimate the resistor they use for the led by taking some measurements. Or you can guess. The source voltage and the color of the led can give you an idea of the range of resistors use. Measure the current and you can get really close. Current in a series circuit is the same. \$\endgroup\$
    – Passerby
    Commented Mar 3, 2021 at 2:50
  • 1
    \$\begingroup\$ @eilee so (source voltage - led voltage drop ) / current = resistance. If you know current and source voltage and educated guess for led voltage drop based on color, you find the resistor. Assuming its a 20 mA led, we have (12 - Led Vf ) / 0.02 = x. Let's remove the led Vf as if the resistor is taking all the voltage. We have 12 / 0.02 = 600 ohms. Now let's assume a high blue led Vf of 3.6V @ 20 mA. (12 - 3.6) / 0.02 = 420 ohms. Based on this the resistor used is somewhere between 420 and 600 ohms. It's just plugging in numbers based on a few known factors and Ohms Law. \$\endgroup\$
    – Passerby
    Commented Mar 3, 2021 at 2:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.