About: DIY projects

The aim of this PROJECT to control devices via wireless in a Bluetooth band . Now a days Android smart phone is a craze and many things can be controlled by Android through Bluetooth, WiFi. So, my motive is to make a communication system via smart phone to control Robot, home appliances,etc.

Here, I am using bluetooth (HC05) serial module for communication with AVR(Atmega32) micro-controller.

The principle task of this project was to design proper circuit and programming the Micro-controller to control my device via wireless. The communication protocols dealing with transmission and reception of data.

Bluetooth is a standardized protocol for sending and receiving data via a 2.4GHz wireless link. It’s a secure protocol, and it’s perfect for short-range, low-power, low-cost, wireless transmissions between electronic devices. These days Bluetooth is like wireless, and Bluetooth is a big part of that wireless communication. Variety of consumer products using Bluetooth, like headsets, video game controllers, or remote trackers.

Bluetooth serves as an excellent protocol for wireless transmitting relatively small amounts of data over a short range (<100m). It’s perfectly suited as a wireless replacement for serial communication interfaces. Or we can use it to create Computer Keyboard. or Home Appliances, wireless MP3-playing speaker , robots etc.

Step 1: Bluetooth Communication

Creating a Bluetooth connection between two devices is a multi-step process involving three progressive states:

1. Inquiry – If two Bluetooth devices know absolutely nothing about each other, one must run an inquiry to try to discover the other. One device sends out the inquiry request, and any device listening for such a request will respond with its address, and possibly its name and other information.

2. Paging (Connecting) – Paging is the process of forming a connection between two Bluetooth devices. Before this connection can be initiated, each device needs to know the address of the other (found in the inquiry process).

3. Connection – After a device has completed the paging process, it enters the connection state. While connected, a device can either be actively participating or it can be put into a low power sleep mode.

  • Active Mode – This is the regular connected mode, where the device is actively transmitting or receiving data.
  • Sniff Mode – This is a power-saving mode, where the device is less active. It’ll sleep and only listen for transmissions at a set interval (e.g. every 100ms).
  • Hold Mode – Hold mode is a temporary, power-saving mode where a device sleeps for a defined period and then returns back to active mode when that interval has passed. The master can command a slave device to hold.
  • Park Mode – Park is the deepest of sleep modes. A master can command a slave to “park”, and that slave will become inactive until the master tells it to wake back up.

Bonding and Pairing:

When two Bluetooth devices share a special affinity for each other, they can be bonded together. Bonded devices automatically establish a connection whenever they’re close enough. When I start up my car, for example, the phone in my pocket immediately connects to the car’s Bluetooth system because they share a bond. No UI interactions are required!

Bonds are created through one-time a process called pairing. When devices pair up, they share their addresses, names, and profiles, and usually store them in memory. The also share a common secret key, which allows them to bond whenever they’re together in the future.

Pairing usually requires an authentication process where a user must validate the connection between devices. The flow of the authentication process varies and usually depends on the interface capabilities of one device or the other. Sometimes pairing is a simple “Just Works” operation, where the click of a button is all it takes to pair (this is common for devices with no UI, like headsets). Other times pairing involves matching 6-digit numeric codes. Older, legacy (v2.0 and earlier), pairing processes involve the entering of a common PIN code on each device. The PIN code can range in length and complexity from four numbers (e.g. “0000” or “1234”) to a 16-character alphanumeric string.

Masters, Slaves, and Piconets:

Bluetooth networks (commonly referred to as piconets) use a master/slave model to control when and where devices can send data. In this model, a single master device can be connected to up to seven different slave devices. Any slave device in the piconet can only be connected to a single master.

The master coordinates communication throughout the piconet. It can send data to any of its slaves and request data from them as well. Slaves are only allowed to transmit to and receive from their master. They can’t talk to other slaves in the piconet.

Range is application specific and although a minimum range is mandated by the Core Specification, there is not a limit and manufacturers can tune their implementation to support the use case they are enabling.

Range may vary depending on class of radio used in an implementation:

  • Class 3 radios – have a range of up to 1 meter or 3 feet
  • Class 2 radios – most commonly found in mobile devices – have a range of 10 meters or 33 feet
  • Class 1 radios – used primarily in industrial use cases – have a range of 100 meters or 300 feet

Step 2: Bluetooth Communication Contd...

Device Usage

  • (almost) Stand-alone wireless connection – Needs external application to drive services

Device Communications

  • Client/Server (Master/Slave) configuration
  • Devices are inherently equal
  • Application determines identity

