Connecting HM-10 to hardware serial on Uno

Hi,

I have a basic sketch that works great for sending AT commands to HM-10 (bluetooth 4.0 module) and receiving data back from the HM-10. But recently I have read that it might be wiser to use the hardware serial of the UNO (digital pins 0 and 1 if I am not mistaken) rather than a software serial. So, with that in mind, I want to modify this sketch, but try as I might, nothing seems to work.

My goal is to plug in the RX/TX lines of the HM-10 module to the 0 and 1 digital pins of the UNO and then be able to communicate back and forth without creating a SoftwareSerial as well. I know I am missing something obvious here, but after two hours of searching, I am stumped.

Any suggestions would be greatly appreciated:

#include <SoftwareSerial.h>

SoftwareSerial HM10(2, 3); //HM10(Receive Pin, Transmit Pin) 

void setup()
{
  Serial.begin(57600);  // Begin the Serial Monitor connection at 57600bps
  HM10.begin(57600);  // Begin the HM-10 connection at 57600bps
}

void loop()
{
  if (HM10.available()) // Read from HM-10 and send to Serial Monitor
    Serial.write(HM10.read());
  
  if (Serial.available()) // Read from Serial Monitor and send to HM-10
    HM10.write(Serial.read());
}

FYI, the “funny” thing is that I can achieve this goal simply by uploading a blank sketch to the UNO and then just opening up a serial monitor window. But when I try to put this into code, I can’t figure it out.

If you use the pin 0 and 1 for the BT module, then you can't use the USB to Serial to connect your console to your arduino - it's one or the other, not both

J-M-L:
If you use the pin 0 and 1 for the BT module, then you can't use the USB to Serial to connect your console to your arduino - it's one or the other, not both

OK, I vaguely understand what you are saying...

With that in mind, another option perhaps: How about if I want the sketch to send AT commands to the HM-10 BL Module, have the HM-10 react accordingly (for example, scan for beacons and then send the scan results back to the UNO) BUT use a software serial to display those results on the serial monitor while I am debugging the sketch. Later, once everything is debugged, I would then "deactivate" (comment out) the software serial and just let the sketch run without me monitoring it?

-Is that possible?
-How would I modify the sketch to do that?
-Later after deactivating the software serial, would the UNO still receive info back from the HM-10 and be able to parse it or do whatever it should based on that data?

(Sorry if I am being dense about this stuff. Still grasping at straws a bit on this stuff in spite of reading the serial communication sticky ten times.)

I think the answer to my question is somewhere in this sketch which I just dug up from the internet.

Especially these words: “If you want to view via Serial Monitor and have your Arduino connect to BT module simultaneously use Software Serial to open a second port.”

“A second port”??? Is he saying that I can connect the HM-10 to TWO ports at the same time, i.e. hardware serial AND software serial (3, 4) at the same time??? How would that be possible?

Or is he saying I could connect the HM-10 to the hardware serial OR ALTERNATIVELY a software serial?

Still not sure what this all means, but I will keep at it to see if I can figure it out:

/*
Connect Bluetooth RX,TX to Arduino RX,TX
Requires NeoPixel Library
https://github.com/adafruit/Adafruit_NeoPixel
If you want to view via Serial Monitor and have your Arduino connect to BT module 
simultaneously use Software Serial to open a second port.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 4); // RX, TX
*/



#include <Adafruit_NeoPixel.h>

int red,green,blue;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(10, 6, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

  // initialize serial:
  Serial.begin(9600);
  // mySerial.begin(9600);
}

