How to Bluetooth

This is a guide to using the RedBear Ble Mini for Bluetooth communication using a PIC.

Overview

Bluetooth uses a master/slave relationship, with the master device beginning the connection. Once connected, the slave works the same as a normal UART connection, and the master only requires a little extra code.

 

A few notes to start

  1. Make sure you have set the baud rate in config.h to 57600
  2. Make sure you have the correct UART pins in pt_cornell
  3. The slave device does not need any control from the PIC to run, it can be useful to hook it directly to realterm to check that it is receiving properly
  4. Make sure the transmission and reception packet size is set to the correct number of byte
    1. Make your send buffer the exact size of the packet you want to send
    2. Set max_chars in pt_cornell (in UART setup around line 616) to this same size
  5. If the slaves are not still neatly color coded, check the section below on how to determine a MAC address.
  6. The led on the slave will be on when it is powered, on the master it will blink when reset, but will be off normally.
  7. If you cannot figure out which are the master, check the section below on checking the firmware.

Example Code

Here is the code I wrote when testing the Bluetooth chips.

master_example

slave_example

Steps to set up Bluetooth

I would begin with just the master device, so download the master_test code above, and wire the master as indicated in the table below (Pinout for the redbear chip). There are a number of MAC addresses at the top of master_test.c, make sure that you are using the address of the device you are connecting to. Connect the slave device to power and ground, and connect its tx pin to the rx pin of a usb adapter to realterm (make sure to connect to the correct port/baud rate). When you run the master code, after a few seconds you should begin to see text appearing in realterm. If this does not happen, connect the rx pin of the usb to the rx pin on the master to make sure the PIC is actually transmitting data.

Pin on Redbear Mini Pin on PIC
Vcc Vcc
GND GND
Tx Pin 17 (or set uart rx in pt_cornell)
Rx Pin 21 (or set uart tx in pt_cornell)
P0_0 Unconnected
Reset Pin 26 (or any digital out)

Once you have verified the slave is receiving, you can connect it to a PIC. The pinout is similar to the master, except the tx goes to pin 18, and the reset goes to pin 12, but again these are configurable. The screen is connected as documented in previous labs. The slave_test code is designed to parse through the received UART and display it on the screen.

Ensuring Connection Between Devices

It can be difficult to make sure the master and slave are connected, because there is no way to debug what the Bluetooth chips themselves are doing. Make sure the reset pins on the master and slave are connected to the PIC, and are being pulsed high during init. Resetting the PIC without this won’t reset the Bluetooth chips, and can lead to problems. If you are having connection issues, or if you are reprogramming or are turning either device back on for some reason, I would advise resetting both devices.

Always reset the slave device before the resetting master device.

My theory as to why these connection issues arise is that the Bluetooth chips, is not reset properly, think that they are connected to devices that they are no longer connected to. For example, if you reset the master but not the slave, the slave device may think it is still connected, making the next connection attempt fail. This is why resetting the slave device first is beneficial.

Master

In our implementation, the master connects to each slave device using a specific master address. Once connected, the master addresses slave with a connection handler in the order they were connected (0, 1, 2). The master can connect to at most three slaves at once. The extra Bluetooth files add a header to the packets you are sending.

Slave

The slave requires no Bluetooth code on the PIC to run, it will work on its own. Once connected, it works like a normal UART line.

How to find a slave MAC address

If the devices are no longer neatly color coded, you will need to determine which MAC is associated with each chip. To do this, find an arduino (one of the professors will have one you can borrow) and download the Arduino Example Code. Connect the master device as described in the code, and simply connect the slave device to power and ground. Run the example BLE_HCI_BiscuitCentral, and open the Serial Monitor in the Arduino IDE. Type ‘d’ into the serial monitor to begin device discovery. You should see the device information for any nearby Bluetooth devices appear in the serial monitor. There are usually some random devices, but the one you want will have a MAC address starting with “B4:99:4C:7B.” Note: the MAC address will appear here backwards from how it is entered in the C code.

Checking the firmware on the chip

Press and hold the push button on BLE Mini while connecting it to your PC, it should show a mass storage drive (e.g. E:). It will contain a file called DEFAULT.CFG and a .bin file.

Biscuit-UART_20140409.bin -> slave

HCI_UART_57600bps_20130502 -> master

For more information check the redbear mini page. If you need to change the firmware for any reason, it can also be found on the redbear page. NOTE: when you load new firmware onto the chip, it will disconnect. This is normal.