Black Lives Matter - Action and Equality. ... Adafruit is part of the Stop Hate for Profit campaign. Adafruit is open and shipping.
0

Switching
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Switching

by erc on Sat Oct 02, 2010 4:20 pm

I need a little help designing a circuit for my Halloween costume. I'm going to have a 9V source and I want to to have a temporary switch that, when pressed, will switch between two outputs and hold that output state until the temporary switch is pressed again.

The switch will be in one of my shoes, so hopefully every step will change outputs until i step again.

Any help is greatly appreciated.
erc
 
Posts: 6
Joined: Sat Oct 02, 2010 3:42 pm

Re: Switching

by franklin97355 on Sat Oct 02, 2010 4:49 pm

Could you tell us a bit more on your project? Like what micro are you using and how do you have it connected to everything else.

franklin97355
 
Posts: 21498
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: Switching

by erc on Sat Oct 02, 2010 5:09 pm

The project is going to be two colors of el wire that change with the switch press.

Well I didn't really have anything specific. I was thinking of using the TI MSP430G2231. I have a test program running now that switches leds on a button press, but the state is changing back after I release the button. After I get the test program I'll use the shoe switch for the button and the output will go to transistors connected to the inverter power supply for the el wire. I guess I'll need a voltage regulator for the supply voltage to the msp430 as well, I'll probably use a LM317 for that, or maybe just some AAs.

Code I have so far:

#include <io.h>

#include <signal.h>



#define LED0 BIT0

#define LED1 BIT6

#define BUTTON BIT3



int main(void)

{

WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer

P1DIR |= (LED0 + LED1); // Set P1.0 & P1.6 to output direction

// P1.3 must stay at input

P1OUT &= ~LED0; // set P1.0 to 0 (LED OFF)
P1OUT |= LED1; // set P1.6 to 1 (LED ON)

P1IE |= BUTTON; // P1.3 interrupt enabled



P1IFG &= ~BUTTON; // P1.3 IFG cleared



eint(); // enable all interrupts

for(;;)

{}

}



// Port 1 interrupt service routine



interrupt (PORT1_VECTOR) P1_ISR(void) {

P1OUT ^= (LED0 + LED1); // P1.0 = toggle

P1IFG &= ~BUTTON; // P1.3 IFG cleared

P1IES ^= BUTTON; // toggle the interrupt edge,

// the interrupt vector will be called

// when P1.3 goes from HitoLow as well as

// LowtoHigh

}


As you can see the interrupt vector is called when the button goes high to low and low to high, so it switches back when I release.
erc
 
Posts: 6
Joined: Sat Oct 02, 2010 3:42 pm

Re: Switching

by erc on Sat Oct 02, 2010 5:20 pm

Well I kind of answered my own question right there. I changed the last line of code in the interrupt wise Or

P1IES ^= BUTTON;

from the Bitwise XOR (^=) to the Bitwise OR (|=) assignment

P1IES |= BUTTON;

now everything is working.

Are there any transistors you can recommend for switching the 9V with the outputs from the MSP430?
erc
 
Posts: 6
Joined: Sat Oct 02, 2010 3:42 pm

Re: Switching

by zener on Sat Oct 02, 2010 10:26 pm

How much current?

zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: Switching

by erc on Sat Oct 02, 2010 11:37 pm

The inverter will be drawing 180+/-10% mA

Would it be best to connect the transistor to the negative battery lead?

Also, what would be the best way to get the 9V from the inverter battery to 3.3V for the msp430?
erc
 
Posts: 6
Joined: Sat Oct 02, 2010 3:42 pm

Re: Switching

by zener on Sun Oct 03, 2010 12:17 am

There are a lot of transistors you could use. Here is a good candidate:

Zetex ZVN4206A. That's N channel so it is low side switching. So you would go from 9V to the inverter to the fet to ground.

For the regulator you could use Microchip MCP1702-3302E/TO

These are both through hole, TO-92, available at Digikey.

zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: Switching

by erc on Mon Oct 04, 2010 7:48 pm

Ok so using the components suggested I came up with this diagram. Does it look ok? or did I read the data sheets wrong.
Attachments
circuit.jpg
circuit.jpg (32.18 KiB) Viewed 2041 times
erc
 
Posts: 6
Joined: Sat Oct 02, 2010 3:42 pm

Re: Switching

by chatham on Tue Oct 05, 2010 3:33 am

Just a quick thought - you probably already know about debouncing buttons, but if you haven't heard of it, you might want to do a quick search. Mechanical switches tend to have a brief period of sort of jittery connection when they get activated. Wikipedia can say it better than I can, but basically, the metal contacts in the switch bounce off each other a bit, so just when a switch is closed, the circuit is completed for a tiny second, is disconnected briefly, gets reconnected, etc., then is finally connected for good. It's not noticeable for you or me, but it can be for a microcontroller. It sees:

Code: Select all | TOGGLE FULL SIZE
HIGH
           -     --    -------------------
          | |   |  |  |
----------   ---    --
LOW


when the switch is closed, and so thinks there are a number of on-off cycles, messing with your circuit. To avoid it, you'll need to have your microcontroller test the switch for a couple of micro/milliseconds and have it wait until it's returning the same value for a brief time before concluding that the switch has really been triggered.
This is one of those problems that if you don't know about it, will have you questioning God Himself as to why your crazy simple circuit is freaking out, so I hope it helps!
OpenChord.org - Open Source kits to let you play Guitar Hero with a real guitar, or build your own Wii/PS3/USB controllers.

chatham
 
Posts: 144
Joined: Thu Feb 04, 2010 2:30 am

Re: Switching

by zener on Tue Oct 05, 2010 3:42 am

erc wrote:Ok so using the components suggested I came up with this diagram. Does it look ok? or did I read the data sheets wrong.
It is interesting you drew your AC inverter as a logic inverter... The main issue I see is you drew the fets as bipolar transistors. With fets you don't need any base resistor, since there is no base, just a gate. Which makes them a little easier to work with. I admit the data sheet on those particular ones is a little sparse. But basically it looks correct. You want the gate to the cpu output pin, the source to ground and the drain to the negative side of your load (inverter).

zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: Switching

by erc on Tue Oct 05, 2010 1:20 pm

Alan, thanks for the heads up about the debouncing, I'm going to be making my own switch out of some conductive material with some foam with holes in it between it, so I'm sure it will give me some issues.

Zener, I just looked up the symbol as I've never really used an inverter, guess I found the wrong one. As for the base resistor (R2), I'll remove it and read a bit more about fets. Thanks for all your help.
erc
 
Posts: 6
Joined: Sat Oct 02, 2010 3:42 pm

Re: Switching

by zener on Tue Oct 05, 2010 3:33 pm

Take out R1 also. The load will limit its own current.

zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Please be positive and constructive with your questions and comments.