Here's are some circuit diagrams for driving relays from a microcontroller. Make sure you're using a 5-volt relay (this refers to the coil, not the load circuit), and make sure that the relay has a high enough rating for the load that you're driving.
This circuit is necessary if you are using a relay with a coil that needs more power than the microcontroller can supply (this includes most miniature electromechanical relays):
Troubleshooting:
• Make sure that you have the pins correctly labeled on the relay; different relays have different configurations.
• The diode protects the circuit in case the polarity is reversed. The stripe on the diode should be towards the 5v side.
• The base pin on the transistor is usually the center pin; consult documentation to distinguish the collector from the transmitter.
• If the relay is working correctly, you should be able to hear a little 'click' when you trigger it (except when using a solid-state relay).
• Make sure that you have the pins correctly labeled on the relay; different relays have different configurations.
• The diode protects the circuit in case the polarity is reversed. The stripe on the diode should be towards the 5v side.
• The base pin on the transistor is usually the center pin; consult documentation to distinguish the collector from the transmitter.
• If the relay is working correctly, you should be able to hear a little 'click' when you trigger it (except when using a solid-state relay).
Some relays (such as reed relays and solid-state relays) have coils that can be switched directly from the microcontroller, in which case you can use a less complex circuit:
Finally: if you're switching an AC device, PLEASE BE CAREFUL. Make sure you've got the coil working BEFORE you hook up the AC load. Make sure you have correctly labeled all the pins on the relay - NEVER connect AC voltage directly to the relay coil. Build your AC circuit BEFORE you plug it in. If you're not sure, ask someone to check it for you first.
very nice