I'm trying to establish a serial communication with a HM-10 BLE module from DSD Tech with an Arduino Mega 2560 board.
-
What works:
I have connected the HM-10 (power, GND, Tx to Pin 1 (Tx0) and Rx to Pin 0 (Rx0).
Using the Arduino IDE Serial Monitor, I can now talk to the HM-10 via AT-Commands while the module is disconnected. All works fine. Baudrate is 9600 as set by factory.
After connecting to my iPhone, I can also send and receive messages (using e.g. BLExAR app, other apps work as well). The sketch used is the "bare minimum" sketch.
So far so good.
-
What does not work:
As soon as I connect to other pins and use another sketch I only can send data to the module / iPhone but I cannot receive data.
Sample non-working setup:
- Connect Tx to Tx3 (Pin 14) and Rx to Rx3 (Pin 15).
- Use this sketch:
#include <SoftwareSerial.h>
// according to Arduino Docs, the first param is Rx, the second is Tx:
SoftwareSerial bluetooth(15,14);
void setup()
{
bluetooth.begin(9600);
Serial.begin(9600);
delay(100);
}
void loop()
{
if (Serial.available()>0)
bluetooth.write(Serial.read());
if (bluetooth.available()>0)
Serial.write(bluetooth.read());
}
In this configuration, I cannot send and I do not receive anything.
If I exchange Tx and Rx, I can at least send data to the module & iPhone.
Sending works also if I only connect Tx3 (Pin 14) with Rx on the module.
I have also tried a lot of other Pin combinations, e.g. Pin 7/8, 8/9 etc.
Always the same effect. I can send if I connect the "Tx" pin of the board with the "Rx" pin of the module (except for Tx0 and Rx0 with "bare minimum" sketch).
It's really annoying because it works without sketch on Pin 0 and 1 but refuses to work on any other pin combination.
I have also tried a module from a different HM-10 manufacturer but same result.
Any suggestions welcome!
The connections should be reversed, e.g. Tx to Rx3 etc. And, of course you would use Serial3.begin() and Serial3.read(), etc. Don't forget 5V to 3.3V level shifters.
This website has everything you need to know about the HM-10: HM-10 Bluetooth 4 BLE Modules – Martyn Currey
I added a voltage divider according to the post you mentioned but it did not change anything.
Exactly the same behavior as before.
Works if directly connected to Pin0/1 with minimum sketch; doesn't work otherwise.
It works from Pin0/1 for the simple reason that, from the SERIAL MONITOR point of view, you are connecting TX (serial monitor) to RX (HM-10) and RX (serial monitor) to TX (HM-10).
You should avoid using pins 0 and 1 for external devices, because they are not intended to be shared.
The Martin Currey site explains how to connect the HM-10 properly, but if you have ever connected the HM-10 without using a voltage divider or level shifter, you may have damaged it.
OK but still, why does it work properly on Pin0/1 (in both directions), but not on any other Pins?
Please post the code, and a wiring diagram, for a "nonworking" setup, in which you have connected RX and TX as I suggested.
Code (from Martin Currey site):
// SerialIn_SerialOut_HM-10_01
//
// Uses hardware serial to talk to the host computer and AltSoftSerial for communication with the bluetooth module
//
// What ever is entered in the serial monitor is sent to the connected device
// Anything received from the connected device is copied to the serial monitor
// Does not send line endings to the HM-10
//
// Pins
// BT VCC to Arduino 5V out.
// BT GND to GND
// Arduino D8 (SS RX) - BT TX no need voltage divider
// Arduino D9 (SS TX) - BT RX through a voltage divider (5v to 3.3v)
//
#include <AltSoftSerial.h>
AltSoftSerial BTserial;
// https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html
char c=' ';
boolean NL = true;
void setup()
{
Serial.begin(9600);
Serial.print("Sketch: "); Serial.println(__FILE__);
Serial.print("Uploaded: "); Serial.println(__DATE__);
Serial.println(" ");
BTserial.begin(9600);
Serial.println("BTserial started at 9600");
}
void loop()
{
// Read from the Bluetooth module and send to the Arduino Serial Monitor
if (BTserial.available())
{
c = BTserial.read();
Serial.write(c);
}
// Read from the Serial Monitor and send to the Bluetooth module
if (Serial.available())
{
c = Serial.read();
// do not send line end characters to the HM-10
if (c!=10 & c!=13 )
{
BTserial.write(c);
}
// Echo the user input to the main window.
// If there is a new line print the ">" character.
if (NL) { Serial.print("\r\n>"); NL = false; }
Serial.write(c);
if (c==10) { NL = true; }
}
}
Wiring Diagram (also from M. Currey, except I'm using Mega 2560 instead of Nano):
If that doesn't work, then either you have made a wiring mistake, or the HM-10 has been damaged.
NOTE that with AltSoftSerial, pin numbers are fixed. For a Mega, you are required to use pins 46 for TX and 48 for RX.
Solved.
Not all pins on the Mega and Mega 2560 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).
(from https://www.arduino.cc/en/Reference/SoftwareSerial)
I changed the pins to 10 and 11 and it works.
But anyhow, thank you very much for your support!
(For some reason, ALL examples if found used pins 7 & 8 or 8 & 9)
You are not using SoftwareSerial, according to the code you posted.
yes but the AltSoftSerial use PIN 8& 9 which doesn't work either.
AltSoftSerial use PIN 8& 9 which doesn't work either
For the reason that I explained. Glad you got it working!
First, you should not use Software Serial or AltSoftSerial on the Mega when you have several extra hardware serial ports to use.
jremington has explained which pins to use on the Mega if for some unknown reason you insist on using AltSoftSerial.