I am using two Arduino Megas to try and interface with each other for a project. I have used many tutorials, and nothing has seemed to work. I have configured the modules in AT Command mode, paired them, and I have no idea why this isn't working. Can someone please help me out? Thank you in advance!
This is the code I have so far:
Master:
#include <Arduino.h>
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2, 3); // RX | TX
void setup() {
Serial.begin(9600);
BTSerial.begin(9600);
}
void loop() {
BTSerial.println("hello");
delay(1000);
}
Slave:
#include <Arduino.h>
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2, 3); // RX | TX
void setup() {
Serial.begin(9600);
BTSerial.begin(9600);
}
void loop() {
Serial.println(BTSerial.readString());
}
You haven't provided enough information for forum members to guess the problem.
This seems by far the best collection of HC-05 tutorials, so go through them carefully:: https://www.martyncurrey.com/arduino-with-hc-05-bluetooth-module-at-mode/
I can't help you but I'm curious why you're using SoftwareSerial on a board that has 3 spare UARTs?
Do I need to share my wiring too?
It means that in general one does not need to use SoftwareSerial for Bluetooth but can use Serial1/2/3 and the respective pins on the Mega.
#include <Arduino.h>
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2, 3); // RX | TX
void setup() {
Serial.begin(9600);
BTSerial.begin(9600);
}
void loop() {
BTSerial.println("hello");
delay(1000);
}
If i'm correct this line.
BTSerial.println("hello");
should be.
BTSerial.write("hello");
No, write will behave the same as print ; println will add a <CR><LF>.
You can test those differences by simply using Serial instead of BTSerial and check what shows in serial monitor.
There is a good chance that it is wrong, especially if you forgot to use logic level converters, or a resistive divider as the Martyn Currey tutorial recommends.
From
https://docs.arduino.cc/learn/built-in-libraries/software-serial/
Not all pins on the Mega and Mega 2560 boards support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).
That explains why software serial instance was not working on the Mega.
As others have said, using software serial on a device with three additional hardware serial ports is the wrong way to go.
I tried using UARTs and same issue.
I don't think so because even after I changed the code to the UARTs I tried to make the arduinos communicate over wires.
This is how I connected it.
TX -> RX
RX -> TX
decentro:
I tried using UARTs
Please post your code.
Can you read the line "hello" sent by the HC05 when you use a phone app like Kai Morich's Serial Bluetooth Terminal.
I tried to make the arduinos communicate over wires.
If the two Mega's can't communicate over wires between two hardware serial ports then there is a different issue.
Here is my new code:
#include <Arduino.h>
#include <SoftwareSerial.h>
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
Serial1.println("Hello World");
delay(1000);
}
Slave:
#include <Arduino.h>
#include <SoftwareSerial.h>
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
Serial.println(Serial1.readString());
}
That outputted Hello World but still no two-way communication on the arduinos yet. How can I change the code to make it work?
It's not clear how you configured the two devices, but a good tutorial on how to configure and connect two HC05's is here
https://www.martyncurrey.com/connecting-2-arduinos-by-bluetooth-using-a-hc-05-and-a-hc-06-easy-method-using-cmode/
If you are now using the Hardware Serial1 on pins 19 (RX) and 18 (TX), you do not need to include the software serial library.
If you remove the HC05 modules, can you get the Megas to communicate with each other over wires and Serial1?
Using the voltage divider on the module RX so that it doesn't see 5v is a good idea. It's not clear to me why you haven't done this.
I have done it because I was following that tutorial a while back, but the programming stumped me. Should the baud rate be 38400 instead of 9600?
Whenever I take out the wire of the TX and RX pins on the slave it prints a bunch of hello worlds. It never does this when it is plugged in. Is there any reason for this?
The default baud rate for communication mode is 9600. The default baud rate for AT mode is 38400.
Whenever I take out the wire of the TX and RX pins on the slave it prints a bunch of hello worlds.
I thought that the modules were wired to TX1 and RX1 of the Megas. I don't understand what you have connected to TX and RX.
Do I need to share my wiring too?
Yes, please.