0

I've been cracking my head over this for a long while.

Like many before me, I'm trying to send and receive data from a HM-10 bluetooth module, arduino mega adk and the app called BLE Scanner. I've figured out receiving alright. Whatever I write into the serial monitor, shows up on the app. Whatever I write into the software serial, shows up. However, I haven't figured out how to read from the software serial. The available function never gets triggered, and always printing out the software serial read is always -1

This is my connection

The app supports sending data over bluetooth as well. However, using:

    #include <SoftwareSerial.h>
SoftwareSerial blueSerial(14, 15); //RX, TX
  blueSerial.begin(9600);
  Serial.begin(9600);
while (1)
  {
    blueSerial.listen();
    if (Serial.available()) {
      Serial.println("Writing");
      blueSerial.write(Serial.read());
      Serial.println(Serial.read());
      Serial.println(blueSerial.available());
    }
    if (blueSerial.available()) {
      Serial.println("Blueserial writing");
      Serial.write(blueSerial.read());
    }
}

The listen function is used out of pure desperation.

How do I fix this? This should be working right? Since I want to be sending AT commands to the bluetooth modules at some time and read their output, getting this to work is really important.

9
  • Have you tested the HM10 using a usb to serial adapter?
    – MrFixIt87
    Commented Nov 13, 2018 at 1:00
  • What do you mean? The Arduino is connected to the laptop, and I'm reading of Arduino IDE's Serial Monitor. I don't have any special cables but the one it came with(the one that puts the code in the board)
    – Nephilim
    Commented Nov 13, 2018 at 1:30
  • 1
    Why on earth are you using software serial????
    – Majenko
    Commented Nov 13, 2018 at 8:40
  • 1
    How do you take this tone and not suggest an alternative? @Majenko
    – Nephilim
    Commented Nov 13, 2018 at 14:26
  • 1
    If hardware serial doesn't work, then you have a problem with either your hardware or your code. When you have a problem, the right approach is to troubleshoot it: find out what is wrong and fix it. You can ask for help here if needed. If you just give up and switch to software serial, then instead of fixing the initial problem you are just creating another problem. Commented Nov 13, 2018 at 19:36

3 Answers 3

1

Possibly the built-in UART (Hardware Serial) assigned to the same pins is interfering with SoftwareSerial? If you really want to us SoftwareSerial, try it on a pair of pins that has no hardware UART attached.

And/or try SoftwareSerial (again, on a non-UART pair of pins) to connect to your PC (after uploading; you can't upload with it), and use Serial1 to connect to your device. This isn't the recommended or easiest way but it might help debug the problems you're seeing.

1

On Mega you don't need to use SoftwareSerial. Mega has 4 Serials. Connect the module to RX3/TX3 like on the Fritzing in your question and use Serial3 in sketch.

void setup() {
  Serial.begin(9600);
  Serial3.begin(8600);
}

void loop() {

  while (Serial.available()) {
    Serial3.write(Serial.read());
  }
  while (Serial3.available()) {
    Serial.write(Serial3.read());
  }
}
2
  • I have to confirm, that what worked on UNO over SoftwareSerial was not working on Mega ... but switch to Serial2 instead worked.
    – Uli
    Commented Mar 3, 2022 at 11:17
  • @Uli, on Mega not all pins support SoftwareSerial
    – Juraj
    Commented Mar 3, 2022 at 12:20
0

If SoftwareSerial is not working then simply do not use it.

You shouldn't be using it anyway.

You have a Mega with 4 UARTs. You are using a pair of the UART pins, and yet you are using SoftwareSerial.

Don't.

instead use the SerialX object (where X is the number by the TX and RX for the pins) to communicate.

SoftwareSerial is only for those times when you don't have enough hardware UARTs, and you have plenty.

2
  • Serial3 didn't work in either direction which is why I switched to software serial in the first place
    – Nephilim
    Commented Nov 13, 2018 at 16:54
  • @Juraj I rewired the whole thing a bunch of times, it's now on Serial with TX->1 and RX->0 . Same issue throughout.
    – Nephilim
    Commented Nov 16, 2018 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.