Jump to content
43oh

Controlling a Relay


Recommended Posts

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

Link to post
Share on other sites

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.

 

Link to post
Share on other sites

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

}

Link to post
Share on other sites

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.

Link to post
Share on other sites

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.

 

arduinorelay.jpg?w=400&h=361

 

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.

Link to post
Share on other sites

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.

Link to post
Share on other sites

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.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...