BTooth to IDE:TX/TX&RX/RX works, but for BT to sketch must use TX/RX & RX/TX !?!

Can anyone shed light on this behaviour?

Using an HC-05 Bluetooth and a Nano (non-genuine) and communicating via the h/w serial port (pins 0 & 1). HC-05 is connected to Android phone running Bluetooth serial terminal app.

To communicate b/w the HC-05 and the IDE monitor without going thru a sketch, this hookup is the one that works:

. HC-05 TX to Nano TX (pin 0) . HC-05 RX to Nano RX (pin 1) . Nano RST pin grounded, so sketch is suppressed

Text typed on the phone terminal app appears in the IDE monitor and vice versa.

But to communicate b/w the phone and a sketch, this is the hookup that works (opposite to above):

. HC-05 TX to Nano RX (pin 1) . HC-05 RX to Nano TX (pin 0) . Nano RST pin is not grounded . Sketch uses Serial.read to get char from the serial port and turns on LED (pin 13) if '1' received and turns it off if '0' received and Serial.print to echo the character received.

This works. Text entered on the phone is returned by the sketch to the phone (terminal's local echo is off). Entering '1' makes the LED come on and '0' makes it go off.

So, exact opposite connections required in each case. It's as if the sketch reverses the roles of pins 0 & 1, which you could do with software serial on other pins, but the statement used is the normal "Serial.begin(9600)".

Is this surprising or am I misunderstanding something fundamental (wouldn't be the first time)?

John.

Pin 1 on the Nano (TX1) is connected to the TX pin of the ATmega328P microcontroller, which runs your sketch. Pin 0 on the Nano (RX0) is connected to the RX pin on the ATmega328P.

There is a chip on the Nano that converts serial communication to USB communication with the computer, which then creates a virtual serial port for that USB communication to pass through. The connection between the ATmega328P and the USB to TTL serial adapter chip (FT232 or CH340) is RX-TX, TX-RX. So pin 1 on the Nano (TX1) is connected to the RX pin of the USB to TTL serial chip and pin 0 on the Nano (RX0) is connected to the TX pin of the USB to TTL serial chip. So when you connect the TX pin of the HC-05 to the TX0 pin on the Nano, you are actually connecting it to the RX pin on the USB to TTL serial adapter, and vice versa with the RX pin of the HC-05.

So it's not the matter of the "sketch reversing the roles", it's that you're communicating with a different chip, which has the opposite connections to the pins on the Nano.

Are you saying you connected the HC-05 to to the same serial port that is used to send serial communication to the PC? Don't do that. UART serial is not a multi master-bus.

A TX pin is an output pin creating a voltage. A RX pin is an input pin reading a voltage

. HC-05 TX to Nano TX (pin 0) . HC-05 RX to Nano RX (pin 1) . Nano RST pin grounded, so sketch is suppressed

This only seems to work because your Nano is leaving the pins alone. The HC-05 is sending data to another chip on your board. If the Nano would use the TX pin the same time you send data to the Serial Monitor it can destroy your I/O pins.

Thanks Pert and Klaus.

This is very helpful and unravels some puzzles that I've had.

I've always thought pin 0 was the first pin in the row, but really the first pin is TX1 pin and that's called pin 1. The second pin in the row is RX0 and that's called pin 0. A little confusing, but I'm glad I've now got the right terms. Ta.

I've sketched out your description, Pert, and I can see how the data flows when I have the sketch suppressed.

Can you tell me -- what happens when the sketch is running? Is the USB/TTL converter chip off limits to the sketch?

Klaus, yes I do connect it up that way. I have the sketch suppressed. This is a convenient way to send serial data straight to the monitor w/o need of a sketch. I've had a GPS module connected to a Nano and sending the GPS sentences to a phone via teh HC-05 for at least 20 hours. It works. Hasn't blown. So is it safe so long as the Nano is not running a sketch that attempts to use the converter chip?

Thanks again, John.

If you connect the gps module to a second uart you can use a very simple sketch to do this safely.

#define gpsSerial Serial1

void setup()
{
  Serial.begin( 9600 );
  gpsSerial.begin( 9600 );
}

void loop()
{
  if ( gpsSerial.available() )
  {
    char c = gpsSerial.read();
    Serial.write( c );
  }
  if( Serial.available() )
  {
    char c = Serial.read();
    gpsSerial.write( c );
  }
}

It also works with SoftwareSerial if your Arduino does not have a second uart. Just include the library and modify the SoftwareSerial call to your pins.

#include <SoftwareSerial.h>

SoftwareSerial gpsSerial(4, 3);

Can you tell me – what happens when the sketch is running? Is the USB/TTL converter chip off limits to the sketch?

The USB chip is connected to the UART. When you send a character it is send to the virtual COM port in your PC (thats what you see in the Serial Monitor), if you send a character from the serial monitor it is send to UART of the microcontroller. You can have a look into the schematic of your board on the Arduino website (if you have a original board).