Skip to main content

social

pop

Interfacing a relay using a microcontroller

Interfacing a relay circuit with a PIC microcontroller

We are going to see how to interface a relay to a PIC micro controller. But before that, you need to know what is a relay.
A relay is a switching device which permits the switching of a load in a circuit or to a supply. The advantage of using a relay is due to the fact that, this device can permit the switching of high loads in circuits.
Before interfacing a relay in a circuit, the relay must be chosen to suit the supplying circuit and the required load. For example, there are relays which operate at 5V, 6V, 12V, etc. and having a maximum load current and voltage rating.

Materials

PIC 16F877A, 5V relay, 5V DC source, 1 BC547 transistor, 1 diode 1N4148, 230V AC load (heater), 230V AC source, 8MHz Crystal Oscillator, 2 ceramic capacitors, 2 resistors, 1 push button.

Circuit diagram of relay with microcontroller

The circuit diagram is shown below.

interfacing a relay
The relay is connected to PORTC of the microcontroller at Pin RC3.

The push button to activate the relay which switches on the load is connected to PORTB of the microcontroller at Pin RB0.
A flyback diode is connected to the relay to protect it against back emf.

 Interfacing a relay with microcontroller Code

This code is written in C with the software: mikroC pro.
See the image of the code below
code interfacing a relay
You can copy the code below
void main() {
TRISB=0xFF;
TRISC=0x00;
PORTC=0X00;
while(1) {
if (PORTB.RB0==0)
     {PORTC.RC3=1;}
else {PORTC.RC3=0;}
         }
            }

Explanation of Code

TRISB = 0XFF; means configuring PORTB as input (FF means 1111 1111 in digital)
TRISC=0x00; means configuring PORTC as output (00 means 0000 0000 in digital)
PORTC=0X00; means putting PORTC OFF (initialized)
while(1) { some code here} means taking a loop. The code in the braces is executed infinitely.
if (PORTB.RB0 = =0) {PORTC.RC3=1;} means if Pin RB0 is pressed (Pin is at logic 0), put on the relay connected to Pin RC3.
else {PORTC.RC3=0;} means if Pin RB0 is not pressed (Pin is at logic 1), put OFF the relay connected to Pin RC3.
Once the code is imported into the PIC microcontroller, the system behaves as such:

The relay is initially OFF. Hence the heater OFF (blue).  See below
interfacing a relay with PIC microcontroller


Once the push button connected to Pin RB0 is pressed, the relay is activated switches the load to the 230V supply and the heater is ON and starts heating. See below

The heater heated to 75%


interfacing a relay with microcontroller



Heater heated to 100%

Interfacing a relay with PIC microcontroller


When the push button is pressed again, the relay is disactivated and switches of the load (heater).

Related: 


Interfacing 16x2 LCDInterfacing 16x4 LCDScrolling text on LCD


Interfacing infrared distance sensor

Interfacing a pH meter







Popular posts from this blog

GCE QUESTIONS AND SOLUTIONS FOR ELECTRICAL POWER SYSTEMS

 DOWNLOAD GCE PAST QUESTIONS AND SOLUTIONS FOR EPS POWER ELECTRONICS  Download 1     Download 1b Download 2     Download 2b      Download 2C Download 3 AUTOMATIC CONTROL OF ELECTRICAL MACHINES Download 1 Download 2 Download 3      Download 3b Download 4      Download 4b    2020 DESIGN OF ELECTRICAL INSTALLATION Download 1 Download 2 Download 3      Download 3b      Download 3C      Download 3D CIRCUIT ANALYSIS Download 1 Download 2 Download 3 Download 4 ENGINEERING SCIENCE Download 1 2019 Download 2 2023 paper 1 Download 3   MOCK      Download 3B  Download 4         Download 5          Download 6 Download 7   ITV ELECTRICAL AND ELECTRONIC CIRCUIT  Download 1 ELECTRICAL DIAGRAM Download 1   CAP ELECTRICAL TECHNOLOGY AND DIAGRAM Download 1 Download 2   ITV ELECTRICAL POWER SYSTEMS SOLUTIONS Download Solutions ELECTRICAL MACHINES  Download 1  ITV Download 2   MOCK Download 3   EE4 ENGINEERING DRAWING Download 1 ITV SOLUTIONS CIRCUIT THEORY Download 1

Download GCE syllabus | scheme of work all subjects

GCE syllabus secondary education Cameroon  570 Mathematics ♻️DOWNLOAD 570 Math 📲 575 Additional Mathematics ♻️DOWNLOAD Additional Mathematics📲 775 Further Math 🆓 Download Further Mathematics📲 Physics Ordinary Level 🆓 Download Physics Ordinary Level 📲 Physics Form 3-5 🆓 Download Physics Form 3 to Form 5 📲 Physics New Form 1 to 2 🆓Download Physics Form 1 and 2 📲 530 English Language 🆓 Download English Language 📲 540 Food and Nutrition 🆓DOWNLOAD FOOD AND NUTRITION 📲 545 French 🆓DOWNLOAD FRENCH 📲 590 Logic 🆓 LOGIC 📲 Chemistry Form 1 to Upper Six 🆓 DOWNLOAD CHEMISTRY Form 1 to U6📲 Chemistry Second Cycle 🆓Download CHEMISTRY Second Cycle📲 Geography  🆓DOWNLOAD GEOGRAPHY 📲 Geography Form 3-5 🆓DOWNLOAD GEOGRAPHY Form 3 to 5📲 History, Geography, Citizenship Form 1-2 🆓Download HISTORY, Geography, CITIZENSHIP📲 Geology 🆓Download  GEOLOGY 📲

GCE Advanced Level Past Questions Power Electronics

  GCE Advanced Level Past Questions   Technical and Vocational Education Examinations Electrical Power Systems EPS Power Electronics Specialty Name (Specialty Code) Electrical Power Systems EPS (F3) Subject Title   Power Electronics Paper Nº   2 Subject Code Nº 7235  1.  Control of a D.C motor          The circuit shown in figure 1, include the control and the power circuit of a DC motor to make the up and down movements of a system. All the OP-AMP and switches are ideal a)   Study of the control circuit (OA1; R’s and (AO2). i)       Give the operating mode of the operational amplifiers (OA1; R’s) and (AO2).                  ( 6 marks) ii)      prove that the expression of the voltage at the OA1  inverting input terminal in function of V 1  and V 01  is  V- = ( V 1  + V 01 ) / 2         (6 marks) iii) Prove that the expression of the voltage a