Technology Sharing

STM32 Intelligent Access Control System Tutorial

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Table of contents

  1. introduction
  2. Environment Preparation
  3. Smart access control system basics
  4. Code implementation: Implementation of intelligent access control system 4.1 Data acquisition module 4.2 Data processing and control module 4.3 Communication and network system implementation 4.4 User interface and data visualization
  5. Application scenario: access control management and optimization
  6. Problem Solving and Optimization
  7. Ending and Conclusion

1 Introduction

The intelligent access control system combines various sensors, actuators and communication modules through the STM32 embedded system to achieve real-time monitoring, automatic control and data transmission of access control. This article will introduce in detail how to implement an intelligent access control system in the STM32 system, including environment preparation, system architecture, code implementation, application scenarios, problem solutions and optimization methods.

2. Environmental Preparation

Hardware Preparation

  1. Development Boards: STM32F4 series or STM32H7 series development board
  2. debugger:ST-LINK V2 or onboard debugger
  3. sensor: Such as RFID card reader, fingerprint sensor, PIR sensor
  4. Actuator: Such as electromagnetic lock, buzzer, LED indicator
  5. Communication Module: Such as Wi-Fi module, ZigBee module
  6. Display: Such as OLED display
  7. Button or knob: For user input and settings
  8. power supply:Power Adapter

Software Preparation

  1. Integrated Development Environment (IDE):STM32CubeIDE或Keil MDK
  2. Debugging Tools:STM32 ST-LINK Utility或GDB
  3. Libraries and middleware: STM32 HAL library and FreeRTOS

installation steps

  1. Download and install STM32CubeMX
  2. Download and install STM32CubeIDE
  3. Configure STM32CubeMX project and generate STM32CubeIDE project
  4. Install necessary libraries and drivers

3. Basics of Smart Access Control System

Control system architecture

The intelligent access control system consists of the following parts:

  1. Data acquisition module: Used to collect access cards, fingerprints and motion data
  2. Data processing and control module: Process and analyze the collected data to generate control signals
  3. Communications and Network Systems:Realize the communication between the access control system and the server or other devices
  4. display system: Used to display system status and control information
  5. User Input System: Set and adjust via buttons or knobs

Functional Description

Access card, fingerprint and motion data are collected through various sensors and displayed in real time on the OLED display. The system realizes monitoring and automatic control of access control through data processing and network communication. Users can set it by buttons or knobs, and check the current status through the display.

4. Code implementation: Implementing the intelligent access control system

4.1 Data Acquisition Module

Configuring RFID Reader

Use STM32CubeMX to configure the UART interface:

  1. Open STM32CubeMX and select your STM32 development board model.
  2. In the graphical interface, find the UART pin that needs to be configured and set it to UART mode.
  3. Generate code and import it into STM32CubeIDE.

Code:

  1. #include "stm32f4xx_hal.h"
  2. UART_HandleTypeDef huart1;
  3. void UART1_Init(void) {
  4. huart1.Instance = USART1;
  5. huart1.Init.BaudRate = 9600;
  6. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  7. huart1.Init.StopBits = UART_STOPBITS_1;
  8. huart1.Init.Parity = UART_PARITY_NONE;
  9. huart1.Init.Mode = UART_MODE_TX_RX;
  10. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  11. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  12. HAL_UART_Init(&huart1);
  13. }
  14. uint8_t Read_RFID_Card(uint8_t* buffer, uint16_t size) {
  15. return HAL_UART_Receive(&huart1, buffer, size, HAL_MAX_DELAY);
  16. }
  17. int main(void) {
  18. HAL_Init();
  19. SystemClock_Config();
  20. UART1_Init();
  21. uint8_t rfid_buffer[16];
  22. while (1) {
  23. if (Read_RFID_Card(rfid_buffer, 16) == HAL_OK) {
  24. // 处理RFID数据
  25. }
  26. HAL_Delay(1000);
  27. }
  28. }
Configuring the Fingerprint Sensor

Use STM32CubeMX to configure the UART interface:

  1. Open STM32CubeMX and select your STM32 development board model.
  2. In the graphical interface, find the UART pin that needs to be configured and set it to UART mode.
  3. Generate code and import it into STM32CubeIDE.

