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.
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.
- Make an Interactive Arcade Bedside Clock
- Isolated transmit/receive loop circuit (XTR105, RCV420)
- How to use ESP8266 to make a mini fully functional clock
- How to make a simple quadcopter using ESP8266
- How to use ESP32 to implement the design of air quality monitoring system
- Circuit design analysis of smart car image recognition system
- Encodable digital lock circuit diagram
- SST89C58 electronic disk circuit and code
- Microcomputer motherboard reset part circuit diagram
- MAX232 connection method
- Microcontroller buzzer control program and drive circuit diagram
- Single chip multi-microcomputer system sharing RAM circuit
- SPI interface and microcontroller interface principle circuit diagram
- Microcontroller control circuit
- Two types of negative voltage generation circuit diagrams of single-chip microcomputer
- Microcontroller measurement circuit
- Light control switch circuit
- Simple battery charging and discharging automatic control device circuit
- IR2130 combined with microcontroller and SLE4520 for three-phase frequency conversion system diagram
- The interface between DAC1231 and microcontroller 8031