Introduction
A relay control circuit is essential for controlling high-power devices using a low-power microcontroller. This project demonstrates how to use the 8051 microcontroller to control a relay, which can then switch on or off an external device such as a light bulb, motor, or fan. Relays act as electrically operated switches that isolate the control circuit from the high-power load, providing safety and ease of control.
Components Required
8051 Microcontroller (e.g., AT89S52)
Relay Module (5V)
Transistor (e.g., BC547)
Diode (e.g., 1N4007)
Resistor (1kΩ)
LED (optional for indication)
16x2 LCD Display (optional for displaying status)
Push Button
Capacitors (33pF, 100μF)
Crystal Oscillator (11.0592 MHz)
Breadboard and Connecting Wires
Power Supply (5V)
Circuit Diagram
The relay is controlled by a transistor, which is driven by the 8051 microcontroller. The diode is used to protect the transistor from back EMF generated by the relay coil. A push button is used to toggle the relay state.
+5V ----- +5V
|
|
RELAY MODULE
+---+
+5V --|VCC|
| |
GND --|GND |
| IN |------- Collector (BC547)
+---+
|
|
Emitter (BC547) ----- GND
Base (BC547) -------- R1 (1kΩ) -------- P1.0 (8051)
8051
+---+
| |
| P1.0 (Relay Control)
| P3.2 (Push Button)
| |
+---+
DIODE
Cathode to Collector (BC547)
Anode to Relay IN
PUSH BUTTON
One end to P3.2
Other end to GND
Pin Connections
VCC to +5V
GND to Ground
IN to Collector of BC547
Transistor (BC547):
Collector to Relay IN
Emitter to Ground
Base to P1.0 of 8051 through 1kΩ resistor
Diode (1N4007):
Anode to Collector of BC547
Cathode to VCC of Relay
Push Button:
One end to P3.2 of 8051
Other end to Ground
LED (Optional):
Anode to P1.1 of 8051 through 1kΩ resistor
Cathode to Ground
8051 Microcontroller:
Connect crystal oscillator and capacitors for clock generation
Software Implementation
The code is written in C using Keil uVision IDE. It involves initializing the ports, reading the push button state, and toggling the relay state accordingly.
#include <reg51.h>
sbit RELAY = P1^0; // Relay control pin
sbit BUTTON = P3^2; // Push button pin
sbit LED = P1^1; // LED pin (optional for indication)
void delay(unsigned int count) {
int i, j;
for(i=0; i<count; i++)
for(j=0; j<1275; j++);
}
void main() {
unsigned char button_state = 0; // Variable to store button state
unsigned char relay_state = 0; // Variable to store relay state
while(1) {
if(BUTTON == 0) { // If button is pressed
delay(20); // Debounce delay
if(BUTTON == 0) { // Confirm button press
button_state = 1;
}
}
else if(button_state == 1) { // If button was pressed and now released
relay_state = !relay_state; // Toggle relay state
RELAY = relay_state; // Set relay output
LED = relay_state; // Set LED output (optional)
button_state = 0; // Reset button state
}
delay(100); // Small delay for debouncing
}
}
Explanation
Initialization:
Ports Configuration: RELAY is configured as an output to control the relay. BUTTON is configured as an input to read the push button state. LED is optional for visual indication of the relay state.
Button Debouncing:
A delay is used to debounce the button press to avoid multiple toggles due to mechanical bounce.
Relay Control:
The relay state is toggled each time the button is pressed and released. The new state is set to the RELAY pin, and the LED is updated accordingly.
Conclusion
This project demonstrates the use of the 8051 microcontroller to control a relay, enabling the control of high-power devices through a low-power microcontroller. The system uses a transistor to drive the relay, providing isolation between the microcontroller and the high-power load. This project is a great way to learn about interfacing relays with microcontrollers and controlling external devices safely and efficiently.
Want us to guide you through your project or make the project for you ?
Create Various Projects
Check out our Free Arduino Projects Playlist - Arduino Projects
Check out our Free Raspberry Pi Projects Playlist - Raspberry Pi Projects
Check out our Free TinkerCAD Projects Playlist - TinkerCAD Projects
Check out our Free IoT Projects Playlist - IoT Projects
Check out our Free Home Automation Projects Playlist - Home Automation Projects
Check out our Free NodeMCu Projects Playlist - NodeMCu Projects
Comentários