Relay Interfacing With Microcontroller using ULN2003A

Hello friends, I hope you all are doing great. In today's tutorial, I am going to explain the Relay Interfacing with Microcontroller using ULN2003A. In the previous lecture, we have discussed the detailed Introduction to Relay along with its working. Now we are going to practically interface the relay with a microcontroller to design an automatic switch. Relay is a key component in almost every electronic circuit. It can be used as a switch and can also be used as a voltage regulator.

The microcontroller I am going to use here is PIC18F4552 but you can use any other Microcontroller. You just need to change the syntax of coding but the logic will remain the same. Here, I am using ULN2003A to control the relay and from this relay, we can control anything.

So, let's get started:

Where To Buy?
No.ComponentsDistributorLink To Buy
1LEDsAmazonBuy Now
2RelayAmazonBuy Now
3ResistorAmazonBuy Now
4ULN2003AmazonBuy Now

Relay Interfacing With Microcontroller

  • I am using a 12V relay, meaning we need to provide a 12V at its input coil, in order to get it energized.
  • In simple words, when we send the +12V signal from our PIC microcontroller, it will actuate the relay coil and the relay output gets connected and when we make the input LOW, the coil de-energized.

Now, the question is ???

  • But the real question is PIC gives 5V at its high signal but the relay operates at 12V so how to convert this 5V signal into 12V?

What's the Solution ???

  • The solution to this problem is ULN2003A.
  • ULN2003A is used in between the PIC and the relay, so when the PIC sends the HIGH signal i.e. 5V, ULN converts it to 12V, sends it to the relay and the relay gets actuated.

Circuit Diagram of Relay with ULN2003A

  • Here's the circuit diagram for this complete project:

  • Resistance R1 is used as a pull-up resistance.
  • LED is used for the indication, when the relay is actuated LED goes ON otherwise OFF.
  • The programming portion is not much, just send high and low signals from PIC to ON and OFF the relay.

That's all for today guys. If you guys have any problem in any part of this tutorial ask in the comments, and I will reply to your queries. Till next tutorial ALLAH HAFIZ .... :))

LED Blinking Project using PIC16F877a

Hello friends , I hope you all are fine and enjoying good health. In today's tutorial, I am going to design an LED Blinking Project using PIC16F877a. In the previous chapters, we have seen all the basics of PIC Microcontroller and now we are ready to design our first project on PIC Microcontroller.

Its a very simple project so the programming level is very basic. We will just add some LEDs on all pins of PortB of PIC16F877a and then we will program it in such a way that these LEDs will blink in different pattern, we can also change the speed of blinking by adding a delay.

I will explain the whole code but if you got any problem may ask in comments.I will recommend you guys to do this project, I know a beginner can't make this project work in first attempt but when you do it, you will make mistakes and will learn a lot. So let's start the LED Blinking Project using PIC16F877a:

LED Blinking Project on PIC Microcontroller

  • You can download the Proteus Simulation and Programming code for LED Blinking Project using PIC16F877a, by clicking the below button:

[dt_default_button link="https://www.theengineeringprojects.com/PICProjects/Led Blinking Project with PIC Microcontroller.rar" button_alignment="default" animation="fadeIn" size="medium" default_btn_bg_color="" bg_hover_color="" text_color="" text_hover_color="" icon="fa fa-chevron-circle-right" icon_align="left"]Download Proteus Simulation & Code[/dt_default_button]

  • Now let's design this project so that you can understand how its done.
  • First of all, design a simulation of LED Blinking Project using PIC16F877a in Proteus as shown in below figure:
Components Used:
These components are used while designing this simulation:
  • PIC16F877a.
  • LED. ( We need 8 LEDs )
  • Resistors. ( We need 2 resistors of 10k ohm )
  • Capacitors. ( We need 2 capacitors of 1nF )
  • Crystal Oscillator. ( 16MHz )
Working Principle:
  • The circuit on the left side of PIC is its basic circuit, we need this circuit in order to power up the PIC and to give it a frequency on which it operates. Like in this diagram, I have used crystal oscillator of 16MHz, which is its frequency of operating. You can operate it at different frequencies e.g. 4MHz ,10MHz , 16MHz, 20MHz etc.
  • But keep this thing in mind that if you change the oscillator then you must change the capacitors as well e.g. for 16MHz the capacitors will be of 33pF.
  • Vdd for PIC is 5V , if you have a 5V battery then its cool but mostly adapters are of 12V. So if you have 12V adapter then use 7805 which converts the 12V into 5V.
  • Now come to the circuit on the right side of the PIC, these are simple LEDs which I have connected with PortB of PIC Microcontroller.
  • On the other side of these LEDs is a resistor just for current control and then a GND (ground).
  • Now when we make any of these pins high then respective LED will turn ON and when we make that pin low, LED will turn OFF.
NOTE :
  • Pin High means its at 5V and LOW means its at 0V.
  • Now let's come to the programming part.Open your MikroC Pro For PIC Compiler and create a new project in it.
  • Make sure that you select PIC16F877a in Device Name and keep the frequency to 16MHz.
  • Watch the video, if you got into any trouble, its attached at the end of this article.
  • Now place the below code in your MikroC Pro for PIC Compiler:
// ************ LED Blinking Project using PIC16F877a
void main() {
trisb= 0x00;

while(1)
{
        portb = 0x00;
        delay_ms(2000);
        portb = 0xFF;
        delay_ms(2000);
}
}
  • Now let me explain the code, it's a very simple code just five to six lines.
  • void main ()  This is the main function of program, the compiler always come straight to this part and leaves the rest it will execute only what is written under its braces.
  • trisb = 0x00; This code will tell the compiler to use the pins as output. If we were using a sensor on any pin then we have to make it 1.
  • while(1) This one runs continuously means pic will execute the code with its braces forever and it never stops.
  • and in the next two lines we make the pin high and then low which is connected to the LED so now the LED will turn ON and then OFF continuously.
  • You can also increase the delay between these lines if you want to increase the duration of ON / OFF.
  • Now Build your project to get the hex file.
  • Upload this hex file in your Proteus Simulation and then run your simulation.
  • If everything goes fine then you will get something as shown in below figure:
  • Looking Pretty, isn't it ? :D
  • Here's the video which will give you better idea of its working:

I have tried my best to explain it in full detail but if still you got any problem or anything left then ask in comments and I will explain it to you. So, that was all about LED Blinking Project using PIC16F877a.

Stay Blessed .... ALLAH HAFIZ :))
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir