Using SPDT on-off-on Rocker Switch

Hi everyone!

I have a really cool project I am working on and I need some help.

I have on of those cheap dollar store LED lanterns that take 4 AA batteries with a center off sub mini rocker switch. Turn it on mode 1 and it is bright and mode 2 turns on at about 60%. I have replaced the white LEDs with WS2812 digital RGB LEDs (aka NeoPixels) being driven by an ATTiny85. Everything is working great.

What I want to do is when I switch to mode 1, just turn set all the LEDs to white. When switched to mode 2, do some cool color effects.

I already know how to program the ATTiny85 to do what I need. I am only using 1 pin to control the LEDs. I have 5 left so I thought I could use one for mode selection.

The rocker switch is SPDT on-off-on and I figure I can use it for power either way. I want to be able to see which way the switch is toggled using a digital input. My question is how?

If I had the right size DPDT it would be a no brainer.

i am trying to not buy anything else and use the same case and switch. I was thinking of using a transistor on one side of the switch so that the base/collector is either conducting or shorted by the switch.

I apprciate any and all help/suggestions.

I am planning on posting my project when I get this working completely.

Thanks in advance!

Wire the common pin to Gnd. Wire the NO and NC to two input pins with internal pullups enabled pinMode (pinX, INPUT_PULLUP);

One or the other will be pulled low when the switch goes from side to side. Since one side is always the opposite of the other, you really only need 1 input.If it's high, do one code, and if low do the other.

CrossRoads: Wire the common pin to Gnd. Wire the NO and NC to two input pins with internal pullups enabled pinMode (pinX, INPUT_PULLUP);

One or the other will be pulled low when the switch goes from side to side. Since one side is always the opposite of the other, you really only need 1 input.If it's high, do one code, and if low do the other.

Sounds great. How do I use that to turn on power to the whole thing as well?

Thanks!

You can't - need a separate switch for that.

If you leave the switch controlling the power and connect two Attiny pins each equipped with a pull-down resistor maybe it could detect which pole of the switch is powered?

...R

CrossRoads: You can't - need a separate switch for that.

I think you could do it by connecting the two switch outputs via a couple of diodes to produce a common output which you can use to power the Arduino - one of the 'raw' switch outputs can then be connected to a digital I/O pin so that the Arduino can tell which way the switch was operated. You would need to make sure that you don't ever end up with power applied to the I/O pin without the Arduino as a whole being powered.

I was thinking about possibly using a transistor to send the signal when the switch is in one position.

Here is the schematic for what I was thinking:

I tried the concept in a circuit simulator and it seems to work. Is there anything I may be missing? I know I did not add the NeoPixels in the diagram and I know they will draw current themselves as well.

Thanks for all the input!

No - won't work because you have no pull-down on the collector of the transistor - which would only be functioning as two diodes anyway.

Paul__B: No - won't work because you have no pull-down on the collector of the transistor - which would only be functioning as two diodes anyway.

So if I add the proper pull-down resistor that would work? I guess that I could change the transistor to a diode and just bring the signal directly from behind the diode. I will post another version and perhaps you could let me know if that will work.

It has been a long time since I designed a circuit and this is my first microcontroller project using an ATTiny85.

I appreciate all the help!

If the LEDs don't draw a lot of power, you might use one of the small h-bridge chips to control which bank of LEDs gets power and also control the LED brightness.

dmilton2004: So if I add the proper pull-down resistor that would work? I guess that I could change the transistor to a diode and just bring the signal directly from behind the diode. I will post another version and perhaps you could let me know if that will work.

It would indeed work, but I do not like the idea of using the base-emitter junction of a transistor as a diode. It would arguably be sufficient to use just one diode as I gather you have figured out, and the pull-down resistor. Either you switch the battery to the Arduino supply and the diode blocks it to the control input, or you switch it to the control input and the diode then powers up the Arduino. However you should actually use two diodes - where your transistor was depicted - so that the extra diode to the pull-down resistor prevents the input pin being pulled significantly higher than the supply to the Arduino.

What if you connect 2 diodes, both cathodes to chip v+. The anodes to each side of the switch with common to batt+. If center off, then either way will power the chip. Now connect 2 100k resistors, from ground to each side of the switch. Then connect each side of the switch to a input pin. Resulting in side 1 High, side 2 Low or side 1 Low, side 2 High.

