Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Use a 2-way / 3-way switch as a PIC mode selector

Status
Not open for further replies.

Hippogriff

Member
Hi,

I have one of those switches with I / O / II on it, where the switch can be off or on or on again. I wanted to use this switch as a startup mode selector for a PIC... either the PIC is off, easy enough... or the PIC is on in mode 1 or on in mode 2, depending on which switch output it gets power from. At startup I could check which on it is and act accordingly.

I figured - wrongly - that what I would do is wire up each of the switch outputs (I and II) to the VDD pin of the PIC and then 'split' or 'double' the output of the II switch output into pin A5 of the PIC, which I'd set to be an input and, at startup, I'd do a quick boolean check - if that pin is low or high, then go into the program mode I want.

Obviously - now that I think about it - the A5 pin is high all the time that either I or II is selected on the switch because the PIC is always getting 5V on its VDD pin and the electrons will run down the wire coming from I into VDD but back up the wire leading to II on the switch and then back down to VDD on the PIC but also down the 'split' wire coming from II on the switch into A5 on the PIC.

Yes, I think that makes sense... so pin A5 on the PIC always sees high if I or II is selected.

Is there a way I can make this work?

I think this is basic stuff, I apologise... I'm much more of a programmer than I am an electronician.
 
Center off switches (generally those marked On/ Off/ On) could be used to do what you want. Just place a resistor from your A5 pin to VSS/ GND and and tie your "mode 2" on switch position to both the A5 pin and VDD. That way when your "mode 1" position is selected, only VDD has +V, the A5 pin will see VSS through the resistor.

Measure with your meter and make sure that it really is a center off switch :)
 
I figured this too... and I thought I had tried it! Maybe incorrectly. Can you make head or tail of this picture?

Switch2Way.jpg


If the switch is in the O position then obviously the circuit doesn't receive any power at all... but if in either the I or II position I get a reading of 5V on either output pin of the switch, as the electrons are coursing down the output and then back up (as both pins are connected at PIC VDD) I suppose. The addition of the 10K resistor connecting A5 to VSS seems to not change the behaviour or what I measure... so I must have done it wrong.
 
Forgive me, my brain hasn't had it's coffee this morning. You also need to add a diode between the switch contacts, with the cathode on your "mode 1" switch position. It is the junction of the diode and your "mode 1" switch position that goes to VDD on the PIC. The other side of the diode is on your "mode 2" switch position, and it is this junction that goes to your A5 pin.

Schema.jpg
 
Last edited:
Would you believe that a diode is not something I have in my electronics kit-bag..? Never needed to use one!
 
Well, you already know what I tried next... I stuffed a LED in there and, yes, it works. Thank you very much. I also understand why it works. Which ain't bad. Bit miffed at the extra components I'll need to use to get this mode-choose functionality... and I do so hate using resistors and I'll need to buy some proper diodes, but I guess I should've always had some.
 
While technically you could use an LED in place of a standard diode, the voltage drop created by it would likely be too high. Ideally, if you are planning on running your circuit at 4V or less, you would be wanting a schottky type with a low forward voltage drop. However, for 5V operation, pretty much all of the PIC devices will run at 4V, so virtually any standard silicon diode would do you, if it can handle your current requirements. An 1n41418, 1n918 or something similar would probably be fine if it's just a few LED's your driving. The only thing you have to keep in mind here, is that if you are driving other devices that are voltage sensitive and would be expecting 5V on their inputs, in your "mode 2" position, the PIC is only being fed with approximately 4.3V because of the diode drop, the output on the PIC will be slightly lower than this due to voltage drops internally in the device.

Anyway, glad you got it working :)
 
Thanks. I just ordered some bog-standard diodes now... 10 x 1N4001 for 99p from eBay... I'm powering a WS2812B LED ring that wants 5V... I may need something more specialised then? Or a whole new approach... a simple on / off switch and another means of selecting mode? Using the LED just tested the theory for me.
 
If it's the Adafruit Neopixel ring thingy that uses the WS2811 chip that you plan on driving, it should be quite happy with the slightly reduced voltage at it's input. It has an input spec of 0.5V - VDD+0.5V, so you should be golden :)
 
