gpt4 book ai didi

c - HC-05 ⸮ 串行不起作用

转载 作者:行者123 更新时间:2023-11-30 14:51:42 26 4
gpt4 key购买 nike

我最近为我的 arduino 购买了一个 HC-05 蓝牙模块,但我无法从中发送或接收数据。我使用代码来打开或关闭 LED,但是当我从 PC 的串行监视器发送一个字符后,我得到 ⸮。并且模块不响应任何AT命令。 HC-05 Connection Arduino connection我以 9600 和 38400 波特率运行串行,但没有任何变化。我也尝试过无行结尾以及 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(引脚 0)———-> TX

TX(引脚 1)———-> RX

5V ———-> VCC

接地——->接地

将 LED 负极连接到 arduino 的 GND,正极连接到引脚 13,电阻值在 220Ω – 1KΩ 之间。电路就完成了。注意:不要将蓝牙的RX对RX和TX对TX连接到arduino,您将不会收到任何数据,这里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上拔下蓝牙模块的TX和RX线,上传完成后,将它们连接起来。

关于c - HC-05 ⸮ 串行不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48113637/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com