Communication Design Between HC-05 Bluetooth Module and Microcontroller

Communication Design Between HC-05 Bluetooth Module and Microcontroller

1. Basic Introduction

The HC-05 Bluetooth module is a very practical wireless communication module, widely used in projects such as smart cars, remote control, and data collection. It features simple configuration and stable communication. This article will detail how to use the HC-05 Bluetooth module to achieve serial communication with a microcontroller.

2. Hardware Preparation

  • • STC89C52 microcontroller minimum system board
    • • HC-05 Bluetooth module
    • • Several Dupont wires
    • • 5V power supply
    • • USB to TTL module (for debugging)

3. Hardware Connection

HC-05 pin connection instructions:

  • • VCC connected to 5V power supply
    • • GND to ground
    • • TXD connected to microcontroller RXD (P3.0)
    • • RXD connected to microcontroller TXD (P3.1)
    • • EN/KEY pin can be left unconnected (used only during configuration)

Note : The TXD and RXD connections are crossed, meaning the module’s sending end connects to the microcontroller’s receiving end.

4. Software Design

4.1 Initialization Code

void uart_init() { TMOD = 0x20; // Timer 1 mode 2 TH1 = 0xFD; // Baud rate 9600 TL1 = 0xFD; TR1 = 1; // Start Timer 1 SCON = 0x50; // Serial mode 1 EA = 1; // Enable global interrupt ES = 1; // Enable serial interrupt }

Would you like me to explain or break down this code?

4.2 Serial Send and Receive Functions

void uart_send_byte(unsigned char dat) { SBUF = dat; // Send data while(!TI); // Wait for send to complete TI = 0; // Clear send flag } void uart_receive() interrupt 4 { unsigned char recv_data; if(RI) { RI = 0; // Clear receive flag recv_data = SBUF; // Read received data uart_send_byte(recv_data); // Echo data } }

Would you like me to explain or break down this code?

5. Debugging Steps

  1. 1. Baud Rate Setting
  • • HC-05 default baud rate is 9600
  1. 2. Hardware Debugging
  • • Enter AT mode: press and hold the button on the module while powering on
  • • AT command to set baud rate: AT+UART=9600,0,0
  • • Use a multimeter to measure power supply voltage
    1. 3. Software Debugging
    • • Check if TX/RX connections are correct
    • • Observe the status of the Bluetooth module indicator light
  • • Use serial assistant to test communication
    • • Observe if data transmission and reception are normal
    • • Check if data format is correct

    6. Common Problem Solutions

    1. 1. Module Cannot Pair
    • • Check if power supply is stable
    1. 2. Unstable Communication
    • • Confirm if the module is in discoverable mode
    • • Default password is usually “1234” or “0000”
  • • Check baud rate settings
    1. 3. Data Loss
    • • Avoid sending data too quickly
    • • Add necessary delays
  • • Implement data verification mechanisms
    • • Implement acknowledgment confirmation function
    • • Set buffer size reasonably

    7. Practical Suggestions

    1. 1. Test with simple LED control to verify communication
    2. 2. Master the use of serial debugging tools
    3. 3. Understand the basic principles of Bluetooth communication
    4. 4. Pay attention to the impact of power quality on communication stability
    5. 5. Establish a complete error handling mechanism

    8. Performance Optimization

    1. 1. Reduce Power Consumption
    • • Reasonably use Bluetooth’s sleep mode
    1. 2. Improve Stability
    • • Appropriately reduce transmission power
    • • Optimize packet size
    Communication Design Between HC-05 Bluetooth Module and Microcontroller
    • • Add watchdog function
      • • Implement automatic reconnection mechanism
      • • Increase communication timeout handling

    9. Communication Protocol Design

    9.1 Basic Data Frame Format

    typedef struct { uint8_t header; // Frame header 0xAA uint8_t length; // Data length uint8_t command; // Command byte uint8_t data[32]; // Data field uint8_t checksum; // Checksum uint8_t tail; // Frame tail 0x55 } DataFrame;

    Would you like me to explain or break down this code?

    9.2 Protocol Parsing Example

    uint8_t parse_data_frame(uint8_t byte) { static DataFrame frame; static uint8_t recv_state = 0; static uint8_t data_count = 0; switch(recv_state) { case 0: // Waiting for frame header if(byte == 0xAA) { recv_state = 1; data_count = 0; } break; case 1: // Receive length frame.length = byte; recv_state = 2; break; case 2: // Receive command byte frame.command = byte; recv_state = 3; break; case 3: // Receive data frame.data[data_count++] = byte; if(data_count >= frame.length) { recv_state = 4; } break; case 4: // Receive checksum frame.checksum = byte; recv_state = 5; break; case 5: // Receive frame tail if(byte == 0x55) { // Check passed, execute command execute_command(&frame); } recv_state = 0; break; } return 0; }

    Would you like me to explain or break down this code?

    10. Practical Application Case – Bluetooth Remote Control Car

    10.1 Main Control Code Framework

    #include <reg52.h> // Motor control pin definitions sbit LEFT_FORWARD = P1^0; sbit LEFT_BACKWARD = P1^1; sbit RIGHT_FORWARD = P1^2; sbit RIGHT_BACKWARD = P1^3; // Motor control function void motor_control(uint8_t command) { switch(command) { case 0x01: // Move forward LEFT_FORWARD = 1; LEFT_BACKWARD = 0; RIGHT_FORWARD = 1; RIGHT_BACKWARD = 0; break; case 0x02: // Move backward LEFT_FORWARD = 0; LEFT_BACKWARD = 1; RIGHT_FORWARD = 0; RIGHT_BACKWARD = 1; break; case 0x03: // Turn left LEFT_FORWARD = 0; LEFT_BACKWARD = 1; RIGHT_FORWARD = 1; RIGHT_BACKWARD = 0; break; case 0x04: // Turn right LEFT_FORWARD = 1; LEFT_BACKWARD = 0; RIGHT_FORWARD = 0; RIGHT_BACKWARD = 1; break; default: // Stop LEFT_FORWARD = 0; LEFT_BACKWARD = 0; RIGHT_FORWARD = 0; RIGHT_BACKWARD = 0; break; } }

    Would you like me to explain or break down this code?

    11. Debugging Techniques

    11.1 Communication Debugging

    1. 1. Use LED to Indicate Communication Status
    • • Blink when data is received
    1. 2. Serial Data Monitoring
    • • Different colors for different states
    • • Rapid blinking for error states
  • • Add debugging output
    • • Record key data
    • • Use an oscilloscope to observe waveforms

    11.2 Troubleshooting Flow

    1. 1. Check Power Supply
    • • Measure VCC voltage
    1. 2. Signal Integrity
    • • Observe fluctuation
    • • Confirm power supply stability
  • • Measure TX/RX levels
    1. 3. Software Debugging
    • • Check communication waveform
    • • Verify data correctness
  • • Step through
    • • Variable monitoring
    • • Breakpoint debugging

    12. Safety Precautions

    1. 1. Hardware Protection
    • • Add reverse connection protection circuit
    1. 2. Software Protection
    • • Design overcurrent protection
    • • Level conversion circuit protection
  • • Communication timeout handling
    1. 3. Operating Environment
    • • Illegal data filtering
    • • Watchdog timer reset
  • • Avoid high-temperature environments
    • • Prevent electromagnetic interference
    • • Pay attention to waterproof and dustproof

    13. Extended Function Suggestions

    1. 1. Data Encryption
    2. 2. Automatic Reconnection Mechanism
    3. 3. Battery Level Monitoring
    4. 4. Operating Status Logging
    5. 5. Remote Firmware Update

    These functions can be selectively implemented based on actual project needs. The focus is to ensure basic functionality is stable and reliable, then gradually add extended features.

    Leave a Comment