Progamming with USB, problem with RX pin in use.

Hi, I should start out by saying I am an both an Arduino newbie and an Arduino forum newbie. I hope I have posted this in the right section.

I have an Arduino Nano (with onboard USB) connected to an HC-05 BlueTooth module on a breadboard.

All is working fine, I can talk to the BlueTooth etc, except that when I need to program the Nano, I need to disconnect the RX pin from the BlueTooth or else it won't allow me to upload the sketch.

I understand this is an issue that others have encountered

Unfortunately the final product will need to be able to be re-programmed every now and then, so I need to be able to overcome this issue.

Is there a way around this? Can I use different pins for RX/TX for the Bluetooth? Any external hardware solutions (apart from a physical switch)? After reading John Gammon's excellent article on low power projects, I will probably end up implementing everything using a separate USB programmer and a barebones Atmega328 - in this scenario, is it easier to overcome the problem?

Thanks for any assistance.

You could communicate with the blue tooth module using other pins and using the software serial library.

Grumpy_Mike:
You could communicate with the blue tooth module using other pins and using the software serial library.

Thanks for the reply. A few questions if you don't mind:

  1. So if I did that, I guess I could effectively separate the two streams? For example, I think at the moment, if I am plugged into the PC, the Serial.println commands are sent back to the PC via USB.

  2. Are there any other solutions? I am running a bit low on digital pins unfortunately

  3. On that same topic, is there a simple way (using a sketch) to detect when the USB is plugged in? Or would that require some monitoring of voltages that are not easily accessible (and some tricky soldering!)

Your help is very much appreciated.

Cheers

All is working fine, I can talk to the BlueTooth etc, except that when I need to program the Nano, I need to disconnect the RX pin from the BlueTooth or else it won't allow me to upload the sketch.

Is this disconnecting the bluetooth tx pin from the arduino rx pin? If so, you might try a small diode between the rx and tx pins with the diode band side connected to the tx pin.

zoomkat:
Is this disconnecting the bluetooth tx pin from the arduino rx pin? If so, you might try a small diode between the rx and tx pins with the diode band side connected to the tx pin.

Thanks for your help, sorry I didn't make it clear - yes you are right - the way I get around it is to disconnect the jumper lead that goes from the Bluetooth TX to the Arduino RX.

So you are suggesting I put a diode between the Arduino RX and the Bluetooth TX with the band side ("arrow towards") the Bluetooth module TX? Would a 1N4004 be sufficient?

So you are suggesting I put a diode between the Arduino RX and the Bluetooth TX with the band side (“arrow towards”) the Bluetooth module TX? Would a 1N4004 be sufficient?

That diode will probably work for testing. The diode will help keep the tx pin from holding the rx pin high, which may allow for normal program loading.

zoomkat:
That diode will probably work for testing. The diode will help keep the tx pin from holding the rx pin high, which may allow for normal program loading.

Sorry to ask a silly question, but if I have a diode connected in that direction, won't that then block the Bluetooth from being able to TX to the Arduino RX?

A software serial is the general solution for a problem like this.

rj87:

  1. So if I did that, I guess I could effectively separate the two streams? For example, I think at the moment, if I am plugged into the PC, the Serial.println commands are sent back to the PC via USB.

You'd have two different serial commands. "Serial.println" commands would go the the PC, "MySerial.println" would go to the Bluetooth module. I think you could call "MySerial" anything you want so you could use "Bluetooth.println" when sending a line to the Bluetooth module.

rj87:
2. Are there any other solutions? I am running a bit low on digital pins unfortunately

There's generally always other solutions. There are UART multiplexers which allow one to switch between various serial lines. As you mentioned earlier, a switch is a perfectly reasonable solution. You'd probably want a DPDT so you could switch both the tx and rx lines at the same time.

rj87:
3. On that same topic, is there a simple way (using a sketch) to detect when the USB is plugged in?