void loop() {
  // if there's any serial available, read it:
  while (Serial.available() > 0) {

    // look for the next valid integer in the incoming serial stream:
    int red = Serial.parseInt(); 
    // do it again:
    int green = Serial.parseInt(); 
    // do it again:
    int blue = Serial.parseInt(); 

    // look for the newline. That's the end of your
    // sentence:
    // if (mySerial.read() == '\n') {
    if (Serial.read() == '\n') {
      // sends confirmation
      Serial.println("received");
      // constrain the values to 0 - 255
      red = constrain(red, 0, 255);
      green = constrain(green, 0, 255);
      blue = constrain(blue, 0, 255);
      // fill strip
      colorSet(strip.Color(red, green, blue), 0); 
      
      // send some data back
      mySerial.println("received:"+String(red)+","+String(green)+","+String(blue));
     
   }
}
}

// Fill strip with a color
void colorSet(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
  }
   strip.show();
   delay(wait);
}

Zimbu:
BUT use a software serial to display those results on the serial monitor

You can only display the results on the serial monitor via hardware serial.

Later, once everything is debugged, I would then "deactivate" (comment out) the software serial and just let the sketch run without me monitoring it?

It is quite OK to do all that and then simply replace all the software serial commands with hardware and move bluetooth to pins 0,1. Indeed, if your Arduino is then stand-alone, it is a good idea. Your only problem is that, if anything stuffs up, you are faced with the tedium of re-writing the software serial back in. It is far for sensible to stick with hardware all the way, and simply remember to disconnect bluetooth when you use serial monitor to debug. If you forget, Arduino will remind you...... All this arises because serial monitor shares pins 0,1 with your hardware serial.

How would that be possible?

It isn't.
If you have bluetooth on HARDware serial you cannot upload software but, after you have disconnected to upload, you can reconnect and communicate with another device with serial monitor still connected. This enables you so see what's going on, but you can only watch.
Using hardware serial exclusively is not nearly as difficult as it sounds.
I think your original code might be suss. I believe the HM-10 takes AT commands at 9600, and you ought to check this. Further, software serial is unreliable at 57600, irrespective of whether you are using it in AT mode or communications mode.

UPDATE: Maybe just ignore what I wrote below. I just read Nick's full comments (thanks).

From what I have read elsewhere and from J-M-L's comments earlier, I guess that I can attach HM-10 to pins 0 and 1, BUT if I do so, then pins 0 and 1 (which access the USB to UART convertor) can no longer send info back and forth to my computer's Arduino IDE serial monitor.

OK, so far, so good, BUT...

What really bugs me is that a blank sketch uploaded to the UNO works great. I can open the Arduino IDE serial monitor and send & receive commands/data back and forth from the HM-10 that is plugged into pins 0 and 1. Why can't the same thing be accomplished with a sketch???

That frustration aside, looking at Nick's hint, I'm thinking that I should create a software serial on pins 3 and 4. Then Serial read whatever comes in or out of pins 0 and 1, and write that info over onto pins 3 and 4, then print that info to the serial monitor. Sorta like "spying" on pins 0 and 1, copying whatever happens there over to pins 3 and 4, then printing that pin 3 and 4 info to the serial monitor screen.

Does any of this spying idea make sense?

Wow, getting away from SoftwareSerial connection to the HM-10 is much more difficult than I expected!

I think you jumped the gun with reply #4! Are things more clear now?

Zimbu:
I can open the Arduino IDE serial monitor and send & receive commands/data back and forth from the HM-10 that is plugged into pins 0 and 1.

I don't think that is really happening - even if it looks like it is.

Nick_Pyner:
...
If you have bluetooth on HARDware serial you cannot upload software...

From my experiments today, it seems I actually can upload new software to the UNO via USB cable when the HM-10 is connected to (0, 1). But what I think you mean is that the HM-10 will not be accessible under those conditions. Since I uploaded software via USB, UNO then assumes that (0, 1) should be used for serial monitoring, i.e. it ignores the fact that I have the HM-10 connected to (0, 1).

So, tomorrow I will try to load new software via USB. Then unplug USB, then attach HM-10 to (0, 1) then connect wall wart power to the UNO, and see what happens!

