Receive data from serial on pins 0/1

Status
Not open for further replies.

elder0010

New member
Hi, I have an HC-05 Bluetooth module and a Teensy 3.2 wired like this:

Bluetooth Teensy
Tx -> Rx (pin 0)
Rx -> Tx (pin 1)
3.3v -> 3.3v
Gnd -> Gnd.

The HC-05 receives data but I can't get it in my sketch using pins 0/1. Here is the code:

Code:
#define HWSERIAL Serial1

void setup() {
  Serial.begin(9600);
  HWSERIAL.begin(9600);
  while(!Serial);
  while(!HWSERIAL);
  Serial.print("Started!");
}

void loop() {
   int incomingByte;
        
  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    Serial.print("USB received: ");
    Serial.println(incomingByte, DEC);
  //  HWSERIAL.print("USB received:");
  // HWSERIAL.println(incomingByte, DEC);
  }
  if (HWSERIAL.available() > 0) {
    incomingByte = HWSERIAL.read();
    Serial.print("UART received: ");
    Serial.println(incomingByte, DEC);
    HWSERIAL.print("UART received:");
    HWSERIAL.println(incomingByte, DEC);
  }
  delay(100);
  
}

The test sketch that sends data (from an arduino uno using bluetooth) is also very simple:
Code:
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

int x =0;
void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(0);
  delay(400);
  
}
I am 100% sure that the HC-05 is working ok, i have tested it with a Leonardo and everything goes O.K.
The USB serial works perfectly, if i try to type something in the serial monitor I get:
Code:
USB received: 119
USB received: 113
USB received: 13
USB received: 10
USB received: 97
USB received: 13
USB received: 10

But the UART on pins 0/1 seems totally unresponsive. I also tried to wire the leonardo TX to the teensy RX to bypass the bluetooth, but it didn't worked. I have tried to set the CPU speed to 24 and 96 mhz, and nothing changed. Is there something that I am missing here? Everything works nicely using Serial2 (pins 9/10) instead of Serial1 - pins 0/1.
 
Last edited:
You should use Serial1 for U(S)ART #1. Serial is the USB interface.

Indeed this is correct - on Teensy USB is Serial and the first hardware serial port is Serial1 that uses pins 0/1.

Code:
void setup() {
  [B]Serial1.begin[/B] (9600);
}

...

  while ([B]Serial1.available[/B]()) {
    char inChar = (char)[B]Serial1.read[/B]();
    if (inChar == '\n') {
      stringComplete1 = true;
    }
    else {
      inputString1 += inChar;
    }
  }
 
This part doesn't make much sense to me, with the understanding that Serial is the USB connection to your PC and HWSERIAL is your HC-05 Bluetooth module.

Code:
  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    Serial.print("USB received: ");
    Serial.println(incomingByte, DEC);
  //  HWSERIAL.print("USB received:");
  // HWSERIAL.println(incomingByte, DEC);
  }
  if (HWSERIAL.available() > 0) {
    incomingByte = HWSERIAL.read();
    Serial.print("UART received: ");
    Serial.println(incomingByte, DEC);
    HWSERIAL.print("UART received:");
    HWSERIAL.println(incomingByte, DEC);
  }

When you get data from your PC, you echo a message back to the PC, but you are NOT sending the byte to the HC-05.

When you get data from the HC-05, you're printing a verbose message to your PC, but you're also printing that verbose message back to the HC-05. So for every 1 byte you get from the HC-05, you're sending it a message with at least 18 bytes.

Maybe this isn't going to end up doing quite what you want?

Perhaps a first good step, if you're using #define HWSERIAL might be to create two #define with names like MYPC and HC05. Then your could would look like this:

Code:
  if (MYPC.available() > 0) {
    incomingByte = MYPC.read();
    MYPC.print("USB received: ");
    MYPC.println(incomingByte, DEC);
  //  HC05.print("USB received:");
  // HC05.println(incomingByte, DEC);
  }
  if (HC05.available() > 0) {
    incomingByte = HC05.read();
    MYPC.print("UART received: ");
    MYPC.println(incomingByte, DEC);
    HC05.print("UART received:");
    HC05.println(incomingByte, DEC);
  }

Just by using more meaningful names you could much more easily see what it will really do.
 
Last edited:
Status
Not open for further replies.
Back
Top