How to use the HC-05 with teensy 3.2?

Status
Not open for further replies.

VeLXh

Member
Hi, everyone! I've been doing a rocket flight computer, following this series https://www.youtube.com/playlist?list=PLyV774-3p8348Fl5V6ciIBh0ZA446q93I and adding some new things, one of these things is a HC-05 bluetooth module, which would be responsible for transmit some data in small range. The first version worked well, I used a ATMEGA328p, and how can you see here, a gif and the schematic too ->https://imgur.com/a/FBIUugu<. But now, I'm using the teensy 3.2, everything is working well but the HC-05 is not transmitting the data, it's connecting, but is not showing anything >https://imgur.com/a/N72zAoq<.

Here is the code that I used in both:

Code:
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
 
const int MPU=0x68;  

Adafruit_BMP280 bmp; 

int AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ,bmpTemp,bmpPress,bmpAlt;

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B); 
  //Inicializa o MPU-6050
  Wire.write(0); 
  Wire.endTransmission(true);

  if(!bmp.begin(0x76)){
    Serial.println(F("O sensor bmp280 não foi identificado!"));
    while(1);  
  }
}
void loop(){
  Wire.beginTransmission(MPU);
  Wire.write(0x3B);  
  Wire.endTransmission(false);
  
  Wire.requestFrom(MPU,14,true);  
 
  AcX=Wire.read()<<8|Wire.read();      
  AcY=Wire.read()<<8|Wire.read();  
  AcZ=Wire.read()<<8|Wire.read(); 
  GyX=Wire.read()<<8|Wire.read();  
  GyY=Wire.read()<<8|Wire.read(); 
  GyZ=Wire.read()<<8|Wire.read();
  bmpTemp=bmp.readTemperature();
  bmpPress=bmp.readPressure();
  bmpAlt=bmp.readAltitude(1013.25);
  
  Serial.print(" | AcX = "); Serial.print(AcX);
   
  Serial.print(" | AcY = "); Serial.print(AcY);

  Serial.print(" | AcZ = "); Serial.print(AcZ);
   
  Serial.print(" | GyX = "); Serial.print(GyX);
   
  Serial.print(" | GyY = "); Serial.print(GyY);

  Serial.print(" | GyZ = "); Serial.println(GyZ);

  Serial.print(" | Temperature = "); Serial.print(bmpTemp);
    
  Serial.print(" | Pressure = "); Serial.print(bmpPress);

  Serial.print(" | Altitude = "); Serial.print(bmpAlt);
  delay(300);
}

Sorry for my english, I'm brazilian. And that is one of my very first post on a forum
 
Welcome.

Not sure of connections, but looking at the video it appears that Serial3 is wired? Not easy to see clearly with paused video.

How does that data get sent to the HC-05?

Not seeing Serial3.begin() or Serial3.print() in loop? Same would apply to Serial1 or other UART.

The Teensy USB Serial is not connected in parallel with any UART Serial ports like may be the case on ATMEGA328p.
 
As @defragster said welcome and bon gia (no not brazilian but wife is) Looks like all you data is just going to the Serial port and not the HC-05. Besides the info that @defragster stated you will also need to make sure that the baud rates between the HC-05 and the SerialX port selected need to match.

BTW - cool link :)
 
Dito with stuff mentioned by @defraster and @mjs513.

What I am wondering is if I look at the schematic for the T3.2 version
It looks like you have T3.2

If I look at your HC-5 item on your schematic it shows pins Serial_RX and Serial_TX signals going to it and on the T3.2 these signals are on pin
Serial pins as pins 5 and 6 which are NOT hardware Serial pins.

Oops - never mind - It not your HC-12 which is shown lower in the schematic on pins 7 and 8 which look correct.
Your T3.2 shows signals RX and TX On Serial3

And as mentioned I don't see anything setting up Serial3 nor outputting to it.
Default baud rate I think is 9600?
 
Welcome.

Not sure of connections, but looking at the video it appears that Serial3 is wired? Not easy to see clearly with paused video.

How does that data get sent to the HC-05?

Not seeing Serial3.begin() or Serial3.print() in loop? Same would apply to Serial1 or other UART.

The Teensy USB Serial is not connected in parallel with any UART Serial ports like may be the case on ATMEGA328p.

