Page 1 of 1

Help with Bluetooth Door Lock

Posted: Thu Jul 09, 2009 10:15 am
by Tomatoeyes55
Hello All,

I am a newbie so I apologize in advance if these questions are ridiculously obvious.

I am trying to make a bluetooth door lock that is controlled from my cell phone. The basic idea would be that I could send a password to a micro controller (via bluetooth) and if it was correct the microcontroller would tell a soleniod to flick open a door lock.

I am using the blueSMIRF and a atmega168 (with an 16 external crystal)

I now have the SMIRF connected to the atmega and can connect to the smirf with a bluetooth dongle from my PC. I am able to recieve information from the bluetooth when I use a feed back loop, but when I connect to the atmega I get garbage.

Any ideas why this is?

Here is the code I am using. Much of it is from the sparkfun tutorials:
Code: Select all

#include <stdio.h>
#include <avr/io.h>

#define FOSC 16000000
#define BAUD 9600
#define MYUBRR (((((FOSC * 10) / (16L * BAUD)) + 5) / 10) - 1) 

#define sbi(var, mask)   ((var) |= (uint8_t)(1 << mask))
#define cbi(var, mask)   ((var) &= (uint8_t)~(1 << mask))

#define STATUS_LED 0
//Define functions
void ioinit(void);      // initializes IO
static int uart_putchar(char c, FILE *stream);
uint8_t uart_getchar(void);

static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);

void delay_ms(uint16_t x); // general purpose delay

int main (void)
	uint8_t key_press;
    ioinit(); //Setup IO pins and defaults

		key_press = uart_getchar();
		if(key_press == 'g') sbi(PORTC, STATUS_LED);
		if(key_press == 'X') cbi(PORTC, STATUS_LED);

void ioinit (void)
    //1 = output, 0 = input
    DDRB = 0b11101111; //PB4 = MISO 
    DDRC = 0b11111111; //
    DDRD = 0b11111110; //PORTD (RX on PD0)

    //USART Baud rate: 9600
    UBRR0H = MYUBRR >> 8;
    UCSR0B = (1<<RXEN0)|(1<<TXEN0);
    stdout = &mystdout; //Required for printf init

static int uart_putchar(char c, FILE *stream)
    if (c == '\n') uart_putchar('\r', stream);
    loop_until_bit_is_set(UCSR0A, UDRE0);
    UDR0 = c;
    return 0;

uint8_t uart_getchar(void)
    while( !(UCSR0A & (1<<RXC0)) );

//General short delays
void delay_ms(uint16_t x)
  uint8_t y, z;
  for ( ; x > 0 ; x--){
    for ( y = 0 ; y < 80 ; y++){
      for ( z = 0 ; z < 40 ; z++){
        asm volatile ("nop");
Any Help would be greatly appreciated.

Posted: Fri Jul 10, 2009 4:40 am
by signal7
In any serial communication, garbage generally indicates a baud rate mis-match. The blueSmirf modules don't automatically detect what baud rate is being used, so it has to be set to a specific value. If your mcu is communicating at 9600 and the blueSmirf is still at the default 38400 it would explain the problem your experiencing.

When I changed the baud rate, I connected to the module via bluetooth from the pc at the default baud rate and entered $$$ within one minute from power up. That puts you in command mode and you can change the blueSmirf baud rate from there (the specific command for doing so is in the data sheet, but I don't recall what it is at the moment). Once the baud rate is changed, it should stay changed from that moment forward.

Posted: Fri Jul 10, 2009 9:20 am
by Tomatoeyes55
Thanks!!! That totally fixed the problem!