使用Arduino和蓝牙模块通过安卓应用程序控制LED的物联网项目

2025 年 5 月 8 日 | 阅读 3 分钟

让我们构建一个物联网项目,使用Arduino (Arduino UNO) 和蓝牙模块 HC-05 来控制 LED 灯。在这个项目中,我们将使用 Android 智能手机向蓝牙模块发送蓝牙信号。

硬件要求

  1. Arduino UNO 开发板
  2. 用于连接 Arduino UNO 的 USB 电缆
  3. 蓝牙模块 HC-05
  4. 跳线,公对母
  5. LED
  6. 交流 220v/120v 家用电器或 9v 高瓦特电池

软件要求

  1. Arduino 软件
  2. Android Studio

Arduino-蓝牙模块的工作原理

在这个项目中,使用了三个主要组件:Android 智能手机、蓝牙收发器和 Arduino。

IoT project using Arduino and Bluetooth Module to control LED through Android App

Android 应用程序通过按下 ON 按钮向蓝牙模块 HC-05 发送串行数据。由于蓝牙模块 HC-05 使用串行通信,它从应用程序接收数据,并通过蓝牙模块的 TX 引脚将其发送到 Arduino 的 RX 引脚。Arduino 内部上传的代码会检查接收到的数据。如果接收到的数据是 1,LED 亮起,如果接收到的数据是 0,LED 熄灭。

数字电路图

蓝牙模块 HC-05         Arduino UNO

TX  --------------------------------> RX (引脚 0)
RX  --------------------------------> TX (引脚 1)
VCC  --------------------------------> 5v
GND  --------------------------------> GND

 

LED 引脚                                     Arduino UNO
引脚 1  --------------------------------> GND
引脚 2  --------------------------------> 引脚 13

IoT project using Arduino and Bluetooth Module to control LED through Android App

为了对Arduino板进行编程,我们需要下载Arduino软件。这可以从Arduino官方网站 https://www.arduino.cc/ 下载

IoT project using Arduino and Bluetooth Module to control LED through Android App

根据您的操作系统兼容性(Windows 安装程序)下载 Arduino 软件。

IoT project using Arduino and Bluetooth Module to control LED through Android App

点击下载

IoT project using Arduino and Bluetooth Module to control LED through Android App

下载成功后,运行安装程序并按照说明操作。

IoT project using Arduino and Bluetooth Module to control LED through Android App
IoT project using Arduino and Bluetooth Module to control LED through Android App
IoT project using Arduino and Bluetooth Module to control LED through Android App
IoT project using Arduino and Bluetooth Module to control LED through Android App

当它要求安装驱动程序软件时,点击安装

IoT project using Arduino and Bluetooth Module to control LED through Android App
IoT project using Arduino and Bluetooth Module to control LED through Android App

安装完成后,打开软件,如果它生成安全警报,请允许。

IoT project using Arduino and Bluetooth Module to control LED through Android App

为 Arduino UNO 板编写程序,如果接收到的数据等于 1,LED 亮起,如果数据等于 0,LED 熄灭。

保存您的程序并编译它。

IoT project using Arduino and Bluetooth Module to control LED through Android App

通过 Arduino UNO USB 电缆将您的 Arduino 设备连接到您的笔记本电脑(或显示器)。在将程序上传到 Arduino UNO 时,移除与 Arduino UNO 设备的所有其他连接,例如蓝牙模块和 LED。

编译代码后,将其上传到 Arduino UNO 设备。在将代码上传到 Arduino UNO 设备之前,请确保选择了您的 Arduino 串口,否则它会生成错误消息“未选择串口”。

要选择您的串口,请打开设备管理器 > 端口 >Arduino Uno,然后上传您的代码。

IoT project using Arduino and Bluetooth Module to control LED through Android App
IoT project using Arduino and Bluetooth Module to control LED through Android App

下载 Android 应用程序的 .apk 文件并将其安装在您的 Android 智能手机上。点击此处下载

与蓝牙模块连接 Android 应用程序的步骤

1. 打开蓝牙连接器应用程序,并允许打开设备的蓝牙。

IoT project using Arduino and Bluetooth Module to control LED through Android App

2. 搜索蓝牙设备以进行配对。

IoT project using Arduino and Bluetooth Module to control LED through Android App

3. 要与蓝牙 HC-05 模块配对,请输入 PIN 码 0000 或 1234。

IoT project using Arduino and Bluetooth Module to control LED through Android App

4. 选择配对设备 HC-05 以与 Android 应用程序连接。

IoT project using Arduino and Bluetooth Module to control LED through Android App

5. 控制 LED 设备。

IoT project using Arduino and Bluetooth Module to control LED through Android App

单击“ON”按钮时,它将数据 1 发送到蓝牙模块,此数据从蓝牙模块传输到 Arduino 设备,并打开 LED。单击“OFF”时,Android 应用程序将数据 0 发送到蓝牙模块,此数据从蓝牙模块传输到 Arduino,并关闭 LED。

IoT project using Arduino and Bluetooth Module to control LED through Android App
IoT project using Arduino and Bluetooth Module to control LED through Android App