steinie44:
What if you connect 2 diodes, both cathodes to chip v+. The anodes to each side of the switch with common to batt+. If center off, then either way will power the chip.
Now connect 2 100k resistors, from ground to each side of the switch. Then connect each side of the switch to a input pin. Resulting in side 1 High, side 2 Low or side 1 Low, side 2 High.

Two problems.

One is that one of those input pins is superfluous as either one or the other will be pulled high, never both and never neither, so you only need one.

The second is what I just explained before - you should not be pulling an input to a voltage higher then the Vcc which in this case is lesser than the battery voltage by the voltage drop of the diode. Should be using Schottky diodes.

The rocker switch is SPDT on-off-on and I figure I can use it for power either way. I want to be able to see which way the switch is toggled using a digital input.

So the following is not true.

One is that one of those input pins is superfluous as either one or the other will be pulled high, never both and never neither, so you only need one.

Never neither is not true with a center off switch. Also

The second is what I just explained before - you should not be pulling an input to a voltage higher then the Vcc which in this case is lesser than the battery voltage by the voltage drop of the diode.

So use 4 diodes. Add one to each input or a voltage divider.

steinie44:

One is that one of those input pins is superfluous as either one or the other will be pulled high, never both and never neither, so you only need one.

Never neither is not true with a centre off switch.

Actually this is the case.

When the switch is off, both inputs would be pulled to Vcc, therefore both true.

{And simultaneously, false, as it happens! :astonished:}

@Paul__B

Actually this is the case.

When the switch is off, both inputs would be pulled to Vcc, therefore both true.

{And simultaneously, false, as it happens! smiley-eek}

Ha Ha LOL......... You don't know what a 3 position On Off On switch is!

When you set this up, did you use a center off switch?

steinie44:

Actually this is the case.

When the switch is off, both inputs would be pulled to Vcc, therefore both true.

{And simultaneously, false, as it happens! smiley-eek}

Ha Ha LOL......... You don't know what a 3 position On Off On switch is!

When you set this up, did you use a center off switch?

Yes. It is a center off switch. So power will be applied to the whole thing in either switch position. I want to know which position the switch is in after power starts the ATTiny85. Since there are only two modes it is not necessary to sense both positions. I did set up a test circuit (which I will post soon) that uses the switch to supply power to my main circuit (in my test case just a red LED) and displays the switch position by lighting a green LED for mode 1 and a yellow LED for mode 2 using just 2 diodes with the anodes connected to each switch leg and the cathode tied together to supply the positive voltage to the circuit.

It works but instead of lighting an LED I want the microcontroller to "see" which mode is selected.

Thanks! You are really helping me out and stretching my brain!

@dmilton2004
That last post was directed at Paul__B
Not you.

Glad you got it.
Ok if you just connect where you have the LED(s) to one or two input pins then you’re all set.
Then just read the pins or pin in your sketch with digitalRead(pin);

True, if it’s not one way, must be the other way.

steinie44: Ok if you just connect where you have the LED(s) to one or two input pins then you're all set. Then just read the pins or pin in your sketch with digitalRead(pin);

Ok here is an update to my circuit. From what everyone has posted, this is what I came up with:

I hope I am one the right track with this! :)

I appreciate all the input and help. Let me know if anyone sees anything missing.

Thanks in advance!

dmilton2004:
I hope I am one the right track with this! :slight_smile:

Unfortunately, not quite yet.

On two counts.

One is that as you correctly stated before,

dmilton2004:
Since there are only two modes it is not necessary to sense both positions.

So you do not need to sense both sides of the switch, only one. If it is not one, it must be the other. There is no other option!

The second problem is that you have no pull-down resistors to pull the “sense” line (that only one) down when it is not pulled up by the switch, so you are quite likely (very likely I suspect) for it to read HIGH even when the switch is in the other position. What you need - most simply - is a 4k7 resistor in the “R1” position, and a 47k pulldown from that input pin - PB5- to ground. The 4k7 will prevent the port input from being pulled higher than Vcc.

I am having a grand semantic leg-pull with steinie44 here, as he has (apparently?) not realised the consequence of the fact that the centre position is in fact, OFF. With no power to the circuit, if you had resistors connected to both sides of the switch, then in the centre position, both sides of the switch would literally be pulled to a logic HIGH because with no power to the logic, logic HIGH = logic LOW anyway! XD