I think you may have said that I could THEN connect the USB cable in order to watch on serial monitor what's happening on pins (0, 1) BUT not be able to send any commands. Is that correct?

Re your other comment about HM-10 baud: Default is 9600, but I changed it via AT command to 57600. So far I have not had any problem doing this. I've had a HM-10 running independently (receiving AT commands from Arduino sketch, scanning for beacons, and sending back results to the Arduino) for more than a year under these conditions.

Nick_Pyner:
I don't think that is really happening - even if it looks like it is.

To confirm the situation:
-Arduino UNO
-HM-10 (v550) with RX/TX connected to digital pins (0, 1) on UNO
-HM-10 is in central role, i.e. acting as a scanner looking for beacons
-USB cable plugged into UNO and plugged into iMac
-No outside power attached to UNO
-Upload blank sketch to UNO
-Open up serial monitor in Arduino IDE, type in "AT", and get back "OK" response from HM-10.
-Type in "AT+DISI?" and get back scan results of all nearby beacons from HM-10.
-Shut down one of the beacons and repeat the "AT+DISI?" scan. The shutdown beacon no longer appears in the scan results.

I think that proves it is doing what I said it is doing...But still does't explain why I can't get it to do the same thing with a sketch.

Frustrating and fascinating at the same time!

Zimbu:
From my experiments today, it seems I actually can upload new software to the UNO via USB cable when the HM-10 is connected to (0, 1). i.e. it ignores the fact that I have the HM-10 connected to (0, 1).

I don't think that is right, and the IDE should protest most vigourously.

So, tomorrow I will try to load new software via USB. Then unplug USB, then attach HM-10 to (0, 1) then connect wall wart power to the UNO, and see what happens!

That should be OK but you can have wallwart and USB connected at the same time, indeed it is probably safer for debugging purposes.

I think you may have said that I could THEN connect the USB cable in order to watch on serial monitor what's happening on pins (0, 1) BUT not be able to send any commands. Is that correct?

Correct. Just for watching, but that can do a lot for your confidence.

Re your other comment about HM-10 baud: Default is 9600, but I changed it via AT command to 57600. So far I have not had any problem doing this. I've had a HM-10 running independently (receiving AT commands from Arduino sketch, scanning for beacons, and sending back results to the Arduino) for more than a year under these conditions

I'm suss about what you are actually doing and saying, but I could be wrong about what I am saying.
Bluetooth has two modes of operation - AT mode and communications mode.

You can change your comms speed to 57600 by using AT mode. Bluetooth confirms it, and you will be left in no doubt about it because you have to have Arduino communicating at that speed with bluetooth.

You cannot change the AT mode speed, which is just as well because it would be a really bad idea, and I'm pretty sure that that is 9600. I say this guardedly because, while I have a couple of HM-10s, I have never actually used them.

Zimbu:
-HM-10 is in central role, i.e. acting as a scanner looking for beacons

OK. I don't know anything about beacons, and I withdraw my comment about two modes of operation. Apparently there is a third mode I know nothing about.

Open up serial monitor in Arduino IDE, type in "AT", and get back "OK" response from HM-10.
-Type in "AT+DISI?" and get back scan results of all nearby beacons from HM-10.
-Shut down one of the beacons and repeat the "AT+DISI?" scan. The shutdown beacon no longer appears in the scan results.

I think that proves it is doing what I said it is doing

No it doesn't. The "OK" proves you are in AT mode, but the speed is still far from clear.

...But still does't explain why I can't get it to do the same thing with a sketch.

I think it might, but I don't know anything about that beacon stuff, I'm just a bluetooth datalogger.

Frustrating and fascinating at the same time!

Indeed it is.....

The Atmega 1284 has two hardware ports.
Your UNO code will work with the 1284.

Member Crossroads sells several 1284 versions.
I have 5 of these, they are great.
Lots more I/O pins and much more SRAM and Flash.

http://crossroadsfencing.com/BobuinoRev17/index.html