[[Client Application]<=>[HC05]]<=================>[[BT Device]<=>[Server Application]]

Software/Interface Stack

[Bluetooth Host] ------------------- •[ Bluetooth Host]

^ – Host Controller Interface driver

|| – Physical Bus driver


[Physical Bus]---------------------- • [Physical Bus]

^ – Physical Bus Firmware



[Bluetooth Hardware]------------- • [Bluetooth Hardware]

– Host Controller Interface firmware
– Baseband controller

Connecting to Device
• Host Controller Interface – Allows control interaction with Bluetooth hardware

• Transport layer – Physical connection between host and Bluetooth hardware

1. Host Controller Interface

• Uniform interface to access Bluetooth hardware capabilities

• Contains sets of commands for hardware

• Contains handle to possible events

• Contains access to error codes

2. Transport Options
• Standard describes three basic transport formats – USB Transport

• Universal Serial Bus – RS232 Transport, UART Transport

• Universal Asynchronous Receiver/Transmitter

• Serial interface – Can be set to RS232 settings

Step 3: Bluetooth Network

Bluetooth Networking:

• Piconet formed by up to 7 active Bluetooth devices

– Master/Slave configuration

– Additional slaves can be placed in a “parked” state Devices are not active but remain synchronized

– Connection, synchronization, parked/active controlled by master

– All devices connected in a piconet share timing and frequencies

• Scatternet formed by two or more Piconets

– One master per piconet but a master in one piconet can participate as a slave in a different piconet

– Slaves are time division multiplexed into more than one piconet

– Piconets not time or frequency synchronized

Establishing a connection

• Bluetooth units transmit inquiry message to find other Bluetooth units.

Master/Slave Piconet

• One unit becomes the master and the others slaves

• Master/slave relationship establishes timing

• A slave can become a master in another Piconet. This connects two Piconets into a Scatter Net.

Bluetooth Pico/Scatter Nets

• Master in one piconet can be a slave in another

• Addressing limits number of active devices in a piconet to 7

• An indefinite number of parked devices remain synchronized with the piconet but are not active.

Bluetooth technology operates in the unlicensed industrial, scientific and medical (ISM) band at 2.4 to 2.485 GHZ, using a spread spectrum hopping, full-duplex signal at a nominal rate of 1600 hops/sec. the 2.4 GHZ ISM band is available and unlicensed in most countries.

Step 4: Bluetooth Layers and Protocol Stack

  • Bluetooth standard has many protocols that are organized into different layers.
  • The layer structure of Bluetooth does not follow OS1 model, TCP/IP model or any other known model

The different layers and Bluetooth protocol architecture:(Fig1)

Bluetooth Frame Format:
The various fields of blue tooth frame format are:

1. Access Code: It is 72 bit field that contains synchronization bits. It identifies the master.
2. Header: This is 54-bit field. It contain 18 bit pattern that is repeated for 3 time.

The header field contains following subfields:
(i) Address: This 3 bit field can define upto seven slaves (1 to 7). If the address is zero, it is used for broadcast communication from primary to all secondaries.

(ii)Type: This 4 bit field identifies the type of data coming from upper layers.

(iii) F: This flow bit is used for flow control. When set to 1, it means the device is unable to receive more frames. (iv) A: This bit is used for acknowledgement.

(v) S: This bit contains a sequence number of the frame to detect re-transmission. As stop and wait protocol is used, one bit is sufficient.

(vi) Checksum: This 8 bit field contains checksum to detect errors in header.

3. Data: This field can be 0 to 2744 bits long. It contains data or control information coming from upper layers.

Step 5: Bluetooth Baseband Controller

Baseband Controller:

Baseband: baseband protocols and low level link routines

Link Manager: Link Layer messages for setup and link control

• Frequency Hop Time Division Duplex Channel
Channel based on a 625 msec time slot (1600 hop/sec) 220 msec of the slot lost to PLL settling

Bluetooth uses both circuit and packet switched channels, supports:

– Up to 3 simultaneous 64 kbps synchronous voice channels

– Simultaneous synchronous voice and asynchronous data channel

– Asynchronous data channel:

-/- 721/57.6 kbps asymmetric

-/- 432.6 kbps symmetric

Access Codes:

• Channel Access Code (CAC): Identifies a piconet, this code is used with all traffic exchanged on a piconet

• Device Access Code (DAC): Used for signaling, e.g. paging and response to paging

