Stm32 bluetooth

greetings, I am measuring rpm in a motor by means of the pulses of a hall effect sensor, I am using a stm32 Nucleo F411RE and I want to send the data to the cell phone using the hc-06 bluetooth module, however, the data I receive are random characters, could you give me an idea of what is happening?
I have already installed stm32 core for arduino IDE

#include <SoftwareSerial.h>
const byte hallPin = 6;  // Pin 6 (Interrupcion externa )
// Variables de tiempo
volatile unsigned long lastTime = 0;
volatile unsigned long currentTime = 0;
volatile unsigned long deltaTime = 0;
volatile bool newReading = false;
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
  Serial.begin(115200);
  pinMode(hallPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(hallPin), countRPM, FALLING);  // Interrupción en flanco de bajada
  mySerial.begin(38400);
}
void loop() {
  if (newReading) {
    noInterrupts();  // Flag para evitar multiples lecturas en una unica pasada
    unsigned long duration = deltaTime;
    newReading = false;
    interrupts();

    // Calcular RPM
    if (duration > 0) {
      float rpm = 60000000.0 / duration;  // 60e6 microsegundos en un minuto
      if (rpm<10000){
      Serial.print("RPM: ");
      Serial.println(rpm);
      mySerial.print(rpm);
      mySerial.println(rpm);
    } 
  }
}
}
// Función de interrupción
void countRPM() {
  currentTime = micros();  // Tiempo actual en microsegundos
  deltaTime = currentTime - lastTime;  // Diferencia con la última vuelta
  lastTime = currentTime;
  newReading = true;
}

Are the baud rates the same, that is what you see if they are different. I assume mySerial is the bluetooth connection and it is set to 38,400baud. Make sure the phone is also 38,400baud, I have no idea how to do that.

why are you using softwareserial when the STM32 has hardware serial ports?

cannot find my stm32 Nucleo F411RE but did find some results for a HC-06 connected to Serial1 hardware port of a stm32 Nucleo LO73RZ (which has a similar pinout to the F411RE)
code (tools>Board set to Nucleo-64)

// STM32 Nucleo-LO73RZ board serial1 to HC-06 Bluettooth Classic module

// HC-06 TX to STM32 D2
// HC-06 RX to STM32 D8
// HC-06 VCC to STM32 5V GND to GND

// Serial is to pins D0 RX and D1 Tx NOT USB
// connect FTDI USB-RS232-3v3
// orange to D0 RX
// yellow to D1 TX
// black to GND

// Serial1 mapped to RX D2 (PA10) and TX D8 (PA9) 
HardwareSerial Serial1(PA10, PA9);

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  while (!Serial)
    ;
  Serial.begin(115200);
  Serial1.begin(9600);//115200);
  delay(2000);
  Serial.println();
  Serial.println("STM32 Nucleo board Serial1 test \nConnect FTDI USB-RS232-3V3 cable");
  Serial.println("orange to D0 RX - yellow to D1 TX -  black to GND");
  Serial.println("Serial1 mapped to RX D2 (PA10) and TX D8 (PA9) - connect for loopback test");
  Serial.println("HC-06 TX to STM32 D2 - HC-06 RX to STM32 D8");
}

// display *, blink LED and read Serial echo ASCII code
void loop() {
  //Serial.print('*');
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(100);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  delay(100);                      // wait for a second
  if (Serial.available()){
    char ch=Serial.read();
    //Serial.println(ch);  // print ASCII code of character
    Serial1.write(ch);
  }
  if(Serial1.available()){
    char ch=Serial1.read();
    //Serial.print("\nSerial1 read ");
    Serial.print(ch);
   // Serial.println(" <<");
  }
}

I first tested the Serial1 by connecting D2 to D8 for a loopback test - all OK
then connected HC-06 (baudrate 9600)

// HC-06 TX to STM32 D2
// HC-06 RX to STM32 D8

paired Android phone with HC-06 OK and ran Terminal app which connected OK
Serial monitor output

STM32 Nucleo board Serial1 test
Connect FTDI USB-RS232-3V3 cable
orange to D0 RX - yellow to D1 TX -  black to GND
Serial1 mapped to RX D2 (PA10) and TX D8 (PA9) - connect for loopback test
HC-06 TX to STM32 D2 - HC-06 RX to STM32 D8
22345567
hello from android
abcdefghijklmnopqrst

Android Terminal app displays

photo

2 Likes

Neither does anybody else but, fortunately, the baud rate in the phone is irrelevant to the exercise.

1 Like

Really, in post 3 the baud rate was changed to 9600 and it worked.

The baud rate between the Arduino and the HC06 was changed as well as switching from software serial to hardware serial.

It has nothing to do with the baud rate on the phone. The phone and the module are communicating with BT frequency.

1 Like

my HC-06 was set to 9600baud hence the code in post 3
it is possible that the HC-06 module used in post 1 had had the baudrate changed using the AT+BAUD38400 command

Good morning, you were right, it really worked, thank you very much.