Problem with Teensy 3.2, Serial1 and HC-06

Status
Not open for further replies.

SteveSFX

Well-known member
Hello

Still new to the world of Teensy... trying to upload a program that works on a Pro Mini, and it isn't liking the Serial1 for some reason.

I am using Serial1 to talk to a Bluetooth HC-06. But, it is basically telling me that Serial1 isn't available and I cannot see why.

My HC-06 TX is connected to Teensy pin 0, and the HC-06 RX pin is connected to pin 1.
I originally had these going through a 5v/3.3v level converter, but after reading up... I don't think that is required.

My code is messy and appalling, but the relevant bit is this (which I have tried on its own)....

After loading, I open the serial port to read the outcome. It just fails to read Serial1 as available. I am pretty sure I am basically doing something stupid, but I can't see the difference between this and a Pro Mini.

Code:
  Serial1.begin(9600);

if (Serial1.available()) {
  
      Serial.println("Starting Bluetooth configuration");
    
      Serial1.write("AT+NAMETest");                                             // Set up the Bluetooth comms
      delay(1000);
      
      Serial1.write("AT+PIN1234");
      delay(1000);
      
      /*
      The possible baudrates are:
        AT+BAUD1-------1200
        AT+BAUD2-------2400
        AT+BAUD3-------4800
        AT+BAUD4-------9600 - Default
        AT+BAUD5------19200
        AT+BAUD6------38400
        AT+BAUD7------57600
        AT+BAUD8-----115200
        AT+BAUD9-----230400
        AT+BAUDA-----460800
        AT+BAUDB-----921600
    */
      Serial1.write("AT+BAUD4");
      delay(1000);
      Serial.println("Configuration complete");
      
    } else { Serial.println("Bluetooth error");}

Any ideas? Thank you
 
The Serial1.available() function returns the number of already received characters in the serial buffer. At this moment when you call it in your code, the Teensy hasn't yet received anything from the BT module.

Thus, to see if the serial communication is up, you'd rather check simply with if(Serial1). And between Serial1.begin() and if(Serial1), you'll have to put something like delay(400) because the Teensy is so much quicker than the Pro Mini and will move on before the HC-06 had enough time to respond to the serial initialization.
 
Hmm... I tried that. Added various length delays up to delay(5000);

The code listed above is the only thing in the sketch.... it won't even work with this bare minimum.
Tried Serial2 - same.... doesn't allow it.

This seems it might be a settings issue within the Arduino IDE?

Code:
void setup() {
  // put your setup code here, to run once:

  Serial1.begin(9600);
  delay(5000);

if (Serial1.available()) {

      delay(5000);
  
      Serial.println("Starting Bluetooth configuration");
    
      Serial1.write("AT+NAMEGAUGES");                                             // Set up the Bluetooth comms
      Serial.println("Name: Gauges"); 
      delay(5000);
      
      Serial1.write("AT+PIN1234");
      Serial.println("PIN: 1234");
      delay(5000);
      
      /*
      The possible baudrates are:
        AT+BAUD1-------1200
        AT+BAUD2-------2400
        AT+BAUD3-------4800
        AT+BAUD4-------9600 - Default
        AT+BAUD5------19200
        AT+BAUD6------38400
        AT+BAUD7------57600
        AT+BAUD8-----115200
        AT+BAUD9-----230400
        AT+BAUDA-----460800
        AT+BAUDB-----921600
    */
      Serial1.write("AT+BAUD4");
      Serial.println("BAUD: 4 (9600)");
      delay(5000);
      Serial.println("Configuration complete");
      
    } else { Serial.println("Bluetooth error");}
    
}

void loop() {
  // put your main code here, to run repeatedly:

}
 
Does the Bt module output data continuously?

As noted in p#2 'if (Serial1.available())' shows how many characters are waiting from input to Seria1.

Is this the desired test or is this just to wait for when 'Serial1' is ready for use? If so this test is not needed, or would be this is if works like USB Serial on Teensy :: if (Serial1)

