Connecting an HC-06 Bluetooth Module to Teensy 3.1

This page will describe how to set up bluetooth and send commands from a cell phone to the Teensy. Connections are made as in the schematic, and power is supplied from the USB port.

Load this sketch into the Teensy:

String message; //string that stores the incoming message #define HWSERIAL Serial1 const int ledPin = 13; void setup() { pinMode(ledPin, OUTPUT); digitalWrite(ledPin, HIGH); // set the LED on Serial.begin(9600); //set baud rate HWSERIAL.begin(9600); } void loop() { while(Serial.available()) {//while there is data available on the serial monitor message+=char(;//store string from serial command } if(!Serial.available()) {//This comes from the console if(message!="") {//if data is available Serial.println("<" + message); //show the data HWSERIAL.print(message); //show the data message=""; //clear the data } } while(HWSERIAL.available()) {//while there is data available on the serial monitor message+=char(;//store string from serial command } if(!HWSERIAL.available()) {//This comes from the bluetooth device (such as phone) if(message!="") {//if data is available Serial.println(">" + message); //show the data HWSERIAL.println(message); //show the data if(message=="0") digitalWrite(ledPin, LOW); // set the LED off if(message=="1") digitalWrite(ledPin, HIGH); // set the LED on message=""; //clear the data } } delay(2000); //delay }

This code turns on the power LED and waits for input from a bluetooth device. Sending "0" will turn off the LED. Sending "1" will turn is back on. Note that the code is also marked as to which input comes from the serial console, and which comes from the bluetooth device. This is a very simple example, but it provides a framework for accepting more complex commands.

You can change the information on the bluetooth module. The HC-06 defaults to command-mode, then goes into transmit mode when you pair with it. Plug the USB port into your Teensy but do not pair with the bluetooth device yet. Launch the serial monitor from the arduino desktop program. The AT-commands are case-sensitive, so you MUST enter them in all upper-case. Start by typing "AT+NAME" in the serial monitor and clicking the send button. In a couple seconds you should receive the reply "OKsetname". This means you can successfully talk to the device and it is in command mode. Now try changing the name to something meaningful: "AT+NAMEdrgw_13". Once you set the name, try scanning for new devices on your phone. If you previously paired with the BT module already, it will continue to show the old name until the first time you pair with it again.

You can also set or delete the pin number with "AT+PINxxxx" where the x's represent up to a 4-digit number. Leaving off the number will erase the pin.

ATCheck the connection
AT+NAMExyzChange name
AT+PINxxxxchange pin, xxxx is the pin number
AT+BAUDxchange baud rate, x is baud rate code

No space between command and code