It doesn't matter... the LED ring will be connected directly to 5V. I just need to power the PIC which is no bother, the PIC just dumps out bits to the ring via a pin. This will work very well, I reckon.
 
An interesting observation... well, for me.

If my circuit with 2 modes has a potentiometer in it, and I perform AD conversion on its position, I get a different result when it tops-out, depending on which 'mode' I'm in... I assume this is because of the voltage drop that's introduced by the diode.

If I'm in one mode, then the potentiometer reads 0 when turned fully left, and it reads 255 when turned fully right.

If I'm in the other mode, then the potentiometer reads 0 when turned fully left, but it reads 255 when it's about three quarters of the way around.

So, if you turn the potentiometer to the point where it reads 255 in the second mode, and then switch to the other mode, the potentiometer reads 222 - and you've not moved it at all, the dial is three quarters of the way around and reads either 255 or 222 depending on what mode you're in.

I think I'm going to have to investigate the ADPREF bits of ADCON1 more... at the moment I've just set them to 00 when I do AD conversion and it seems this is not good enough.

It looks like "10 = VREF+ is connected to external VREF+ pin" is my best bet, but I'm not sure what I'm going to need to do to achieve that. It looks like, maybe, I connect pin RA1 of the PIC to the unmolested +5V coming directly in from the power supply? Or would I be better trying to figure out what "11 = VREF+ is connected to internal Fixed Voltage Reference (FVR) module" really means... I've never heard of FVR Buffer1 before.
 
If I set ADPREF to 10 and connect pin 12 of the PIC (RA1 / VREF+) directly to +5V, then the circuit gets power even when the switch is in the off position.

Is that right? That has confused me!

If I set ADPREF to 11 then I believe it's using an internal voltage reference of +4V and I can confirm that the potentiometer reads 0 when fully left in both modes, but then 255 when at any other point (just after fully left) in both modes! So I think the FVR does not work by default, need to figure out what I may need to do to enable it... datasheet out.

Sitting here, scratching my head a bit.
 
OK, set FVREN to 1 and then ADFVR to 11 and I get the same reading from the potentiometer in both modes. It reads 0 when fully left and 255 when about three quarters of the way around, which stays at 255 (obviously) until fully turned to the right.

Is there a way I can successfully use the +5V coming in from the power supply so my potentiometer would read 0 when fully left and 255 only when fully right in both modes?

Do I need to do something different than set ADPREF to 10 and connect the VREF+ pin of the PIC to that incoming +5V? I thought I'd tried that, but I did not get any joy... the biggest problem being that the circuit was powered even when the switch was off!

This is the best result I've got, but it's not what I really want.
 
You will need to create a stable reference external to your PIC and use that for your pot and then scale your A2D accordingly. The reference has to be independant of your PIC VDD because of the voltage drop created by your mode select blocking diode, as you have discovered. Look for a small bandgap type device in the 3.3-4.096V range, or if dead on accuracy is less important, create a small zener diode/ resistor reference and use that instead. For the likes of an intensity, volume or speed control type thing, you really only need an approximation of pot position as they are all pretty sloppy in use anyway :)
 
You need 3 diodes, 2 from the two sides of the switch to Vdd and 1 from one side to A5. This way the pic will always see 4.3V. If you don't have the one to A5 then you might power the chip through the protection diodes of A5 and burn them out.

Edit, if you switch the ground side then you might be able to use the internal weak pullup resistors depending on the chip.

Mike.
 
Guys! You're frightening me... I hate adding extra components to stuff. I like to keep it as simple as possible. Instead of having an on / off / on switch I've decided to just go for a mode select push button... now not related to the power at all. So the PIC (indeed, everything) gets that nice 5V from the USB cable and the latching push button and a resistor controls the mode selection into pin A5. It all seems fine and dandy... my three potentiometers all read 0 to 255 from full left to full right so I'm happy... if I press the button the PIC changes mode as it's checking what the mode pin is each time around the loop. Sorted.

That's what I've gone and done. I still learned what a diode is and does, so that's very cool for me, and I thank you all for that... but I don't need it any more.
 
If you are able, connect your potentiometer to the PIC power supply pin instead.

That way the PIC's vref is the same as the pots power supply and you will always get a 0-255 result regardless whether your supply is 4.3v or 5v.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top