robert1510 0 Posted December 3, 2012 Share Posted December 3, 2012 Hi I'm extremely new to this, but I'm trying to control a 12VDC SPDT relay with my MSP430G2553 for my first project. I have my resistors, diode, transistor, and two LEDs. I think I have the circuitry down, but I don't know where to begin with the code. Any help would be greatly appreciated. Thanks, -Robert Quote Link to post Share on other sites
SirPatrick 35 Posted December 3, 2012 Share Posted December 3, 2012 If the circuit is all hooked up the code is the easiest part. All you will be doing is toggling a pin on/off. So any blink example would work, instead of hooking it up to the LED you will hook it up to the base of the transistor. Here is an example of toggling a pin based on a timer. I used this to control a solid state relay every few seconds. Quote Link to post Share on other sites
chibiace 46 Posted December 3, 2012 Share Posted December 3, 2012 basically all you need to do is turn on the msp430 pin which is connected to the base of the transistor. void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer P1DIR |= BIT0; //Set P1.0 as Output P1OUT |= BIT0; //Set P1.0 HIGH } Quote Link to post Share on other sites
robert1510 0 Posted December 3, 2012 Author Share Posted December 3, 2012 Thank you very much for the help. I can't get enough voltage out of the P1.0 pin to control the 5V relay. I guess my circuit is not correct like I originally thought. Do yall know of any links to projects that control a simple relay. The ones I've found are a little too complex for my project. Quote Link to post Share on other sites
chibiace 46 Posted December 3, 2012 Share Posted December 3, 2012 Thank you very much for the help. I can't get enough voltage out of the P1.0 pin to control the 5V relay. I guess my circuit is not correct like I originally thought. Do yall know of any links to projects that control a simple relay. The ones I've found are a little too complex for my project. if you have a 5v relay, you will need to supply it 5v from a separate 5v supply. connect both the gnds together from the 5v supply and msp430, p1.0 connects through a resistor to the base of the npn transistor. when p1.0 goes high it will allow the 5v to flow through the relay then through the transistor to gnd which will turn on your relay. the 3.3v and 5v are not connected as the transistor keeps them apart. but the grounds need to be connected. abecedarian 1 Quote Link to post Share on other sites
abecedarian 330 Posted December 3, 2012 Share Posted December 3, 2012 5v to one terminal of the relay coil other terminal of the relay coil to the collector of an NPN transistor P1.0 to the base of the NPN transistor emitter of the NPN transistor to ground It is important that the MSP share the same ground / 0V with the 5v system otherwise the transistor may not switch. +5v ---------\ 8 {relay coil} / {NPN transistor collector} P1.0 ------< {NPN transistor base} \ {NPN transistor emitter} 0V __________/ {common MSP / 5V ground} chibiace posted mostly the same thing, but probably more correct, though I don't know if the diode across the relay coil is really necessary but it will prevent back EMF, nor if the resistor between P1.0 and the transistor base is necessary... however, it will limit current draw through the MSP pin so probably a good thing to have. Quote Link to post Share on other sites
chibiace 46 Posted December 3, 2012 Share Posted December 3, 2012 wait didnt you want to use a 12v relay? anyway same thing, 12v Positive to Relay+ Diode from Relay- to Relay+ Relay- To Transistor Collector Msp430 P1.0 to Resistor Resistor to Transistor Base Transistor Emitter to 12v Ground and Msp430 Ground connected together abecedarian 1 Quote Link to post Share on other sites
robert1510 0 Posted December 4, 2012 Author Share Posted December 4, 2012 I'm using a 12VDC SDPT relay so I'm a little confused about the relay connections. I can only output 3.6V from the microcontroller, I think. So I don't know if that's enough to switch the relay. Quote Link to post Share on other sites
robert1510 0 Posted December 4, 2012 Author Share Posted December 4, 2012 Nevermind I got it. Thank yall so much for the help! Quote Link to post Share on other sites
chibiace 46 Posted December 5, 2012 Share Posted December 5, 2012 when the coil discharges you will probably want that diode there. Quote Link to post Share on other sites
abecedarian 330 Posted December 7, 2012 Share Posted December 7, 2012 What chibiace said about the diode is true.(research relay coil flyback) Dependent on the voltage and current in the relay coil when you switch the coil "off", if you don't have that diode (and maybe an appropriate Zener in series), you may end up with very high voltage/current on the wire to that pin on the MCU. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.