Electromagnetic relay control design based on PIC16F87X
2026-04-06 09:41:09··#1
Abstract: To ensure good circuit isolation and personnel safety, an intelligent electromagnetic relay control circuit is introduced. After describing the working principle of the electromagnetic relay, a hardware circuit for controlling the electromagnetic relay is designed using a PIC16F87X microcontroller as the core, and the control program flow is given. This control circuit has a simple structure, reliable operation, and certain practical value. Keywords: PIC16F87X; electromagnetic relay; activation; deactivation 0 Introduction Electromagnetic relays are a commonly used component in automatic control. They utilize electromagnetic principles to automatically switch one or a group of contacts, and are widely used in electronic equipment, instruments, and automation equipment. However, in various automatic control devices, there is a problem of interconnecting low-voltage automatic control circuits with high-voltage electrical circuits: on the one hand, the control signals of the low-voltage electronic circuits must be able to control the actuators of the high-voltage electrical circuits; on the other hand, good electrical isolation must be provided for the electronic circuits and electrical circuits to protect the safety of the electronic circuits and personnel. Therefore, this paper attempts to complete this bridging role by using a PIC16F87X microcontroller to control an electromagnetic relay. 1 Working Principle of Electromagnetic Relay An electromagnetic relay is a type of relay that generates a predetermined response through the relative movement of mechanical parts under the action of current in the input circuit. Electromagnetic relays are generally composed of a control coil, an iron core, an armature, and contact springs. The control coil and the contact group are mutually insulated, providing good electrical isolation for the control circuit. It mainly works by utilizing the principle of electromagnetic induction. When the rated voltage of the coil is applied to both ends of the relay coil, a certain current will flow through the coil, thereby generating an electromagnetic effect. The armature will be attracted to the iron core by the electromagnetic force, overcoming the pull of the return spring, thereby driving the moving contact of the armature to engage with the stationary contact (normally open contact). When the coil is de-energized, the electromagnetic attraction also disappears, and the armature will return to its original position under the reaction force of the spring, causing the moving contact to engage with the original stationary contact (normally closed contact). The engagement and release of the relay play the role of switching on and off in the circuit. The following points should be noted when selecting an electromagnetic relay: (1) Selection of the rated operating voltage of the relay. When using a relay, you should first understand the working power supply voltage in the circuit where the relay is located. The working voltage of the relay should be equal to this voltage, or the power supply voltage of the circuit should be 80% of the working voltage of the relay. This can also ensure that the relay works normally, but the voltage of the circuit components should not exceed the rated working voltage of the relay, otherwise the relay coil will easily burn out. (2) Selection of relay contact load. The number and form of the contact load should be determined according to the characteristics of the circuit controlled by the relay. Then, the contact capacity should be selected according to the type, voltage and current of the current in the contact control circuit. The contact capacity reflects the voltage applied to the contact and the ability of the current to pass through the contact. Generally, the load of the contact should not exceed the contact capacity. (3) Selection of relay coil power supply. In general, the circuit design uses DC relays. The type of relay can also be considered according to the characteristics of the control circuit. The power consumed by the relay should also be considered. (4) Selection of relay action time. The action time and release time of the relay should be selected according to the actual circuit's time requirements for the action of the controlled object. Electronic components can also be added to the relay circuit to accelerate or delay the action and release time of the relay to meet different requirements. (5) Working environment conditions. When selecting a relay, environmental temperature, service life, and impact vibration should also be considered. 2. PIC16F87X Control Circuit Design 2.1 Characteristics of PIC16F87X The PIC16F87X series 8-bit microcontrollers from Microchip are high-performance embedded microcontrollers using a Reduced Instruction Set Computing (RISC) architecture and Flash memory technology. They feature fast operation, rich expansion capabilities, powerful functions, clear logic structure, and in-system programming, power-on and power-off reset protection, and watchdog timer functions. 2.2 Relay Control Circuit Principle Based on the many characteristics of the PIC16F87X, a hardware circuit for controlling a relay using the PIC16F87X was designed (see Figure 1). Figure 1. Schematic diagram of PIC16F87X control relay circuit. When the RD0 pin of the microcontroller outputs a low level, the optocoupler U1 is cut off, there is no potential difference across the relay coil, the relay armature is released, and the normally open contact of the relay is released, equivalent to the switch being open. When the RD0 pin of the microcontroller outputs a high level, the optocoupler U1 is saturated and conducting, the 24V power supply is applied across the relay coil, the relay is energized, and its normally open contact is closed, equivalent to the switch being closed. At the instant the optocoupler is cut off, the current in the coil cannot suddenly become zero, and a relatively high induced electromotive force will be generated across the relay coil. A diode D1 should be connected in parallel across the coil to release the induced electromotive force generated by the coil through D1, protecting the transistor from breakdown and eliminating interference from the induced electromotive force to other circuits. 2.3 PIC16F87X Control Software Design The PIC16F87X control software is developed in C language under the MPLABIDE environment and compiled using the PICC compiler developed by Hitech. The program flow is shown in Figure 2. The source code of the relay control software is as follows: #include #include __CONFIG(WDTDIS&PWRTEN&LP&WRTEN&LVPDIS); void delay(void); main() {INTCON=0X00;TRISD=0X00; here: PORTD=0X00;delay(); PORTD=0Xff;delay();gotohere;} void delay() {intti; for(i=0;i<=60000;i++) continue;return;} After the source program is compiled, it is programmed into the PIC16F87X through ICD2. When the control circuit is powered on, the relay repeatedly turns on and off within a specified time. You can hear the relay "clicking" continuously. The signal output by RD0 is observed with an oscilloscope as shown in Figure 3. 3 Conclusion The electromagnetic relay controlled by the I6X microcontroller uses an optocoupler to drive and isolate the relay; the diode freewheeling current is used to prevent the high reverse voltage that occurs when the current in the relay coil is cut off, thus protecting the device driving the relay. The circuit design is simple and reliable, and has certain application value. 【References】 [1] Liu Duren. PIC Software and Hardware System Design - Based on PIC16F87X Series [M]. Beijing: Electronic Industry Press, 2005. [2] Huang Jichang. Electronic Components Application Manual [M]. Beijing: Posts and Telecommunications Press, 2005. [3] Liu Heping. PIC16F87X Microcontroller Practical Software and Interface Technology - C Language and Its Application [M]. Beijing: Beijing University of Aeronautics and Astronautics Press, 2003. [4] Zhao Jingying, Lu Jianguo, Wang Jingqin, et al. Research on Electronic Overload Protection Relay [J]. Relay, 2004, 32 (16): 41243. Click to download: Electromagnetic Relay Control Design Based on PIC16F87X Editor: Chen Dong