• Inquiry Access Code (IAC):

– General Inquiry Access Code (GIAC) Common to all Bluetooth devices

– Dedicated Inquiry Access Code (DIAC) Common to a class of Bluetooth devices

– Inquiry process “finds” BT devices in range

Step 6: Coding for Atmega32 MCU

We can send and receive data to/from android smart phone or PC:---

  1. /*This function is used to initialize the USART at a given UBRR value */
void BlueInit(uint16_t ubrr_value) { //Set Baud rate UBRRL = ubrr_value;

UBRRH = (ubrr_value>>8);

/* We Set Frame Format as
>> Asynchronous mode
>> No Parity
>> 1 StopBit
>> char size 8 */


//Enable The RX receiver and TX transmitter



2. /* function used to read data from USART line. It waits till any data is available */

char BlueRdChar()
{ while(!(UCSRA & (1<<RXC)));

UCSRA |= (1 << RXC);

//Return received data
return UDR;


3. /* function writes the character in 'data' into USART and then transmits it to PC via TX line */

void BlueWrChar(char data)
{ while(!(UCSRA & (1<<UDRE)));



/*###############main Function########################################*/

#include "lcd_io.h" #include "util/delay.h"

/* Extern declarations, prototypes etc. ... */
void BlueInit(); char BlueRdChar(); void BlueWrChar(char); void Waiting(int);

#define F_CPU 16000000UL
#define USART_BAUDRATE 11500UL //9600UL #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)

int main()
{ char data; int i; /*First Initialize the USART with baud rate = 9600bps for Baud rate = 9600bps UBRR value = 103 */ BlueInit(103); //UBRR = 103 //Initialize LCD module lcd_init(); lcd_clrscr(); lcd_gotoxy(0,0); lcd_puts("BLUETOOTH MODULE:"); Waiting(5); lcd_clrscr(); //Loop forever while(1) { lcd_clrscr(); lcd_puts("Receiving Data.."); for (i=0;i<=15;i++) { data=BlueRdChar(); BlueWrChar(data); lcd_gotoxy(i,1); lcd_puti(data,0); } Waiting(2); } return 0; }

Step 7: Android Code for Bluetooth Transmission

Bluetooth is a way to send or receive data between two different devices. Android platform includes support for the Bluetooth framework that allows a device to wirelessly exchange data with other Bluetooth devices.

Android provides Bluetooth API to perform these different operations:-

  • Scan for other Bluetooth devices
  • Get a list of paired devices
  • Connect to other devices through service discovery

Android provides BluetoothAdapter class to communicate with Bluetooth. Create an object of this calling by calling the static method getDefaultAdapter()

<p>private BluetoothAdapter BA;<br>BA = BluetoothAdapter.getDefaultAdapter();</p>

In order to enable the Bluetooth of your device, call the intent with the following Bluetooth constant ACTION_REQUEST_ENABLE.

<p>Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);<br>startActivityForResult(turnOn, 0);</p>

other constants provided the API , that supports different tasks:-

<ul><li>ACTION_REQUEST_DISCOVERABLE</ul>This constant is used for turn on discovering of bluetooth.<ul><li>ACTION_STATE_CHANGED</ul>This constant will notify that Bluetooth state has been changed.<ul><li>ACTION_FOUND</ul>This constant is used for receiving information about each device that is discovered.

Once you enable the Bluetooth , you can get a list of paired devices by calling getBondedDevices() method. It returns a set of bluetooth devices...

<p>private SetpairedDevices;<br>pairedDevices = BA.getBondedDevices();</p>


The Android platform supports Bluetooth connection, which allows exchanging data with other Bluetooth devices. The application framework provides access to the Bluetooth functionality through the Android Bluetooth APIs connect to other Bluetooth devices.

Bluetooth APIs classes are available in the Android Bluetooth package:--

<pre><p><strong>BluetoothAdapter</strong>:-- Represents the local Bluetooth adapter (Bluetooth radio).</p>
<p><strong>BluetoothDevice:</strong>-- Represents a remote Bluetooth device, query information such as its name, address, class, and bonding state.</p>
<p><strong>BluetoothSocket</strong>:-- Represents the interface for a Bluetooth socket (similar to a TCP Socket).</p>
<p><strong>BluetoothServerSocket</strong>:-- Represents an open server socket that listens for incoming requests (similar to a TCP ServerSocket).</p>
<p><strong>BluetoothClass</strong>:--Describes the general characteristics and capabilities of a Bluetooth device.</p>