I am trying to communicate with the HC-05 Bluetooth module for quite long time now, but with no success.
I am using the Nodemcu(V3) ESP8266 module.
I connect the HC-05 to Nodemcu in following sequence:
HC-05 Nodemcu
----- -----------
RX --> Pin 1 (Tx)
TX --> Pin 3 (RX)
Vcc --> +3.3V
GND --> GND
For starters, i want to check if my Nodemcu is communicating properly with my HC-05 module.
I have written the following code to read the response of AT commands:
#include <SoftwareSerial.h>
SoftwareSerial BTserial(3, 1); // RX | TX
char Bluetooth_Name = ' ';
void setup()
{
// Arduino IDE serial monitor
Serial.begin(115200);
// HC-05 default serial speed for AT mode is 38400
BTserial.begin(38400);
// Wait for hardware to initialize
delay(1000);
// Print debug string
}
void loop()
{
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (BTserial.available())
{
reading = BTserial.read();
Serial.println(reading);
}
// Keep reading from Arduino Serial Monitor and send to HC-05
if (Serial.available())
{
reading = Serial.read();
BTserial.write(reading);
}
}
However, i do not get response for any AT command at all. The serial monitor just shows blank.
Thanks in advance
EDIT:- I connected the "EN" pin on the HC-05 to Vcc. Noe, the led on the HC-05 blinks slowly, which means that the HC-05 is configured in command mode. However, i am still not able to receive the response for any AT commands. I have also selected "Both NL & CR" in the serial monitor, configured the baud rate correctly and double-checked the hardware connections.
Everything seems to be correct except that i don't get response for the AT commands.
Please help!!!
I changed the line
SoftwareSerial BTserial(3, 1); // RX, TX
to
SoftwareSerial BTserial(D4, D3); // RX, TX
And got it to work!
Related
I am able to connect a Raspberry Pi 4 an and Arduino Nano BLE through bluepy for Rpi4 and ArduinoBLE.h for Arduino Nano BLE. Unfortunately when I try to write from Rpi4 to Arduino Nano BLE, I'm not seeing the expected output for Read and Write. I don't see any perfect example for Arduino Nano BLE since it is recently released hardware with built-in BLE. It would be very helpful if anyone could help me achieve communication between them. Thanks in advance. Below is my code for Raspberry Pi.
import bluepy.btle as btle
p = btle.Peripheral("de:fc:54:87:b0:04")
services=p.getServices()
s = p.getServiceByUUID(list(services)[2].uuid)
c = s.getCharacteristics()[0]
c.write(bytes("2", "utf-8"))
p.disconnect()
And I'm using the Arduino built-in example from the Arduino Nano BLE Library.
#include <ArduinoBLE.h>
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
const int ledPin = LED_BUILTIN; // pin to use for the LED
void setup() {
Serial.begin(9600);
while (!Serial);
// set LED pin to output mode
pinMode(ledPin, OUTPUT);
// begin initialization
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
// set advertised local name and service UUID:
BLE.setLocalName("LED");
BLE.setAdvertisedService(ledService);
// add the characteristic to the service
ledService.addCharacteristic(switchCharacteristic);
// add service
BLE.addService(ledService);
// set the initial value for the characeristic:
switchCharacteristic.writeValue(0);
// start advertising
BLE.advertise();
Serial.println("BLE LED Peripheral");
}
void loop() {
// listen for BLE peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
//prints the centrals MAC address:
Serial.println(central.address());
// while the central is still connected to peripheral:
while (central.connected()) {
// if the remote device wrote to the characteristic,
// use the value to control the LED:
if (switchCharacteristic.written()) {
if (switchCharacteristic.value()) { // any value other than 0
Serial.println("LED on");
digitalWrite(ledPin, HIGH); // will turn the LED on
} else { // a 0 value
Serial.println(F("LED off"));
digitalWrite(ledPin, LOW); // will turn the LED off
}
}
}
// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}
I figured out myself, it was the value in the write which was going wrong all this time. Below is the right one. I hope now you can find this as a perfect solution to connect raspberry Pi 4 and Arduino Nano BLE wirelessly Via Bluetooth.
c.write(bytes("0001".encode())
I can't run AT commands in Serial Monitor. (Both NL & CR + 9600 baud are set)
I followed this tutorial here.
Hardware:
Arduino MEGA 2560
Bluetooth Module HC-05 (ZS-040)
Ubuntu 16.04
This is the way I wired them:
I want to mention that before this I wired them multiple times without the resistors but not for more than 3-4 minutes.
This is the code I use:
// Basic Bluetooth test sketch 5a for the Arduino Mega.
// AT mode using button switch
// HC-05 with EN pin and button switch
//
// Uses serial with the host computer and serial1 for communication with the Bluetooth module
//
// Pins
// BT VCC to Arduino 5V out. Disconnect before running the sketch
// BT GND to Arduino GND
// BT RX (through a voltage divider) to Arduino TX1 (pin 18)
// BT TX to Arduino RX1 (no need voltage divider) (pin 19)
//
// When a command is entered in to the serial monitor on the computer
// the Arduino will relay it to the Bluetooth module and display the result.
//
char serialByte = '0';
const byte LEDPIN = 13;
void setup()
{
pinMode(LEDPIN, OUTPUT);
// communication with the host computer
Serial.begin(9600);
Serial.println("Do not power the BT module");
Serial.println(" ");
Serial.println("On the BT module, press the button switch (keep pressed, and at the same time power the BT module");
Serial.println("The LED on the BT module should now flash on/off every 2 seconds");
Serial.println("Can now release the button switch on the BT module");
Serial.println(" ");
Serial.println("After entering AT mode, type 1 and hit send");
Serial.println(" ");
// wait for the user to type "1" in the serial monitor
while (serialByte !='1')
{
if ( Serial1.available() ) { serialByte = Serial1.read(); }
}
// communication with the BT module on serial1
Serial1.begin(38400);
// LED to show we have started the serial channels
digitalWrite(LEDPIN, HIGH);
Serial.println(" ");
Serial.println("AT mode.");
Serial.println("Remember to to set Both NL & CR in the serial monitor.");
Serial.println("The HC-05 accepts commands in both upper case and lower case");
Serial.println(" ");
}
void loop()
{
// listen for communication from the BT module and then write it to the serial monitor
if ( Serial1.available() ) { Serial.write( Serial1.read() ); }
// listen for user input and send it to the HC-05
if ( Serial.available() ) { Serial1.write( Serial.read() ); }
}
The flash on the LED should change to on/off every 2 seconds, 1 second
on, 1 second off. This indicates AT mode. You can now release the
button switch.
Even though the LED is blinking like described (quote from above)
I get no response from typing 1 in Serial Monitor.
This is how my Serial Monitor looks after running everything as supposed:
Solution
Connection:
Bluetooth RXD to Arduino Mega pin 11
Bluetooth TXD to Arduino Mega pin 10
Bluetooth VCC to Arduino Mega 5V
Bluetooth GND to Arduino Mega GND
I used this code:
#include <SoftwareSerial.h>
#define RxD 10
#define TxD 11
SoftwareSerial BTSerial(RxD, TxD);
void setup(){
// replace BAUDRATE as suggested
BTSerial.begin(BAUDRATE);
Serial.begin(9600);
BTSerial.print("AT\r\n");
}
void loop(){
if (BTSerial.available())
Serial.write(BTSerial.read());
if (Serial.available())
BTSerial.write(Serial.read());
}
after setting AT mode as described in the tutorial above.
I tried running the code with BAUDRATE starting from 9600 to 460800:
For me 9600 was the right baud rate of my HC-05.
I have been trying to establish connection between two HC-06 Bluetooth modules. Pairing has been done. The two modules are communicating. My aim is to send a letter from one module and receive acknowledgment from the other module. The code for the master module is below.
#include <SoftwareSerial.h>
SoftwareSerial BTserial(2,3); // RX, TX
char c;
char s[]="Matched";
int t[]="NotMatched";
void setup()
{
// start the serial communication with the computer
Serial.begin(9600);
Serial.println("Arduino with HC-06 is ready");
// start communication with the HC-06 using 38400
BTserial.begin(38400);
Serial.println("Bluetooth serial started at 38400");
}
void loop()
{
// Read from HC-06 and send to Arduino Serial Monitor
if (BTserial.available())
{
c=(BTserial.read());
if (c=='a')
{
Serial.write(s);
}
else
{
Serial.write(t);
}
}
// Read from Arduino Serial Monitor and send to HC-06
if (Serial.available())
{
c = Serial.read();
Serial.write(c);
BTserial.write(c);
}
}
Similar code is used for the slave module. Except for the 'else' part in the code everything runs right. I receive acknowledgement along with the else portion being printed twice for both the if and else portion of the code i.e 'matched not matched not matched' is printed when it receives char 'a' and 'not matched not matched not matched' is printed when it receives anything other than 'a' . Can you please give me suggestions on what could be wrong.
Have you printed the characters that you are reading?
I had a similar problem and I found that the Bluetooth was receiving garbage characters. The reason was because the standard baudrate, of HC-06 modules, is 9600, so if you change:
BTserial.begin(38400);
to
BTserial.begin(9600);
In both master and slave, it might work.
I am doing a project in Arduino and want sensor data on my Android. For this purpose I am using HC-05 BT module and Bluetooth Terminal app on Android. But Bluetooth Terminal is receiving data at constant rate (1 sec, I think). And I want to receive data in 10 millisecs. How can I do this?
Here is my code:
#include <SoftwareSerial.h>
int RX=0;
int TX=1;
SoftwareSerial Bluetooth(RX,TX);
int i=0;
void setup() {
Bluetooth.begin(9600);
Bluetooth.println("The controller has successfuly connected to the phone");
}
void loop() {
Bluetooth.write(i);
i++;
delay(5);
}
From here:
The bluetooth module baud rates are set by an hexadecimal index from
‘1’ to ‘C’. Indexes are: 1:1200, 2:2400, 3:4800, 4:9600, 5:19200,
6:38400, 7:57600, 8:115200, 9:230400, A:460800, B:921600, C:1382400
To set a default baud rate, enter this command
AT+BAUD
For example for multiwii, we need 115200 as the BT baud rate, we would
enter AT+BAUD8.
If successful you should be returned “OK” in the serial monitor.
You can change the baud rate of the HC-05 by using AT commands.
Use:
AT+UART=57600,1,0
here is a tutorial
If it is the full code then you sending not printed characters. And also after you will get i=127 you will send characters from upper part of ascii table. And think what will happens when i>255. Try this code in loop:
Bluetooth.println("this is test code")
instead of writing Bluetooth.write(i);
You can get more info about ASCII and more about Serial.print and Serial.write
You can change the baud rate of the HC-06 by using AT commands
#include <SoftwareSerial.h>
SoftwareSerial btSerial(2, 3); // RX, TX
/*
* Connect pin 2 Arduino to pin TX HC-06
* Connect pin 3 Arduino to pin RX HC-06
*/
void setup() {
Serial.begin(9600);
Serial.println("Enter AT commands:");
btSerial.begin(9600);
}
void loop()
{
if (btSerial.available())
Serial.write(btSerial.read());
if (Serial.available())
btSerial.write(Serial.read());
}
AT
AT+BAUD4==>9600
AT+BAUD8==>115200
Here's my problem. I have an Arduino Mega 2560 connected with USB on my pc (windows 7).On the arduino i have connected a bluetooth device HC-06. I upload the following the program to my arduino:
#include <SoftwareSerial.h>// import the serial library
SoftwareSerial Genotronex(14, 15); // RX, TX
int BluetoothData; // the data given from Computer
void setup() {
// put your setup code here, to run once:
Genotronex.begin(9600);
}
void loop() {
BluetoothData=Genotronex.read(); //read incoming data
Genotronex.println(BluetoothData); //print data received from bluetooth
delay(100);// prepare for next data ...
}
I successfully connect my arduino with the bluetooth. Next i use putty and connect to the bluetooth but the problem is that it prints "-1" meaning that the incoming data to the bluetooth is "-1" although i do not send any data from any other progamm. I also tried to send other data from putty but it didn't work. Thanks and sorry for my english.
Try something like that to be sure that you received some data before send them to computer
void loop()
{
if (Genotronex.available())
{
BluetoothData=Genotronex.read();
Genotronex.write(BluetoothData);
}
delay(100);
}
And Have you check the HC-06 configuration ?
Here
and Here