蓝牙主从配对
首先接线
主:RX接D2,TX接D3,VCC接VCC,GND接GND
从也是一样
程序
#include <SoftwareSerial.h>
#define tx 2
#define rx 3
SoftwareSerial configBt(rx, tx); // RX, TX
void setup()
{
Serial.begin(38400);
configBt.begin(38400);
pinMode(tx, OUTPUT);
pinMode(rx, INPUT);
}
void loop()
{
if(configBt.available()) // if the HC05 is sending something…
{
Serial.print(configBt.readString()); // print in serial monitor
}
if(Serial.available()) // if serial monitor is outputting something…
{
configBt.write(Serial.read()); // write to Arduino’s Tx pin
}
}
分别给主uno和从uno上传程序,然后拔掉,重新插上
记得要打开二个程序,二个串口监视器
然后主uno打开串口监视器,设置如下
然后按步骤来
主程序的发完了就发从程序的
最后连接的结果就是二个模块同步闪烁
然后给主uno上传下面的程序测试
#include <SoftwareSerial.h>
#define tx 2
#define rx 3
SoftwareSerial bt(rx,tx); //RX, TX
void setup()
{
Serial.begin(9600);
bt.begin(9600);
pinMode(tx, OUTPUT);
pinMode(rx, INPUT);
}
void loop()
{
bt.write(123);
}
从uno上传下面的程序
#include <SoftwareSerial.h>
#define tx 2
#define rx 3
SoftwareSerial bt(rx, tx); //RX, TX
void setup()
{
Serial.begin(9600);
bt.begin(9600);
pinMode(tx, OUTPUT);
pinMode(rx, INPUT);
}
void loop()
{
if(bt.available()>0)
{
Serial.println(bt.read());
}
}
将串口设置成如下模式
可以看到从uno的串口不断的接收到123,也可以换过来测试,从而可以双向通信