No announcement yet.

8051 micro to control a relay

  • Filter
  • Time
  • Show
Clear All
new posts

  • 8051 micro to control a relay

    **I moved this from general hardware as I believe it belongs here**

    Hi Smart People

    I am finally working on my project that entails the use of an 8051 micro. I have the program all written for it and the simulation is working great. I now need to hook it up to a real life circuit.

    Issue: I have a motor driver controlled by the micro. When I start my vehicle I will obviously have power going to my micro and the motor driver chip but I need it to stay on when I turn off the ignition and then kill the power once the micro program terminates.

    Back ground is I have an automatic screen that will open when I turn on the ignition and it will close when I turn off the ignition. The kicker is when I turn off the ignition I can't lose power to the controller and motor until the screen closes. I would like for the micro to send a signal to a relay to kill the power once it senses that the screen has closed.

    How could I drive a signal to the relay as the digital output of the micro is not enough to drive the coil of a relay.

    Any ideas or thoughts.

    Thanks, Chuck

  • #2
    You normally drive a relay with a microcontroller using a transistor: just a simple BJT, a darlington pair or a power mosfet (it depends how much current your relay draws). Here's an example circuit: Don't forget to put in the back EMF diode across the relay coil, otherwise you'll eventually burn out the transistor.

    Looking at your problem though, if i was you i'd have power going to the micro all the time and have the micro sit in sleep mode with an interrupt pin connected (indirectly via a transistor) to your accessory line. When your accessory line changes, power your motor and open / close the automatic screen.


    • #3
      Thanks a ton for the reply. I did manage to find a few driver circuits after I posted but none like the one you posted. The one I was going to follow is shown in tip 9 on this page I think this will work as well.

      Your idea to keep the micro on but in sleep is interesting. The reason I wanted to keep it on was to not risk running my battery down if it were parked for a week or so at the airport (for instance). I'm thinking to use a couple relays one NC and one NO so when I start the vehicle current will flow latching one relay and then then I could send a signal when it's ready to shut itself down. I may need more than two relays for this but I'm just brainstorming at this time.

      My program is written to also include a bypass switch. I can put it into manual mode so if I'm driving and I want to hide the display I can switch over to manual use a push button to close the display. THe program works like a charm right now on my simulation board. Now I'm trying to apply it in real world. A challenge but it's getting there. I'm a little stuck on this relay stuff for they can become confusing.

      So what I desire is to run the circuit on Start and kill the circuit upon program termination. There would be no draw on my battery.

      Keep the ideas and thoughts coming as this can become very productive for meto bounce this stuff off of other folks.

      thx, Chuck


      • #4
        There are a number of ways you can drive a relay. There are some good driver chips like the one mentioned above. I have never used one personally abut they might be the best option. In the past i have used a 2n3904/2n3906 tansistor with an appropriate resistor. If you do this you will want to install a beefy diode in series with the relay coil so that it is normally reverse biased and does not conduct current. The relay coil has an inductance... a rathar large one at that. when power is switched to it, it can develope some pretty high reverse oltage across it which can damage the curcuit driving it. The diode will absorb this and protect everything else.

        OO a google search turned up a better explanation of the transistor approach


        • #5
          Originally posted by Rickertsen2
          If you do this you will want to install a beefy diode in series with the relay coil so that it is normally reverse biased and does not conduct current. The relay coil has an inductance... a rathar large one at that. when power is switched to it, it can develope some pretty high reverse oltage across it which can damage the curcuit driving it. The diode will absorb this and protect everything else.
          I think you made a typo and meant parallel to the relay coil.

          Use a schottky diode across the coil, they're faster than standard power diodes. Make sure you overspec the schottky voltage by maybe 3x, because schottkys have a lower breakdown voltage (check datasheets for this) and if your relay's switched on for long periods of time you may kill the diode.


          • #6
            I think I got the relay circuit licked. I'm using 3 relays; One to issue 12 volts to my 8051 board when the ignition is turned on, one to latch 12 volts for when I turn the ignition off my screen will close, one to receive the signal from the 8051 when the screen is closed and will kill the power to the latched relay (maybe this sounds confusing). I'll post schematics once I find my circuit design software from school

            I decided on a 2N3904 NPN Transistor for the driver along with a 2.2K resistor in series between the 8051 I/O and the Base of the transistor. I'm reading that with the 8051 I may need a pull up resistor but I'm finding mixed results on the net so I'll try it without and if I can't drive the transistor I'll try the pullup.

            On paper this seems to work. I'm currently breadboarding it and should have it tested over the weekend.


            • #7
              Hi Folks,

              Wired up my circuit tonight, bypassing the micro for the moment, and bingo I got the motor turning and the relays latching as they should. The proto was working thus far.

              Problem I ran into was using the 3904 transistor as a relay driver. I have the emitter going to ground, I have the base going through a 430ohm resistor (for testing this is hooked to logic 5V vcc), and the collector is going to the coild of the relay. I was able to eliminate one relay so the way I have the coil is one end going to 12v battery and the other going to the collector. When I drive 5V to the base of the transistor the relay will latch but then when I ground the base it will stay latched. For some reason the transistor will not stop conducting once I drive the base low.

              TO test it I disconnected the collector from the relay coil and using a meter I can switch the transistor off and on. SO this is telling me that the coil current may be keeping the transistor on even after I tie the base low. I put a volt meter across at the collector of the transistor and sure enough I have about 7V reading on the meter. COuld this be keeping my transisor on?

              I was think of maybe using a diode between the collector and the relay coil. THis may prevent that voltage from possibly keeping my transistor on.

              Any ideas what may be happening to keep my transistor from switching as should? I've read a lot of sites and I seem to have everything hooked correctly and the transistor does switch on when I send a high to it but it stays on even when I gound it low.



              • #8
                I'm not too sure what that could be, but there's a few things to try:
                Swap a new transistor in, because you might have a bad one
                Make sure the transistor's pins are wired up correctly: transistors sometimes work if you mix up the pins, but have reduced hFE and reduced lifespan


                • #9
                  As STARFOX says check the pinout. the 2n3904 is manufactured with 2 variants depending on manufacturer.

                  The other detail to remember is the 8051 ports go to 'ff' when the part is reset, this will turn on the relays until you write '00' to the port lines. The time it takes to do this may not cause a problem but it is worth remembering.



                  • #10
                    Thanks for the reply's.

                    I got it all working last night. I ended up burning out my 3094 transistor because mistakening put the diode in backwards while debugging. THis actually worked out for the best because I had a 2N2222A can transistor. I put that in with a 1K resistor and it works like a champ.

                    I found out I need one more transistor. I have a sense line sensing when I have the ignition turned on but it brings the line high. I couldn't get my program to terminate correctly. I tested out the sense line by grounding it instead and this worked. I'm going to use another 2n2222 transistor to bring this line low when the ignition is on. This should do the trick and I'll have it all working on the breadboard (I hope!).

                    Thanks for the help and ideas everyone.