I'm sorry I didn't look at this before recording, but I had already tested it on all the serial ports, I just forgot to put it back on the third serial port. I didn't know about this, start the third serial port, I thought it was standard to start the serial as in Arduino, I'll try that. Just one more question, when I use serial.print, should I use Serial3.print? And thanks.
 
Yes - Serial.print goes to USB connection.
Serial3.print goes to Serial3... (likewise for all of the hardware Serial ports)

And again you need a call to begin with the baud rate. So something like: Serial3.begin(9600);
 
As @defragster said welcome and bon gia (no not brazilian but wife is) Looks like all you data is just going to the Serial port and not the HC-05. Besides the info that @defragster stated you will also need to make sure that the baud rates between the HC-05 and the SerialX port selected need to match.

BTW - cool link :)

HAHA, bom dia to you too, I was using the HC-05 like in arduino, I didn't need to insert anything specific, it seems just the correct connections and Serial.print is enough, HC-05 passes the data directly, at least with ATMEGA. And about the baud rates, if I weren't asking too much, could you explain how to do this?
 
Yes - Serial.print goes to USB connection.
Serial3.print goes to Serial3... (likewise for all of the hardware Serial ports)

And again you need a call to begin with the baud rate. So something like: Serial3.begin(9600);

Ok, I really thank you
 
Dito with stuff mentioned by @defraster and @mjs513.

What I am wondering is if I look at the schematic for the T3.2 version
It looks like you have T3.2

If I look at your HC-5 item on your schematic it shows pins Serial_RX and Serial_TX signals going to it and on the T3.2 these signals are on pin
Serial pins as pins 5 and 6 which are NOT hardware Serial pins.

Oops - never mind - It not your HC-12 which is shown lower in the schematic on pins 7 and 8 which look correct.
Your T3.2 shows signals RX and TX On Serial3

And as mentioned I don't see anything setting up Serial3 nor outputting to it.
Default baud rate I think is 9600?

Ok, hmm, as I said to @defragster, I unfortunately didn't see it, but I was testing it on all ports, and forgot to put it back on serial3. But about the code, I didn't put anything referencing to Serial3, I gonna try it now.
 
I did a quick and dirty edit of your program, note I did not try to compile it:
Code:
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>

#define HC05Serial Serial3 

const int MPU=0x68;  

Adafruit_BMP280 bmp; 

int AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ,bmpTemp,bmpPress,bmpAlt;

void setup()
{
  Serial.begin(9600);
  HC05Serial.begin(9600);
  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B); 
  //Inicializa o MPU-6050
  Wire.write(0); 
  Wire.endTransmission(true);

  if(!bmp.begin(0x76)){
    HC05Serial.println(F("O sensor bmp280 não foi identificado!"));
    while(1);  
  }
}
void loop(){
  Wire.beginTransmission(MPU);
  Wire.write(0x3B);  
  Wire.endTransmission(false);
  
  Wire.requestFrom(MPU,14,true);  
 
  AcX=Wire.read()<<8|Wire.read();      
  AcY=Wire.read()<<8|Wire.read();  
  AcZ=Wire.read()<<8|Wire.read(); 
  GyX=Wire.read()<<8|Wire.read();  
  GyY=Wire.read()<<8|Wire.read(); 
  GyZ=Wire.read()<<8|Wire.read();
  bmpTemp=bmp.readTemperature();
  bmpPress=bmp.readPressure();
  bmpAlt=bmp.readAltitude(1013.25);
  
  HC05Serial.print(" | AcX = "); HC05Serial.print(AcX);
   
  HC05Serial.print(" | AcY = "); HC05Serial.print(AcY);

  HC05Serial.print(" | AcZ = "); HC05Serial.print(AcZ);
   
  HC05Serial.print(" | GyX = "); HC05Serial.print(GyX);
   
  HC05Serial.print(" | GyY = "); HC05Serial.print(GyY);

  HC05Serial.print(" | GyZ = "); HC05Serial.println(GyZ);

  HC05Serial.print(" | Temperature = "); HC05Serial.print(bmpTemp);
    
  HC05Serial.print(" | Pressure = "); HC05Serial.print(bmpPress);

  HC05Serial.print(" | Altitude = "); HC05Serial.print(bmpAlt);
  delay(300);
}
But I did a #define at the beginning of your program defined as Serial3 and then replaced all Serial. with that define name. This way you can decide to change to a different one and only have to change the one define. For example if you decide: oops maybe better on Serial2 as Serial1 and Serial2 have larger hardware fifo queues so need less interrupts to be processed...
 
