0

Connecting hardware serial to HC-05 bluetooth
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Connecting hardware serial to HC-05 bluetooth

by oumichaelm on Mon Feb 11, 2019 1:51 am

I have a Circuit Playground Classic and a Circuit Playground Express and have been trying to connect an HC-05 bluetooth module. They are both so rich in sensors that I thought it would be a great start to learn how to send data over the Bluetooth connection to my phone to view the sensor data.

So to begin I started learning all I could about the HC-05 Bluetooth module. I was able to use an FTDI board to hook it up to my computer. I booted it up into command mode and used the serial monitor to verify the settings of the Bluetooth module. It was setup with the follow settings which is basically the way they all come.
AT+NAME:HC-05

* AT+UART:9600,0,0
*
* AT+ROLE:0
*
* AT+PSWD:1234
*
* AT+ADDR:98d3:33:8119d6

I then was able to pair the module with my phone and could send and receive data through the Serial Monitor built into the Arduino IDE.

So I know the module works and how to use it. The next step was to hook it up to the Circuit Playground.

I thought I found the perfect resource on the Adafruit Explore and Learn website
https://learn.adafruit.com/circuit-playground-express-serial-communications

The tutorial works through the entire concept and then has an Arduino page, a MakeCode page, and a Circuit Python code page. I'm still most comfortable in the Arduino world and so I gravitated to that page first. Unfortuantely its blank!

So I moved on to the MadeCode example.

Here is the MakeCode from the tutorial
Image

But when I went tired to enter the same code in the MakeCode browser, they seem to have changed the serial plugin because you can only change the RX and TX pins, there is no third option to change the Baud rate. It actually looks like this
Screenshot 2019-02-10 21.38.30.png
Screenshot 2019-02-10 21.38.30.png (6.5 KiB) Viewed 92 times


So I moved on to the circuit python example. I'm still learning and not at all comfortable with python but at this point I just wanted something that worked!
And luckily it did. This picture shows how I hooked up the Bluetooth module using VBat, GND, TX, and RX,
I know it is hard to see so here is the pin out.
CPY ---> HC-05
VBATT ---> VCC (RED)
GND ---> GND (Black)
TX ---> RXD (Yellow)
RX <---- TXD (White)

IMG_20190210_223017797.jpg
IMG_20190210_223017797.jpg (57.76 KiB) Viewed 92 times


So with this success, I was happy that things were working but I really wanted to get this working under Arduino Code. So I switched to the Circuit Playground Classic, hooked it up exactly the same way and tried this code.

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_CircuitPlayground.h>

long previousMillis = 0;        // will store last time Serial data was sent
 
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)
 
void setup() {
 
  Serial.begin(9600);
  Serial.println("Circuit Playground test!");

  CircuitPlayground.begin();
}


void loop() {
   CircuitPlayground.redLED(LOW); // turn off the built in LED
   
  while (Serial.available()) {
     CircuitPlayground.redLED(HIGH); // turn on the LED to let us know Serial is available
 
    Serial.println( Serial.read() ); //Just echo it back out
  }

   unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
   
    // save the last time you blinked the LED
    previousMillis = currentMillis;   
     
    /************* TEST LIGHT SENSOR */
    Serial.print("Light sensor: ");
    Serial.print(CircuitPlayground.lightSensor());

    /************* TEST THERMISTOR */
    Serial.print("   Temperature ");
    Serial.print(CircuitPlayground.temperature());
    Serial.println(" *C");

   
  }
 
}


This code works fine when hooked up to the computer and using the Serial Monitor. But it doesn't work when I hook up the Bluetooth module to the Serial TX and RX pins as pictured above.

The problem seems to be using the pins to access the hardware serial. I really hope to figure this out.

One of the best videos i found was this one describing Serial communications between a Arduino Mega and an Uno. https://www.youtube.com/watch?v=mFBpuUYdXPs But I don't have a Mega so can't try it.

And this instructable shows how to connect using Software Serial library using pins other than PIN 0 and PIN 1 https://www.instructables.com/id/Arduino-AND-Bluetooth-HC-05-Connecting-easily/ But I don't want to use the Software Serial library. I don't see why you just can't use these pins for TX an RX!

Please tell me what I'm doing wrong!!

oumichaelm
 
Posts: 3
Joined: Wed Jun 13, 2018 6:13 pm

Re: Connecting hardware serial to HC-05 bluetooth

by adafruit_support_mike on Mon Feb 11, 2019 5:52 am

The TX and RX pins on a Circuit Playground and Circuit Playground Express are identified in code as interface Serial1.

The microcontrollers on both boards handle USB internally, so the Serial connection that talks to the Arduino IDE's Serial Monitor only exists in code. The pads at the edge of the board are connected to a hardware Serial peripheral, and are free for connections to other Serial devices.

To set the baudrate, just use Serial1.begin(BAUDRATE), like with the connection to the computer.

adafruit_support_mike
 
Posts: 57106
Joined: Thu Feb 11, 2010 2:51 pm

Re: Connecting hardware serial to HC-05 bluetooth

by oumichaelm on Tue Feb 12, 2019 3:04 pm

adafruit_support_mike wrote:The TX and RX pins on a Circuit Playground and Circuit Playground Express are identified in code as interface Serial1..


It worked! I really thought I had tried Serial1 but forgot to mention it in my write up. I just changed my code from Serial to Serial1 and it ran just fine.

I reread the tutorial and I don't see it mentioned any where to use Serial1 for the pins.

Thanks so much for replying so quickly!

oumichaelm
 
Posts: 3
Joined: Wed Jun 13, 2018 6:13 pm

Re: Connecting hardware serial to HC-05 bluetooth

by adafruit_support_mike on Sun Feb 17, 2019 5:35 am

Glad to hear you got things working!

The use of Serial1 is fairly standard on boards whose microcontrollers handle USB communication internally. The interface identified in code as 'Serial' is always assumed to be the one that talks to the computer.

adafruit_support_mike
 
Posts: 57106
Joined: Thu Feb 11, 2010 2:51 pm

Please be positive and constructive with your questions and comments.