Or is this just looking to see if the USB Serial is connected to SerialMonitor like this to make sure the prints will show? :: if (Serial) {
 
No, there is most probably no settings issue with the Arduino IDE. But if you want to send debug output to the serial monitor in the IDE, you'll have to add Serial.begin(9600) before Serial1.begin(9600). If not, the Teensy will send the debug output into the Nirwana.

If that doesn't still solve your problem, it's time to unpack your logic analyzer and to track the communication between the Teensy and the HC-06
 
Logic analyzer would be very handy.... but I don't have one.

It is not talking to the BT module with or without the (Serial1.available())
The led on the BT module should go steady as its being 'programmed' and it doesn't.

Jumped this short code back over to a Pro Mini and it worked straight away

The serial.print statements are showing in the Serial monitor (which surprised me).

Hmmm
 
With a line like this before printing it will allow 4 seconds for SerMon to connect :: while (!Serial && millis() < 4000 );

With the delay(5000) as shown it will work the same way and USB Serial to SerMon will have time to connect - it only takes about 400-600 ms depending on system in use.

And when the Teensy is programmed for USB - it is automatically started and this line is not required on Teensy :: Serial.begin(115200);
 
Well something is now completely screwed up...

I have not seen the LED stop flashing once yet. The only time I have seen it 'connect' is when I use the Bluetooth terminal app on my phone.
So, I connected with that and sent the commands to set the baud etc... (so that I knew they were correcct) but not convinced that actually changed anything.

Played around with the Teensy for another 15 mins.... now nothing connects.
The phone shows it in the Bluetooth list, but will not connect.

Time to give this up I think
 
I think this may be a HC-06 issue.
Can't change it at all using my phone app either. Ordered a couple of new ones
 
OK. I have a new HC-06. Nope... still not working.
Clearly the command is not correct.

If I short serial 1 output, then I can see the reflected commands in the monitor... so that is working.
Now... Pins 0 and 1 on a Teensy 3.2 are actually Serial1 are they not!!! Says they are on the crib sheet that came with the IC.

So, if I initialise Serial1, and wait 3 seconds (more than enough)... how come it still cannot report back that Serial1 is available?
 
OK. I have a new HC-06. Nope... still not working.
Clearly the command is not correct.

If I short serial 1 output, then I can see the reflected commands in the monitor... so that is working.
Now... Pins 0 and 1 on a Teensy 3.2 are actually Serial1 are they not!!! Says they are on the crib sheet that came with the IC.

So, if I initialise Serial1, and wait 3 seconds (more than enough)... how come it still cannot report back that Serial1 is available?

After the init Serial1 it should be immediately available without wait or check as I've used it when properly connected to an active device.

Yes, Serial1 is as indicated on Pin0_RX and Pin1_Tx.

As noted and queried above the line :: if (Serial1.available())
>> Is asking - are bytes ready in the read buffer, not is the port ready for use.

Have not used HC-06 so not sure of how it works - assuming it is not sending out any messages { like a GPS does } - but rather just waiting to hear commands over the UART port. As long as it is running at 3.3V to make sure the signal values are the right voltage for HIGH/LOW when connect RX>TX and TX>Rx at the right baud rate commands will be sent.

On return from :: Serial1.begin(9600);
It can be assumed that Serial1 is initialized and ready for use, I always just do and it works that way.

Not sure if prior AVR/UNO/Arduino experience is on your mind - but on Teensy Serial is a dedicated hardware USB port wholly independent of Serial1 UART hardware.
 
I removed the if (Serial1.available()) command..... it is just going straight to the write commands with a pause between each one.
Not sure what else to try. I know this is a good HC-06, so clearly its a coding issue.

I fear I may have to fall back to a Pro Mini (I know that works). Shame

Code:
void setup() {
  // put your setup code here, to run once:

  Serial1.begin(9600);
  delay(5000);

      Serial1.write("AT+NAMEGAUGES");                                             // Set up the Bluetooth comms
      delay(5000);
      
      Serial1.write("AT+PIN1234");
      delay(5000);
 
      Serial1.write("AT+BAUD4");
      delay(5000);
     
    
}

void loop() {
  // put your main code here, to run repeatedly:

}

This is as basic as it gets. Nothing
The new HC-06 is default at 9600 (checked with supplier)
 
I don't see any code looking for feedback/output from the device? That would confirm communications at the right baud rate with right wiring - and show what returns.

Call something like showMe(); after Serial1.begin() and then after issuing each command - and in loop()?
Code:
void showMe() {
  int foo;
  delay(20); // optional to wait for incoming chars
  foo=Serial1.available();
  while ( Serial1.available() ) {
    Serial.print( (char)Serial1.read() );
  }
  if (foo) Serial.println(foo);
}
 
Thanks...

Code:
void setup() {
  // put your setup code here, to run once:

  Serial1.begin(9600);
  showMe();
  delay(5000);

      Serial1.write("AT+NAMEGAUGES");                                             // Set up the Bluetooth comms
      showMe();
      delay(5000);
      
      Serial1.write("AT+PIN1234");
      showMe();
      delay(5000);
 
      Serial1.write("AT+BAUD4");
      showMe();
      delay(5000);
     
    
}

void loop() {
  // put your main code here, to run repeatedly:

}

void showMe() {
  int foo;
  delay(20); // optional to wait for incoming chars
  foo=Serial1.available();
  while ( Serial1.available() ) {
    Serial.print( (char)Serial1.read() );
  }
  if (foo) Serial.println(foo);
}

Nothing....
 
Have you tried swapping Rx/Tx lines?

If you can see it work on the other Arduino then the baud rate is right - what shows there with the showMe() code?
 
RX and TX are correct (I have tried swapping them). HC-06 has its own clean 5v supply (they are noisy things).
Nothing shows with the showMe() code.

I shorted RX and TX and typed in commands in the monitor.... they reflected back.

Stumped. Used these before many times. I do know that sometimes you need Serial.write and sometimes Serial.print works.... but nothing works.
It's late, and too tired to think now. Thanks for the suggestions.
I will pick it up again tomorrow, or just bail on the Teensy (not a good first project to start with!).
 
Hitting Amazon the HC-06 I came up with showed 3.3V. If you are running it at 5V the communications may be suspect without level shifters.

To talk properly with a Teensy it should run at 3.3V - or at least all the UART TTL lines should expect and respect 3.3V signals. For a 5V device it may not see 3.3V as high enough to be HIGH.

A T_3.2 on digital capable pins won't be harmed by 5V - but the outputs won't go over 3.3V and that may be ignored by a 5V device looking for higher.
 
I had it through a level shifter to start. However the HC-06 actually has 5v written on it as the VCC and 3.3v on the RX/TX pins
 
I had it through a level shifter to start. However the HC-06 actually has 5v written on it as the VCC and 3.3v on the RX/TX pins

Just guessing without one in front of me … Have used GPS spitting UART Serial and multiple Teensy on Serial# ports to another Teensy or programming an ESP8266.

Should have GND between as well as Rx<>Tx between the two right, that is another required connect thing I can't see.

Is the AVR/Arduino thing 3.3V? The old one stopped working with that too? Was the new one tested on that?

With the showMe() in place the Teensy Serial1 Rx<>Tx connect should echo back the commands as it sends them to USB Serial from the posted code?
 
Just tried the HC-06 running on its own 3.3v regulator. Still no change.
Yes, sharing a common ground.
Maddening.... I have hugely complicated builds going on with 10+ Arduino Mega's all talking to each other over serial... gyros, TFT screens etc...

and yet.... this defeats me :(

I would not be anything to do with CPU speed? Should not make a difference, but straws+clutching
 
This is where I am. All the serial port does is report back 4x 'Checking...'
Tried Serial2..... still nothing

Code:
void setup() {
  // put your setup code here, to run once:

  delay(2000);
  Serial2.begin(9600);
  showMe();
  delay(1000);

      Serial2.write("AT+NAMEGAUGES");                                             // Set up the Bluetooth comms
      showMe();
      delay(1000);
      
      Serial2.write("AT+PIN1234");
      showMe();
      delay(1000);
 
      Serial2.write("AT+BAUD4");
      showMe();
      delay(1000);
     
    
}

void loop() {
  // put your main code here, to run repeatedly:

}

void showMe() {
  Serial.print("Checking....");
  int foo;
  delay(20); // optional to wait for incoming chars
  foo=Serial2.available();
  while ( Serial2.available() ) {
    Serial.print( (char)Serial2.read() );
  }
  if (foo) Serial.println(foo);
}

Short the RX/TX line, and you see the commands reflected back to the serial monitor. So the HC-06 is basically ignoring what you send it.
I am out of ideas
 
Last edited:
That same code on the 8 bit Arduino would show something - what? - from that device?

What does Serial2.print("AT+NAMEGAUGES"); show?

And with Rx2 crossed/shorted to teensy Tx2?
 
I will have to go and dig out a Pro Mini....

Rx2 and Tx2 shorted basically fires back:

Checking....Checking....AT+NAMEGAUGES13
Checking....AT+PIN123410
Checking....AT+BAUD48
 
Well. Time to give this hobby up :confused:

Mini Pro - Nope. Tried it with exactly the same bit of code I have used twice in another projects and it doesn't work at all.

Tried using Software Serial (on the Pro Mini)..... nope
Tried new power supply and also a battery.... nope
Tried 3x HC-06's..... nope
Tried every Baud speed at startup just is case its not the factory 9600..... nope

But, I can connect to the HC-06 with my phone... so it works.

The circuit has nothing in it except the damn HC-06.

Never had anything like this. 2 days trying to get this to work.
Time to move on I think
 
Status
Not open for further replies.
Back
Top