cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f4discovery problem with hc-05 bluetooth module

raykill99
Associate II
Posted on March 17, 2014 at 16:56

I'm having a problem with my usart communication between stm32f4 and hc-05 bluetooth module. I'm trying to configure the AT command of HC-05 using the stm32 board and i'm checking the response using the USB serial module and serial monitor.  But i'm not getting any response. I'm using USART3. Can you please tell me if you find any problem in my pin configuration or my code. thanks

This is my pin configuration:

stm32 3v pin ---------> hc-05 vcc and key pin

stm32 tx --------------> hc-05 rx pin

hc-05 tx ---------------> USB serial rx pin (to check the response on serial monitor)

this is the code that i'm using:

#include ''stm32f4xx.h''

#include ''stm32f4xx_gpio.h''

#include ''stm32f4xx_rcc.h''

#include ''stm32f4xx_usart.h''

#include <string.h>

#include <stdio.h>

GPIO_InitTypeDef              GPIO_InitStructure;

USART_InitTypeDef             USART_InitStructure;

void USART_put(USART_TypeDef* USARTx, volatile char *s){

    while(*s){

        // wait until data register is empty

        while( !(USARTx->SR & 0x00000040) );

        USART_SendData(USARTx, *s);

        while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)

                {

                }

        *s++;

    }

void GPIO_USART_Configuration(void){

     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

     /* Configure USART Tx as alternate function  */

       GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

       GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

       GPIO_Init(GPIOC, &GPIO_InitStructure);

     /* Connect PXx to USARTx_Tx*/

       GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);

 }

 void USART3_Configuration(void)

 {

         USART_InitStructure.USART_BaudRate = 38400;

         USART_InitStructure.USART_WordLength = USART_WordLength_8b;

         USART_InitStructure.USART_StopBits = USART_StopBits_1;

         USART_InitStructure.USART_Parity = USART_Parity_No;

         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

         USART_InitStructure.USART_Mode = USART_Mode_Tx;

         USART_Init(USART3, &USART_InitStructure);

         USART_Cmd(USART3, ENABLE); // enable USART2

 }

void usart_inisialisasi()

 {

     GPIO_USART_Configuration();

     USART3_Configuration();

 }

int main(void) {

usart_inisialisasi();

while(1)

    {

USART_put(USART3, ''AT\r\n'');

delay(2000);

}

}

9 REPLIES 9
Posted on March 17, 2014 at 17:52

What type of USB-to-Serial? None of the voltages here will be RS232 compatible.

If you look at the STM32 TX side, can you see the AT characters?

Why spin on TC bit? And why on USART1?

Why *s++, wouldn't s++ suffice?
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
raykill99
Associate II
Posted on March 17, 2014 at 20:40

I don't know the type of the usb serial cause i just borrowed it from the lab. But i don't think the voltage level is the problem cause i already tried usart comunication between the stm32 to usb serial (i can see the AT characters) and usb serial to hc-05 (i tried sending the at command using the serial monitor and it works cause i'm getting an OK response).

As for the usart1, sorry it's my mistake, this is from my recent project that using 2 usart communication, but i already erase it in my code.

I change the *s++ to s++ but it does no change.. 

raykill99
Associate II
Posted on March 17, 2014 at 20:50

thanks clive for your help. i still don't understand why but i need to plug the usb serial after i plug the stm32f4discovery supply cable first. If i tried to do the other way around, i will not getting any response. Can you tell me the reason?

michaelmccartyeng
Associate II
Posted on March 17, 2014 at 22:58

Hello Fitdra,

  the hc-05 is uart level. If you do a ''AT+VERSION'' is it the bolutek or linvor firmware ? Or something else?

  You have already used it with a usb<->Uart module right ? This will confirm the baud etc.

  Some of these modules like /n and some like /r/n.

  You say your code is already working, but use it to connect to your usb through putty and save a log of the output, then open that in n++ and set view->show all symbols and ensure all the chars are getting across correctly.

   Some of these modules have to go into ''command mode'' etc. Let me know your experience with your hc-05 and your usb /terminal input and we will go from there.

raykill99
Associate II
Posted on March 18, 2014 at 00:30

Thank you for the response mccarty,

I haven't tried to connect my usb through putty cause i never use it. but maybe i will try it later.

I tried sending the at+version and this is the response i get :+VERSION:2.0-20100601

I don't know about the firmware but it's not linvor (usually for slave only device and HC-05 is master/slave device). I looked at the datasheet, it says the device has HC-05 firmware.

The baud rate is configured using the command mode. I follow all the step to make it into command mode (set the key pin high) and i'm using the default baud rate 38400 (i already checked it  like you told me).

From the datasheet, i need to send CR and LF (/r/n) after each command.

raykill99
Associate II
Posted on March 18, 2014 at 00:49

Now i get a new problem.

 I actually need to plug and unplug my usb serial from my PC a few times (usually 2-5 times) to get a response from the bluetooth module. why is that?

michaelmccartyeng
Associate II
Posted on March 18, 2014 at 01:29

Well if you soldered to the board instead of using the breakout board (

http://www.ebay.com/itm/Wireless-Serial-4-Pin-Bluetooth-RF-Transceiver-Module-HC-06-RS232-With-backplane-/200924726178?pt=LH_DefaultDomain_0&hash=item2ec80c03a2

) you might be having all kinds of issues. I remember some of these modules will get ruined if you use 5v. I hope you have more than one module, I've got a stack of burnt up hc-05's. If you let me know what you are trying to do I might be able to suggest a better solution. These modules are cheap but they all come with different firmware with different bugs and quirks, and its hard to find modules with the same firmware once you have figured them out. I have also had issues with my usb<->uart devices. Sometimes they will not work again till I restart my computer. Unplugging them does not remove them from device manager. Try unplugging usb stuff from your computer, or use a powered usb hub, or alternate power for the hc- I stopped using the power from usb to uart for this reason. I dont think I've worked with that firmware. Think about it this way. If you send with your computer and it works, then you send the same thing with stm32 and it does not work then maybe stm32 is not outputting correctly. But you say you connected stm32 to your computer and you see the expected output ? So if your stm and your computer are feeding the same thing into the bluetooth module why do you not see the same results ? Another thing you can do is plug the RX line of your usb<->uart device into the tx line of the hc- this way you have two devices listening, then if the hc05 is outputting something you will see it. Do not plug two TX lines together though. I''m debugging with a wifi module right now and this is how I do it STM32 TX ---- > RX HC05 STM32 RX -----> TX HC05 |........... RX USB module So it works like a splitter. Use the RX of your usb module to listen on either end and see if that helps you debug. This is the code I use to send out data

/**
* @brief 
* sends buffer out without dma using polling 
* 
* @note 
* @param buffer buffer to send data from 
* @param size the size of the buffer, must not be greater than the buffer!
* @retval none
*/
void sendBuffer(u8* buffer, int size)
{
int i=0;
// todo convert size to ascii 
for(i=0; i<size; i++)
{
// wait for it to go out
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, buffer[i]);
}
}

raykill99
Associate II
Posted on March 18, 2014 at 15:47

Hallo mccarty,

I think you are right. I  used the board that was soldered by myself. I just bought a new HC-05 with the breakout board and i don't have any problem configuring the at command at all for now. Thank you very much for the help.

Andrew Neil
Chief II
Posted on March 18, 2014 at 22:37

''I just bought a new HC-05 with the breakout board and i don't have any problem configuring the at command at all for now''

Perhaps, in messing about with loads of commands, you managed to accidentally configure your old unit into an unusable state?

Check the documentation for how to reset everyting to factory defaults...