arduino蓝牙通讯代码_arduino 联接蓝牙模块

本文介绍如何使用Arduino IDE配置蓝牙模块实现与Arduino板的串口通信。通过上传特定代码,能够实现LED灯的远程控制功能。文中详细展示了代码片段及其实现原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

硬件连接后,连上电脑ARDUINO IDE 并上传相关代码测试。代码片断为:

#include

SoftwareSerial BT(10, 11);

// creates a "virtual" serial port/UART

// connect BT module TX to D10

// connect BT module RX to D11

// connect BT Vcc to 5V, GND to GND

void setup()

{

// set digital pin to control as an output

pinMode(13, OUTPUT);

// set the data rate for the SoftwareSerial port

BT.begin(9600);

// Send test message to other device

BT.println("Hello from Arduino");

}

char a; // stores incoming character from other device

void loop()

{

if (BT.available())

// if text arrived in from BT serial...

{

a=(BT.read());

if (a=='1')

{

digitalWrite(13, HIGH);

BT.println("LED on");

}

if (a=='2')

{

digitalWrite(13, LOW);

BT.println("LED off");

}

if (a=='?')

{

BT.println("Send '1' to turn LED on");

BT.println("Send '2' to turn LED on");

}

// you can add more "if" statements with other characters to add more commands

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值