HC-05 Bluetooth. Arduino. Change baud rate 38400. Send random to app

Hello friends,

  • To carry out applications with Bluetooth between Arduino and App Inventor, the HC-06 module is usually used. This module can only work as a client, enough for most of the projects that you will see in the tutorials, it is the most used and recommended. Default 9600.
    bluetooth1
  • Another similar module is the HC-05, this can function as a server or a client. You may have to set the speed using AT commands to adapt it to App Inventor. It comes with 38400 baud and you will have to change it to 9600. Note that it has 6 terminals and a button.
    bluetooth2

1.- Change baud rate with AT commands. HC-05

  • We are going to change the baud rate from 38400 to 9600 using AT commands.

On the internet you can find many tutorials on how to change the baud rate using AT commands, here I will put a small summary. This is a tutorial in Spanish.

#include <SoftwareSerial.h>
SoftwareSerial BT(10,11);
 
void setup()
{
  BT.begin(38400);
  Serial.begin(38400);
  // BT.begin(9600);
  // Serial.begin(9600);
}
 
void loop(){ 
  if(BT.available()) {Serial.write(BT.read());}
 
  if(Serial.available()){BT.write(Serial.read());}
}
  • Programming in Mode 2

To program in Mode 2, we remove the red wire (5 V) from the module's power supply. We press the small button on the module. We connect the red wire to 5 V by holding down the button. When the red wire is connected, we release the button.

The module's LED will blink slowly.

  • Serial Monitor: 38400 bauds.
    bluetooth4

  • We wrote:

AT response OK
AT+UART? response +UART: 38400,0,0
AT+UART=9600,0,0 response OK
AT+UART? response +UART: 9600,0,0

Now module with 9600 bauds.

4 Likes

2.- Arduino with the HC-05 Bluetooth module sends two random numbers to the app.

p9A0i_bluetooth_aleatorios_V2.aia (3.3 KB)

const unsigned long interval= 3000;
unsigned long previousMillis;
unsigned long currentMillis;

String send_this;

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

void loop() {
 currentMillis = millis();
 if (currentMillis - previousMillis >= interval){
   float t = random(0,10000) / 100.0;
   float h = random(0,10000) / 100.0;

   send_this = String(t,2) + "," + String(h,2);
   Serial.println(send_this);
   previousMillis = millis();
 }
}
  • App Inventor. Blocks.

- Screenshot.

bluetooth8

5 Likes

3.- App sends two random values ​​separated by commas to the HC-05.

p9A0i_bluetooth_aleatorios_V3.aia (3.2 KB)

String a;
String b;

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

void loop() { 
  if(Serial.available()) {
    a = Serial.readStringUntil(',');
    b = Serial.readStringUntil('\n');
    Serial.print(a);
    Serial.print(" , ");
    Serial.println(b);
  }
}

4.- AT commands with HC-06.

To use AT commands with the HC-06 module, we will use the following configuration:

bluetooh_hc06

#include <SoftwareSerial.h>
SoftwareSerial BT(10,11); // TX to pin_10. RX to pin_11 of Arduino.

void setup() {
  Serial.begin(9600);
  BT.begin(9600);
}

void loop() {
    if(Serial.available()) {   
    String command = Serial.readStringUntil('\n');
    Serial.println(command); 
    BT.print(command);
    }
 
   if (BT.available()){
   String retorno = BT.readStringUntil('\n');
   Serial.println(retorno);
   }
}
  • Serial Monitor with New line.
    bluetooth_hc06_2

AT
AT+VERSION
AT+NAMEmyhc06
AT+BAUDx

1 .. 1200
2 .. 2400
3 .. 4800
4 .. 9600
5 .. 19200
6 .. 38400
7 .. 57600
8 .. 115200

1 Like