Arduino Bluetooth module communicates with mobile phone---realizes Bluetooth control of LED

When using Arduino for Bluetooth communication, we can easily realize data exchange with other Bluetooth devices. Bluetooth communication is useful in various IoT and remote control applications as it provides wireless connectivity and easy data transfer. In this blog, we will introduce how to set up Bluetooth communication on Arduino and perform basic data transmission.

1 Introduction

Bluetooth communication is a wireless communication technology used to transmit data over short distances. In Arduino projects, we can use Bluetooth modules to communicate with other devices such as smartphones, computers, or other Bluetooth-enabled devices. In this way, we can transmit data between Arduino and other devices through Bluetooth communication to achieve remote control, sensor data monitoring and other functions.

2. Prepare materials

 Arduino 开发板(如 Arduino Uno)
蓝牙模块(hc05,或者hc08)
杜邦线和面包板
电脑或智能手机

Bluetooth module

3. Wiring

**蓝牙模块的 VCC 引脚连接到 Arduino 的 5V 引脚。
蓝牙模块的 GND 引脚连接到 Arduino 的 GND 引脚。
蓝牙模块的 TX 引脚连接到 Arduino 的 10 引脚。
蓝牙模块的 RX 引脚连接到 Arduino 的 11 引脚。**

Insert image description here
Insert image description here

4. Code

#include <SoftwareSerial.h>
SoftwareSerial blue(10, 11); // RX, TX
void setup() {
    
    
  Serial.begin(9600);//初始化 Arduino 的硬串口
  blue.begin(9600); // 初始化软串口与蓝牙模块的通信
  pinMode(13,OUTPUT);
}

void loop() {
    
    
  if (blue.available() > 0) {
    
    
    char receivedChar = blue.read();
    if (receivedChar == '1'){
    
    
      digitalWrite(13,HIGH);
      Serial.println("led on");
    }
    else if(receivedChar == '2'){
    
    
      digitalWrite(13,LOW);
      Serial.println("led off");
    }
    Serial.print("Received Data: ");
    Serial.println(receivedChar);
  }
}

This is a basic Arduino bluetooth communication example program. It uses the SoftwareSerial library (SoftwareSerial) to communicate with the Bluetooth module and control the LEDs on the Arduino board.

In the setup() function, we initialize the communication baud rate of Arduino's hard serial port (Serial) and soft serial port (blue) to 9600. We also set pin 13 as an output to control an LED connected to that pin.

In the loop() function, we check if the Bluetooth module has available data. If data is available, we read the received characters and control the state of the LED based on the received characters. If character '1' is received, pin 13 is set high (lights the LED) and "led on" is printed in the serial monitor. If character '2' is received, pin 13 is set low (LED off) and "led off" is printed in the serial monitor. No matter what characters are received, we print "Received Data: " and the received characters in the serial monitor.

This way, when you send the character '1' to the Arduino via Bluetooth, the LED will light up and show "led on" in the serial monitor. When you send the character '2' to the Arduino, the LED will turn off and "led off" will appear in the serial monitor. Received characters will also be printed to the serial monitor.

Please make sure you connect the Bluetooth module and LED correctly and connect the Bluetooth module with the Arduino's soft serial pins (10 and 11). Also, make sure the baud rate setting on the serial monitor matches the baud rate (9600) in the code.

This is a simple example that you can extend and modify to suit your needs. Hope this helps you with basic Arduino bluetooth communication functionality.
Insert image description here

5. Test

1. Connect the Arduino development board to the computer, and upload the code to the Arduino.
2. Open the serial monitor and set the baud rate to 9600.
3. Open the Bluetooth settings of the Bluetooth device (such as a smartphone) and search for nearby Bluetooth devices.
4. Find and connect the Bluetooth module to the Arduino.
5. Enter some characters in the serial monitor and observe whether the Bluetooth device receives the same characters and displays them.
6. Send some characters on the Bluetooth device and observe whether the serial monitor receives the same characters and displays them

Insert image description here

Write the number "1" here to turn on the LED light, enter the number "2" to turn off the LED light

Guess you like

Origin blog.csdn.net/m0_63715549/article/details/131754682