I did a quick and dirty edit of your program, note I did not try to compile it:
Code:
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>

#define HC05Serial Serial3 

const int MPU=0x68;  

Adafruit_BMP280 bmp; 

int AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ,bmpTemp,bmpPress,bmpAlt;

void setup()
{
  Serial.begin(9600);
  HC05Serial.begin(9600);
  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B); 
  //Inicializa o MPU-6050
  Wire.write(0); 
  Wire.endTransmission(true);

  if(!bmp.begin(0x76)){
    HC05Serial.println(F("O sensor bmp280 não foi identificado!"));
    while(1);  
  }
}
void loop(){
  Wire.beginTransmission(MPU);
  Wire.write(0x3B);  
  Wire.endTransmission(false);
  
  Wire.requestFrom(MPU,14,true);  
 
  AcX=Wire.read()<<8|Wire.read();      
  AcY=Wire.read()<<8|Wire.read();  
  AcZ=Wire.read()<<8|Wire.read(); 
  GyX=Wire.read()<<8|Wire.read();  
  GyY=Wire.read()<<8|Wire.read(); 
  GyZ=Wire.read()<<8|Wire.read();
  bmpTemp=bmp.readTemperature();
  bmpPress=bmp.readPressure();
  bmpAlt=bmp.readAltitude(1013.25);
  
  HC05Serial.print(" | AcX = "); HC05Serial.print(AcX);
   
  HC05Serial.print(" | AcY = "); HC05Serial.print(AcY);

  HC05Serial.print(" | AcZ = "); HC05Serial.print(AcZ);
   
  HC05Serial.print(" | GyX = "); HC05Serial.print(GyX);
   
  HC05Serial.print(" | GyY = "); HC05Serial.print(GyY);

  HC05Serial.print(" | GyZ = "); HC05Serial.println(GyZ);

  HC05Serial.print(" | Temperature = "); HC05Serial.print(bmpTemp);
    
  HC05Serial.print(" | Pressure = "); HC05Serial.print(bmpPress);

  HC05Serial.print(" | Altitude = "); HC05Serial.print(bmpAlt);
  delay(300);
}
But I did a #define at the beginning of your program defined as Serial3 and then replaced all Serial. with that define name. This way you can decide to change to a different one and only have to change the one define. For example if you decide: oops maybe better on Serial2 as Serial1 and Serial2 have larger hardware fifo queues so need less interrupts to be processed...

I just realized that my serial monitor doesn't work if I change it, like if I leave it on Serial, it works, but when I change it to Serial2 or Serial3, it doesn't work. I'll try to record to show
 
Yes with the changes I made quick and dirty, it still only goes to one place. There are a couple of easy fixes.

a) Simply copy the print lines and make second copy that still outputs to Serial. - I would probably start off with that one..

b) I probably not recommend, but could create a new class either based off of Stream or Print class where you provide your own write function.
Something like:
Code:
class DualPrint: public Print{
public: 
	DualPrint(HardwareSerial *pserial) : _pserial(pserial) {}

	// From Print will use default except for one byte version
	using Print::write; 
	virtual size_t write(uint8_t b) {
		Serial.write(b);
		pserial->write(b);
		return 1;
	}

	virtual int availableForWrite(void) {
		int avail_usb = Serail.availableForWrite();
		int avail_serial = pserial->availableForWrite();
		if (avail_usb <= avail_serial) return avail_usb;
		return avail_serial;
	}
private:
	HardwareSerial _pserial;
};
Then in your code you could have an instance of this class, where you pass in pointer to which Serial port you wanted, like:
DualPrint monitor(&Serial3);

You would then still in init, initialize your Serial port (Serial3) and initialize Serial.

And change all of your outputs like: HC05Serial.print(" | AcX = ");
to instead do: monitor.print(" | AcX = ");

There can be enhancements to the above, like maybe check if Serial is valid before you ask for available for write or... And again typed on the fly so could have issues.

But again this may be total overkill and confusion, so again would recommend starting with a)
 
Status
Not open for further replies.
Back
Top