Connect teensy 3.2 to HC-05 bluetooth module

Status
Not open for further replies.
Hi everybody,

I already could connect an arduino uno to a HC-05 bluetooth module successfully .
Unfortunatelly I am not possible to connect to a teensy 3.2. I do not know which are the relevant PINs on the teensy?
Could you please help me?
Where are:
- 5 V
- RX
- TX?

on the teensy?

Last question: When I upload my sketch to the Arduino (which uses RX ad TX) I have to disconnect the RX/TX cable. Is it the same with the teensy?

Many Tanks
Regards Mario
 
Last edited:
On the Teensy:
+5v - Depends - Does your version need +5v or can it take 3.3v. If it can take 3.3v you can connect it to the 3.3v pin near the USB connector. If it requires something like +5v, then it depends on how you are powering your Teensy. if you are powering this through USB, you can use the Vin pin. If you are using your own power supply that connects up to Vin, then hopefully you get it there.

RX/TX - On the Uno you have one Serial port, which doubles duty with the USB connection. With Teensy you have 4 Serial connection. The USB connection is on Serial. And then you have 3 USARTS which are Serial1, Serial2, and Serial3.

Which pins you use? Many who convert stuff over from Uno stick with pins 0 (RX), 1(TX), but you have many other options, depending on your needs. You can for example Use Serial2 which by default uses pin 9(RX) and 10(TX) or use Serial3 which default pins are 7(RX) and 8(TX). Note Serial1 and 2 have more capabilities than Serial 3 (larger hardware buffer), which may or may not impact your usage.

Also some of these USarts have other pins that can be used for RX or TX. You will see them on the Card that comes with the Teensy as being typed with gray text instead of black...

Also with your sketch you will need to change everywhere you talk to the device from using things like Serial.read and Serial.write to use device that you are talking too, like Serial1.write or Serial2.read...

Good luck
 
to help you out, here is the simplest sketch to test that you are reading something from Serial1, and printing it to your terminal window:

Code:
String receivedText = "";

void setup() {
  Serial1.begin(9600); //rx1 and tx1 = pins 0 and 1 on Teensy
  delay(200);
}

void loop() {
   if (Serial.available()) Serial1.write(Serial.read());
  while(Serial1.available() > 0) { // While there is more to be read, keep reading.
    receivedText += (char)Serial1.read();
    delay(10);  
  }

  if(receivedText != "") Serial.println(receivedText);
  receivedText = "";
}
 
Hi Morton and Kurt,

many thanks! It works

Could you explain the code? Why do I need to this coding line?


if (Serial.available()) Serial1.write(Serial.read());

Regards Mario
 
You are welcome,

As for code example,
That above line, just says if there is a character available on the USB serial port, read it in and send it out to the Serial1 port.

Another example of similar code, is an example program that ships with Teensyduino:
If you do menu item: file->Examples->Teensy->Serial->EchoBoth

It brings up a simple program, which simply echoes everything it receives form the USB serial port over to the hardware serial port (default Serial1) and likewise from the Hardware Serial port back to the USB serial port. Example usage if you had an XBee or BlueTooth connected up to Serial1, then it works like a forwarder.
Code:
/* UART Example, any character received on either the real
   serial port, or USB serial (or emulated serial to the
   Arduino Serial Monitor when using non-serial USB types)
   is printed as a message to both ports.

   This example code is in the public domain.
*/

// set this to the hardware serial port you wish to use
#define HWSERIAL Serial1

void setup() {
	Serial.begin(9600);
        HWSERIAL.begin(38400);
}

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);
	}
}

Note: for a true simple forwarder I might code it like:
Code:
/* UART Example, any character received on either the real
   serial port, or USB serial (or emulated serial to the
   Arduino Serial Monitor when using non-serial USB types)
   is printed as a message to both ports.

   This example code is in the public domain.
*/

// set this to the hardware serial port you wish to use
#define HWSERIAL Serial1

void setup() {
	Serial.begin(9600);
        HWSERIAL.begin(38400);
}

void loop() {
        int incomingByte;
        
	while ((incomingByte = Serial.read()) >= 0) {
                HWSERIAL.write(incomingByte);
	}
	while ((incomingByte = HWSERIAL.read()) >=0) {
		Serial.write(incomingByte);
	}
}
Note: typed on fly so could be problems.
 
Hi Kurt,

so there is a technical difference between RX/TX an RX1/TX1
As I interpret your answer: Only SERIAL can "listen" to a port, SERIAL1,2,3 cannot listen to a port. They receive their data from SERIAL?
Is this correct?

Regards
Mario
 
On the Teensy 3.x/LC, if the connection is via USB, you use Serial. If the connection is via a UART line like is used in the HC05, you use Serial1, Serial2, or Serial3. Each of Serial1, Serial2, or Serial3 has a separate set of pins that is used. For example, Serial1 uses pin 0 for reading the bytes (RX1) and pin 1 for sending the bytes (TX1). Depending on how the remote device is setup, you might connect the Teensy's RX1 to the device's TX and the Teensy TX1 to the device's RX. Or perhaps the device has it labeled to be consistent with the device pins. I've seen both ways. You really need to read the datasheet, or experiment before doing the final soldering.

On some other microprocessors, the same UART is shared with the USB line, so you would write to Serial (and if you have a USB connection, you have the potential for the two to interfere with each other).
 
think of it as Receive and Transmit pin pairs in this instance. Also, you can send commands to the bluetooth module when it starts up and when not paired, so the line "if (Serial.available()) Serial1.write(Serial.read());" is about the module receiving a command sent via the teensy.
 
No All of them can listen for input. There are technical differences.

The Teensy has USB support built into the chip, so the Serial object uses code to talk to this.

Serial1, 2, 3 have USARTS on the chip (like the one Uart on the Mega). Again you can send and receive on all of these. But again there are some Technical differences. In particular Serial1 and Serial2 hardware have hardware FIFO receive and transmit queues, where as Serial3 simply has a double buffer like the Mega does. They all work great, but if you are doing lots of Serial IO at a high baud rate, Serial1 and 2 can do it better with having to process fewer Interrupt requests...

Again All three work great and all three can do transmit and receive.
 
Status
Not open for further replies.
Back
Top