我最近为arduino安装了HC-05蓝牙模块,但无法发送或接收数据。我使用代码打开或关闭LED,但是从PC的串行监视器发送了字符后,我得到了⸮。此外,模块不响应任何AT命令。 HC-05 Connection Arduino connection我在9600和38400波特中都运行了Serial,但是没有任何变化。我也尝试了没有行尾和NL和CR。但是这个模块错了吗?这是我的代码:
/*
Arduino Turn LED On/Off using Serial Commands
Created April 22, 2015
Hammad Tariq, Incubator (Pakistan)
It's a simple sketch which waits for a character on serial
and in case of a desirable character, it turns an LED on/off.
Possible string values:
a (to turn the LED on)
b (tor turn the LED off)
*/
char junk;
String inputString="";
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set the baud rate to 9600, same should be of your Serial Monitor
pinMode(13, OUTPUT);
}
void loop()
{
if(Serial.available()){
while(Serial.available())
{
char inChar = (char)Serial.read(); //read the input
inputString += inChar; //make a string of the characters coming on serial
}
Serial.println(inputString);
while (Serial.available() > 0)
{ junk = Serial.read() ; } // clear the serial buffer
if(inputString == "a"){ //in case of 'a' turn the LED on
digitalWrite(13, HIGH);
}else if(inputString == "b"){ //incase of 'b' turn the LED off
digitalWrite(13, LOW);
}
inputString = "";
}
}
最佳答案
我会一步一步走
连接
Arduino引脚蓝牙引脚
RX(Pin 0)———-> TX
TX(Pin 1)———-> RX
5V ———-> VCC
GND ———-> GND
将LED负极连接到arduino的GND,正极连接到引脚13,其电阻值在220Ω–1KΩ之间。和您完成电路。
注意:请勿将RX连接到RX,将TX连接到蓝牙到arduino的TX,您将不会接收到任何数据,此处TX表示发送,RX表示接收。
/*
* This program lets you to control a LED on pin 13 of arduino using a bluetooth module
*/
char data = 0; //Variable for storing received data
void setup()
{
Serial.begin(9600); //Sets the baud for serial data transmission
pinMode(13, OUTPUT); //Sets digital pin 13 as output pin
}
void loop()
{
if(Serial.available() > 0) // Send data only when you receive data:
{
data = Serial.read(); //Read the incoming data & store into data
Serial.print(data); //Print Value inside data in Serial monitor
Serial.print("\n");
if(data == '1') // Checks whether value of data is equal to 1
digitalWrite(13, HIGH); //If value is 1 then LED turns ON
else if(data == '0') // Checks whether value of data is equal to 0
digitalWrite(13, LOW); //If value is 0 then LED turns OFF
}
}
链接到连接:https://halckemy.s3.amazonaws.com/uploads/image_file/file/153200/hc-05-LED%20blink%20Circuit.png
注意:上传代码时,请从Arduino移除Bluetooth模块的TX和RX线,上传完成后,连接它们。
关于c - HC-05⸮串行不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48113637/