You will need an FTDI cable to program these, can also be used on ProMinis.

You might get lucky - I’ve seen the UNO behave somewhat OK with a serial device plugged into 0/1 and still uploading fine, even handling serial console commands sometimes OK - but you should not rely on this

Your hardware wiring looks like this:

USB Tx ---> UNO Rx [color=red]<---- BT Tx[/color]
USB Rx <--- UNO Tx ---> BT Rx

When you send a HIGH from the USB Tx (there is a converter in the middle) the UNO Rx does see the HIGH but you also connect the 5V positive rail into your BT Tx which is an OUTPUT.

On an arduino Pins configured as outputs can be damaged or destroyed if they are connected to either the ground or positive power rails. Your BT component might be protected against this but I would not rely on this.

The MEGA and alternate boards have multiple hardware serials - use that

J-M-L:
Your hardware wiring looks like this:

USB Tx ---> UNO Rx [color=red]<---- BT Tx[/color]

USB Rx <— UNO Tx —> BT Rx



When you send a HIGH from the USB Tx (there is a converter in the middle) the UNO Rx does see the HIGH but you also connect the 5V positive rail into your BT Tx which is an OUTPUT. 

On an arduino Pins configured as outputs can be damaged or destroyed if they are connected to either the ground or positive power rails. Your BT component might be protected against this but I would not rely on this.

I think this HM-10 BT Module is definitely protected against this; I wire the TX/RX wires backwards about 49% of the time!

Your diagram is interesting. Seems like it definitely should not work, but it does when the UNO has a blank sketch, and I simply use USB serial monitor to send commands to and receive data from the HM-10. As you note, it really doesn’t make sense that it works like this, but weirdly it does. Of course, when I try to do this with a sketch, I think some logic inside the UNO firmware takes over and prevents this, i.e. it just decides to ignore the HM-10 that is plugged into the same pins as the USB serial monitor.

J-M-L:
The MEGA and alternate boards have multiple hardware serials - use that.

Where’s the fun in that?!? Haha! But seriously, I agree that’s seems to be the way to go. I just happened to get a hardware serial “curiosity itch” yesterday and spent 5 hours trying to scratch it.

On the other hand, I ultimately only need the HM-10 to be plugged in while the UNO is running my sketch. The only reason I need the USB serial monitor also is to do some testing and debugging before actually letting my creation “wander free in the wild” untethered to the computer. So if Nick’s suggestion works (i.e. I can hook up HM-10 to hardware serial but still spy on the activity via the USB serial monitor), I may just stick with the UNO after all.

And to really drive you crazy, I want to swap out the UNO later for a Pro Mini Enhancement ATMEGA328P 16MHz 5V…which will probably open up another can of worms!

Thanks everyone for the input!

Zimbu:
.which will probably open up another can of worms!

It shouldn't. So long as it is a 5v Pro Mini, it's just a stripped-down Uno, and all your frustrations should have been played out on the Uno you already have.

Having said that, I don't know how good a proposition the 5v Pro Mini would be, free in the wild. This is something you need to check, and a whole new field probably awaits you........

Nick_Pyner:
It shouldn't. So long as it is a 5v Pro Mini, it's just a stripped-down Uno, and all your frustrations should have been played out on the Uno you already have.

Having said that, I don't know how good a proposition the 5v Pro Mini would be, free in the wild. This is something you need to check, and a whole new field probably awaits you........

What should I be worried about when using the 5v Pro Mini free in the wild? (Not really so wild; it will be installed in a box, hooked up to the HM-10 BLE module, and powered by a wall wart)

Zimbu:
powered by a wall wart

That means it is not in the wild, therefore no problem.
I raised the matter because there is a 3.3v Pro Mini which is probably more suitable for use in the wild, but I bet it brings its own problems when it comes to Bluetooth. I have a 5v Pro Mini but I am only just getting to use it. I understand it works just like a Uno, but it may be a bit more frugal.