Code:

  1. #include "stm32f4xx_hal.h"
  2. UART_HandleTypeDef huart2;
  3. void UART2_Init(void) {
  4. huart2.Instance = USART2;
  5. huart2.Init.BaudRate = 57600;
  6. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  7. huart2.Init.StopBits = UART_STOPBITS_1;
  8. huart2.Init.Parity = UART_PARITY_NONE;
  9. huart2.Init.Mode = UART_MODE_TX_RX;
  10. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  11. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  12. HAL_UART_Init(&huart2);
  13. }
  14. uint8_t Read_Fingerprint(uint8_t* buffer, uint16_t size) {
  15. return HAL_UART_Receive(&huart2, buffer, size, HAL_MAX_DELAY);
  16. }
  17. int main(void) {
  18. HAL_Init();
  19. SystemClock_Config();
  20. UART2_Init();
  21. uint8_t fingerprint_buffer[32];
  22. while (1) {
  23. if (Read_Fingerprint(fingerprint_buffer, 32) == HAL_OK) {
  24. // 处理指纹数据
  25. }
  26. HAL_Delay(1000);
  27. }
  28. }

4.2 Data processing and control module

The data processing module converts the sensor data into data that can be used in the control system and performs necessary calculations and analysis.

Access Control Algorithm

Implement a simple access control algorithm to control the switch of the electromagnetic lock based on the access card and fingerprint data:

  1. #define AUTHORIZED_CARD_ID "1234567890"
  2. #define AUTHORIZED_FINGERPRINT_ID "A1B2C3D4E5"
  3. void Control_Door(uint8_t* rfid_data, uint8_t* fingerprint_data) {
  4. if (strcmp((char*)rfid_data, AUTHORIZED_CARD_ID) == 0 ||
  5. strcmp((char*)fingerprint_data, AUTHORIZED_FINGERPRINT_ID) == 0) {
  6. // 打开门锁
  7. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
  8. } else {
  9. // 关闭门锁
  10. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
  11. }
  12. }
  13. int main(void) {
  14. HAL_Init();
  15. SystemClock_Config();
  16. UART1_Init();
  17. UART2_Init();
  18. GPIO_Init();
  19. uint8_t rfid_buffer[16];
  20. uint8_t fingerprint_buffer[32];
  21. while (1) {
  22. if (Read_RFID_Card(rfid_buffer, 16) == HAL_OK &&
  23. Read_Fingerprint(fingerprint_buffer, 32) == HAL_OK) {
  24. Control_Door(rfid_buffer, fingerprint_buffer);
  25. }
  26. HAL_Delay(1000);
  27. }
  28. }

4.3 Communication and Network System Implementation

Configuring the Wi-Fi Module

Use STM32CubeMX to configure the UART interface:

  1. Open STM32CubeMX and select your STM32 development board model.
  2. In the graphical interface, find the UART pin that needs to be configured and set it to UART mode.
  3. Generate code and import it into STM32CubeIDE.