I know this is possible with other microcontrollers I've used and I'd be very surprised if it weren't possible with the Arduino. You would basicly monitor the serial line as an input before starting the serial driver. When the USB is connected, (I think) at least one of the lines should be in a different state than when the USB isn't connected.

Using a software serial is often very useful. IMO, it would be a good idea to learn to use one.

The other solution you could use is a switch to disconnect the blue tooth when programming.

rj87:
Sorry to ask a silly question, but if I have a diode connected in that direction, won't that then block the Bluetooth from being able to TX to the Arduino RX?

It shouldn't, as when the bluetooth transmitts, the tx is actually pulling the rx low, so the current flow is from the rx to the tx. The below post has a little more detail on the setup.

I would use a different Arduino. The Micro has separate USB and hardware serial and it's about the same size as the Nano.

zoomkat:
It shouldn't, as when the bluetooth transmitts, the tx is actually pulling the rx low, so the current flow is from the rx to the tx. The below post has a little more detail on the setup.

If the line is setup as an open drain then the tx line just pulls the line low but this is not the norm for TTL communication when there are two signal wires. When the pins are actively driven high and low it's possible to use much higher bit rates. I'd be very surprised if the Arduino's UART is open drain.

I did a quick search but all the results I found on the topic suggested the Arduino actively drives the tx line.

As I think about this a bit, I'm almost sure a software serial is actively driven since I've used with another microcontroller without pull-ups on the line.

zoomkat:
It shouldn’t, as when the bluetooth transmitts, the tx is actually pulling the rx low, so the current flow is from the rx to the tx. The below post has a little more detail on the setup.

http://forum.arduino.cc/index.php?topic=233906.msg1684842#msg1684842

This seems to be working! I sometimes need two attempts to upload code, but it is certainly more reliable than it was. Thanks so much! Also thanks to MorganS and GrumpyMike for their advice, however I think this might have done the trick.

DuaneDegn:
A software serial is the general solution for a problem like this.

You’d have two different serial commands. “Serial.println” commands would go the the PC, “MySerial.println” would go to the Bluetooth module. I think you could call “MySerial” anything you want so you could use “Bluetooth.println” when sending a line to the Bluetooth module.

Thank you - perhaps I could have separate lines at least for the RX - might this be a solution for monitoring when the USB is plugged in?

On that same subject, this post here has a method for detecting when the USB is connected, however it seems to be a fragment of code and I don’t have anywhere near the knowledge required to fill in the gaps. It involves looking at the 5V line from the USB (“VUSB”) that connects directly to the microprocessor at a pin labeled VBUS. The code is missing variable declarations etc. so it’s not implementable (for me at least!). Here is the code if anyone else can interpret it:

/***************************************************\
|Name: Detecting VBUS                               |
|Programmer: Michael James Salino-Hugg              |
|Date: February 27,2014                             |
|Description: Turns on pin 13 LED, establishes a    |
|             serial connection, and prints "Serial |
|             is connected" every second when VBUS  |
|             is high. It also ends the serial      |
|             connection and turns off the pin 13   |
|             LED when VBUS is low.                 |
\***************************************************/

void setup(){
   pinMode(13,OUTPUT);
   USBCON|=(1<<OTGPADE); //enables VBUS pad
}

void loop(){
   if(USBSTA&(1<<VBUS)){  //checks state of VBUS
      if(!Serial)
         Serial.begin(9600);
      digitalWrite(13,HIGH);
      Serial.println("Serial is connected");
   }
   else {
      if(Serial)
         Serial.end();
      digitalWrite(13,LOW);
   }
   delay(1000);
}

It looks complete to me. But it won't work on all Arduinos. It looks like it might work on a Due. Change your board to Due (you may need to download it with the boards manager) and try the "verify" button.

MorganS:
It looks complete to me. But it won't work on all Arduinos. It looks like it might work on a Due. Change your board to Due (you may need to download it with the boards manager) and try the "verify" button.

Thanks - the Due is overkill for what I am trying to do, so I might keep on seeking an option for the Nano.