Using ATmega328p with 2 HC-05 bluetooth UART problem

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all, 

 

first time posting here so don't hesitate to tell me if something is wrong with this post.

 

I'm trying to build a project with two ATmega328p chips and two HC-05 (ZS-040) bluetooth module communicating with each other.

 

I have configured the two bluetooth module properly (I think, for some reason the default baud rate was 9600 so that was weird but I got the AT commands to work and respond).

I've gathered simple codes online to initialize and send strings in the while loop with some delay.

 I connected this MCU with the master module (linked to the slave) and connected the slave to an arduino to use its serial monitor, it just prints weird characters in the serial monitor with exact delay that I wrote in my code.

/*
 * UART_test.c
 *
 * Created: 7/11/2019 3:46:01 PM
 *
 */ 

#define F_CPU  16000000
#define FOSC 16000000 // Clock Speed
#define BAUD 38400
#define MYUBRR FOSC/16/BAUD-1

#include <avr/io.h>
#include <util/delay.h>
void USART_Init( unsigned int ubrr)
{
    /*Set baud rate */
    UBRR0H = (unsigned char)(ubrr>>8);
    UBRR0L = (unsigned char)ubrr;
    /*Enable receiver and transmitter */
    UCSR0B = (1<<RXEN0)|(1<<TXEN0);
    /* Set frame format: 8data, 1stop bit */
    UCSR0C = (3<<UCSZ00);
    UCSR0A = (1<<UDRE0)|(1<<U2X0);

    }

unsigned char USART_receive( void )
{
    /* Wait for data to be received */
    while ( !(UCSR0A & (1<<RXC0)) )
    ;
    /* Get and return received data from buffer */
    return UDR0;
}

void USART_send(unsigned char data)
{
    /* Wait for empty transmit buffer */
    while (!(UCSR0A & (1<<UDRE0)))
    ;
    /* Put data into buffer, sends the data */
    UDR0 = data;
}

void USART_putstring(char* StringPtr){
    
    while(*StringPtr != 0x00){    //Here we check if there is still more chars to send, this is done checking the actual char and see if it is different from the null char
        USART_send(*StringPtr);    //Using the simple send function we send one char at a time
    StringPtr++;}        //We increment the pointer so we can read the next char
    
}
    
int main(void)
{
    char string[] = "Hello?\n";
    USART_Init(MYUBRR);
    while (1) 
    {
        USART_putstring(string);
        _delay_ms(500);
    }
}

Connection:
MCU         HC-05 (Master)

RXD          TXD

TXD          RXD via voltage divider 

 

Arduino    HC-05 (Slave)

TX            TX (note no divider)

 

I assume I'm doing something very wrong here because I have minimum knowledge on UART.

The purpose of the project is basically using a sensor wirelessly using bluetooth and use the information from the sensor to display something to a LCD, and the sensor part can start or stop sensing depending on user input also wirelessly from a physical switch.

Any suggestion would be appreciated.

 

update1:

factory resetted both bluetooth modules and the baud rate is 38400, so I reconfigured them to match.

Last Edited: Thu. Nov 7, 2019 - 09:26 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Reading:

 

https://howtomechatronics.com/tutorials/arduino/arduino-and-hc-05-bluetooth-module-tutorial/

 

it appears that if you pair your PC to HC05 it will simply appear as a virtual COM listed as "Standard Serial over Bluetooth link" in device manager. So if you do that and then use a PC terminal like teraterm/realterm/etc to connect to the COMn port it creates do you correctly receive the characters the AVR sends? (IOW reduce the "unknowns" - take the Arudino/Serial monitor out of the picture for the time being)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I tried sending only 's' instead and used realterm, I'm receiving 0xFD which is not the right hex for 's' 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

update: I somehow got it to work, I used another arduino with no code on it and it worked perfectly fine. It worked in realterm as well.

Thanks for the help!