Code:

  1. #include "stm32f4xx_hal.h"
  2. #include "usart.h"
  3. #include "wifi_module.h"
  4. UART_HandleTypeDef huart3;
  5. void UART3_Init(void) {
  6. huart3.Instance = USART3;
  7. huart3.Init.BaudRate = 115200;
  8. huart3.Init.WordLength = UART_WORDLENGTH_8B;
  9. huart3.Init.StopBits = UART_STOPBITS_1;
  10. huart3.Init.Parity = UART_PARITY_NONE;
  11. huart3.Init.Mode = UART_MODE_TX_RX;
  12. huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  13. huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  14. HAL_UART_Init(&huart3);
  15. }
  16. void Send_Data_To_Server(uint8_t* rfid_data, uint8_t* fingerprint_data) {
  17. char buffer[128];
  18. sprintf(buffer, "RFID: %s, Fingerprint: %s", rfid_data, fingerprint_data);
  19. HAL_UART_Transmit(&huart3, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
  20. }
  21. int main(void) {
  22. HAL_Init();
  23. SystemClock_Config();
  24. UART3_Init();
  25. UART1_Init();
  26. UART2_Init();
  27. uint8_t rfid_buffer[16];
  28. uint8_t fingerprint_buffer[32];
  29. while (1) {
  30. if (Read_RFID_Card(rfid_buffer, 16) == HAL_OK &&
  31. Read_Fingerprint(fingerprint_buffer, 32) == HAL_OK) {
  32. Send_Data_To_Server(rfid_buffer, fingerprint_buffer);
  33. }
  34. HAL_Delay(1000);
  35. }
  36. }

4.4 User Interface and Data Visualization

Configure OLED display

Use STM32CubeMX to configure the I2C interface:

  1. Open STM32CubeMX and select your STM32 development board model.
  2. In the graphical interface, find the I2C pin that needs to be configured and set it to I2C mode.
  3. Generate code and import it into STM32CubeIDE.

Code:

First, initialize the OLED display:

  1. #include "stm32f4xx_hal.h"
  2. #include "i2c.h"
  3. #include "oled.h"
  4. void Display_Init(void) {
  5. OLED_Init();
  6. }

Then implement the data display function to display the access control status and data on the OLED screen:

  1. void Display_Data(uint8_t* rfid_data, uint8_t* fingerprint_data) {
  2. char buffer[32];
  3. sprintf(buffer, "RFID: %s", rfid_data);
  4. OLED_ShowString(0, 0, buffer);
  5. sprintf(buffer, "Fingerprint: %s", fingerprint_data);
  6. OLED_ShowString(0, 1, buffer);
  7. }
  8. int main(void) {
  9. HAL_Init();
  10. SystemClock_Config();
  11. I2C1_Init();
  12. Display_Init();
  13. UART1_Init();
  14. UART2_Init();
  15. uint8_t rfid_buffer[16];
  16. uint8_t fingerprint_buffer[32];
  17. while (1) {
  18. if (Read_RFID_Card(rfid_buffer, 16) == HAL_OK &&
  19. Read_Fingerprint(fingerprint_buffer, 32) == HAL_OK) {
  20. // 显示门禁数据
  21. Display_Data(rfid_buffer, fingerprint_buffer);
  22. }
  23. HAL_Delay(1000);
  24. }
  25. }

5. Application scenario: access control management and optimization

Office building access control management

The intelligent access control system can be used for access control management of office buildings. By real-time monitoring of access control data, automatic control can be achieved to improve the security and management efficiency of office buildings.

Community access control management

In the community, the smart access control system can realize automated management of residents and visitors, improving the safety and convenience of the community.

Home access control

The smart access control system can be used for home access control, and through automated control and data analysis, it can achieve smarter home access control management.

Intelligent Building Research

Intelligent access control system can be used for intelligent building research, and provide scientific basis for building access control management and optimization through data collection and analysis.

⬇Help everyone organize the information of single chip microcomputer

Including stm32 project collection [source code + development documentation]

Click the blue words below to receive it, thank you for your support! ⬇

Click here to get more embedded details

For discussion and information on stm32, please send me a private message!

 

6. Problem Solving and Optimization

Common Problems and Solutions

Inaccurate sensor data

Ensure that the connection between the sensor and STM32 is stable and calibrate the sensor regularly to obtain accurate data.

Solution: Check whether the connection between the sensor and STM32 is firm, and re-solder or replace the connection wire if necessary. At the same time, calibrate the sensor regularly to ensure accurate data.

Access control is unstable

Optimize control algorithms and hardware configurations to reduce access control instability and improve system response speed.

Solution: Optimize control algorithms, adjust parameters, reduce oscillation and overshoot. Use high-precision sensors to improve the accuracy and stability of data acquisition. Choose more efficient actuators to improve the response speed of access control.

Data transfer failed

Ensure the stable connection between the Wi-Fi module and STM32, optimize the communication protocol, and improve the reliability of data transmission.

Solution: Check whether the connection between the Wi-Fi module and STM32 is firm, and re-solder or replace the connection wire if necessary. Optimize the communication protocol to reduce data transmission delay and packet loss rate. Choose a more stable communication module to improve the reliability of data transmission.

The display shows abnormality

Check the I2C communication line to ensure that the communication between the display and the MCU is normal to avoid display abnormalities caused by line problems.

Solution: Check whether the I2C pins are connected correctly and ensure that the power supply is stable. Use an oscilloscope to detect the I2C bus signal to confirm whether the communication is normal. If necessary, replace the display or MCU.

Optimization suggestions

Data Integration and Analysis

Integrate more types of sensor data and use data analysis technology to predict and optimize environmental conditions.

Suggestion: Add more monitoring sensors, such as door magnetic sensors, temperature and humidity sensors, etc. Use cloud platforms for data analysis and storage to provide more comprehensive environmental monitoring and management services.

User interaction optimization

Improve user interface design to provide more intuitive data display and simpler operation interface to enhance user experience.

Recommendation: Use a high-resolution color display to provide a richer visual experience. Design a simple and easy-to-understand user interface to make it easier for users to operate. Provide graphical data display, such as real-time environmental parameter charts, historical records, etc.

Intelligent control improvement

Add an intelligent decision support system to automatically adjust the control strategy based on historical data and real-time data to achieve more efficient environmental control and management.

Recommendation: Use data analysis technology to analyze environmental data and provide personalized environmental management suggestions. Combine historical data to predict possible problems and needs and optimize control strategies in advance.

7. Closing and Conclusion

This tutorial details how to implement an intelligent access control system in an STM32 embedded system, from hardware selection, software implementation to system configuration and application scenarios.