Home > Microcontroller >Interface Circuits > Experiment on using 51 microcontroller to drive relay

Experiment on using 51 microcontroller to drive relay

Source: InternetPublisher:子丑寅卯 Keywords: Microcontroller relay Updated: 2023/12/15

In modern automatic control equipment, there are problems with the interconnection of electronic circuits (weak current) and electrical circuits (strong current). On the one hand, the control signals of the electronic circuit must be able to control the actuators of the electrical circuit (such as motors, electromagnets, lights). etc.), on the other hand, it is necessary to provide good electrical isolation for the electrical circuits of electronic circuits to protect the personal safety of electronic circuits and workers. The relay enables this bridge function.

1. Function

The relay is controlled by the microcontroller to engage and release, allowing readers to master the use of relays. Readers can also use the normally open and normally closed contacts of the relay to control the light on and off to achieve "controlling the big with the small".

2. Devices and principles

Relays have a control system (also called an input loop) and a controlled system (also called an output loop) and are usually used in automatic control circuits. A relay is actually an "automatic switch" that uses a smaller current to control a larger current. It plays the roles of automatic adjustment, safety protection, and conversion circuit in the circuit. In most cases, a relay is an electromagnet that can close or open one or several contacts. When a current flows through the winding of the electromagnet, the armature is attracted by the electromagnet, thus changing the state of the contact. Relays can generally be divided into electromagnetic relays, thermal reed relays, solid state relays, etc. The relay is an inductive device, so it cannot be directly controlled by the I/0 port of the microcontroller, and a reverse root protection circuit must be added to the three-pole ring; tube and other control devices.

In general experiments, everyone uses a single-chip microcomputer to drive a relay through a PNP transistor as an electronic switch. The opening and closing of the relay is completely controlled by the base level of the transistor. When the base of the transistor is at a high level, the PNP transistor is turned off and the relay does not work; otherwise, the transistor is turned on and the relay is electrically closed.

3.Hardware circuit

The relevant schematic diagram of the relay experiment is shown below.


Relay experiment schematic diagram

4. Programming
01#include<reg51.h>
02
03sbitRELAY=P1^3;
04
05voidDelay()
06{
07unsignedchari,j;
08for(i=0;i<255;i++)
09for(j=0;j<255 ;j++);
10}
11
12voiDMAin()
13{
14while(1)
15{
16RELAY=0
17Delay();
18RELAY=1;
19Delay();
20}
21}

5. Code analysis

Serial number 1: Header file containing 51 microcontroller register definitions:

Serial number 3: The bit definition relay is I/0 port P1.3;

Serial numbers 5 to 10: Delay function, the specific delay is related to the frequency of the crystal oscillator used;

Serial number 7: Define unsigned variables i, j;

Serial number 8-9: Delay is achieved through self-increasing nested loop execution of i and j;

Serial number 12~21: main function;

Serial number 14: Enter the while loop of the main program;

Serial number 16: relay pull-in;

Serial number 17: Call the delay program;

Serial number 18: Relay release;

Serial number 19: Call the delay program.

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号