Detailed Communication Methods Between Bluetooth Modules and Microcontrollers

Bluetooth communication is quite interesting! In simple terms, it allows microcontrollers to chat with mobile phones and communicate with tablets. Beginners often find this quite challenging, but really… it’s just a serial port! Today, we will start from the basics and break down these seemingly advanced technologies.

Basic Concepts – Don’t Be Intimidated by Terminology

A Bluetooth module? Simply put, it’s a little helper that can communicate wirelessly. The most common HC-05 and HC-06 modules are priced around ten bucks, making them quite affordable. They act like translators, converting Bluetooth signals into serial signals that the microcontroller can understand.

It’s just a matter of a few wires — VCC connects to power, GND connects to ground, TXD and RXD are responsible for data transmission. It’s that simple!

Hardware Connection – Pay Attention, Don’t Connect Incorrectly!

A fatal mistake I once made… I reversed the TXD and RXD connections and spent half a day troubleshooting! Remember:

  • Module TXD → Microcontroller RXD
  • Module RXD → Microcontroller TXD

Circuit connection diagram:

Microcontroller                  Bluetooth ModuleVCC ------------------- VCCGND ------------------- GNDTX  ------------------- RXRX  ------------------- TX

Note: Some modules are powered by 3.3V, do not connect directly to 5V, or it will blow up!

Code Implementation – Let’s Get to the Point

// Serial port initialization, baud rate 9600void UART_Init(void){    SCON = 0x50;    // 8-bit data, variable baud rate    TMOD |= 0x20;   // Set timer 1 to 8-bit auto-reload mode    TH1 = 0xFD;     // Set reload value for 9600 baud rate    TL1 = 0xFD;    TR1 = 1;        // Start timer 1    EA = 1;         // Enable global interrupt    ES = 1;         // Enable serial interrupt}// Send string via serial portvoid UART_SendString(unsigned char *str){    while(*str != '\0')    {        SBUF = *str++;        while(!TI);     // Wait for transmission to complete        TI = 0;         // Clear transmission complete flag    }}// Serial port interrupt service functionvoid UART_Routine() interrupt 4{    unsigned char temp;    if(RI)             // Receive interrupt    {        RI = 0;        // Clear receive interrupt flag        temp = SBUF;   // Read received data        // Process received data here        UART_SendString("Received data:");        SBUF = temp;   // Echo received data    }}

Want to control the code? Like this:

void main(){    UART_Init();    // Initialize serial port    while(1)    {        // Send data every second        UART_SendString("I am still alive!\r\n");        delay_ms(1000);    }}

Practical Applications – An Interesting Example

I once made a mobile-controlled car, and it was so much fun! The phone sends “F” to move forward, “B” to move backward, “L” to turn left, and “R” to turn right. The code is simple, and the effect is fantastic!

However, there was a pitfall that almost caused a crash… Bluetooth signals are particularly prone to disconnection after passing through walls, leading to the car losing control. Solution: Add a watchdog timer to automatically stop the car if the signal is interrupted.

Pitfalls and Solutions

  1. Power Supply Issues : The module requires a significant current, and insufficient power can cause various strange issues. It is recommended to use a separate power supply!

  2. Interference Issues : Long wires are prone to interference, so it is recommended:

* Keep wires short* Add decoupling capacitors* Pay attention to PCB layout
  1. Pairing Issues : The default password for HC-05 is 1234, don’t change it and forget… that would be awkward.

Practical Exercise Guide

  1. First, set up a simple communication test circuit
  2. Try sending and receiving strings
  3. Make a remote control for an LED
  4. Upgrade to more complex control projects

Remember: Debugging tool – Serial Assistant! If there are issues, use it to test first.

Fun application directions:

  • Mobile-controlled smart home
  • Wireless data acquisition system
  • Bluetooth printer
  • Remote-controlled car…

Each of these projects can be played with in various ways! That’s how electronics work; the more you do, the more addictive it becomes…

Previous Review

Leave a Comment