Home > uC and Electronics > LED Display Over Bluetooth With Android

LED Display Over Bluetooth With Android

Old Clock Redux

bluetooth moduleI came across this neat bluetooth module called HC-06. It can be found as cheap as $6 on ebay, and gives you convenient UART access to bluetooth wireless capabilities.

My 3yr old has this home-made LED clock in his room. I thought it was time for an upgrade since the clock was very minimalistic. Its only function was to keep track of time and display it on the LED panel. And considering it was my first “completed” project with avr (atmega168), I thought I could make it better this time around.

I wanted to make use of bluetooth capability somehow, and thought it would be neat to send a text message to the display. In addition, I decided change up a few things:

  • receive bluetooth text message up to 64 chars and scroll it across the panel
  • set the time via bluetooth
  • consolidate 2 push buttons into 1 for manual time adjustment
  • make some noise with the piezo I put in there
The upgrade seems to have worked out nicely. I can send messages to it from my Android phone or from a Linux PC with bluetooth dongle attached. I used iteadstudio’s bluetooth assistant app to send text to the display.
A Simple Demonstration
Again, the source code is available at:

Setting Up

The HC-06 module needs a bit of setting up before it can be used. On the internet, usually there are two variants of this module: HC-05 and HC-06. AFAIK, the hardware is identical across both modules, but the firmware is not. The main difference is that HC-05 is slave-only, whereas HC-06 can act as both master and slave.

These modules support AT commands which can be used to configure the bluetooth hardware. For example, you can change the passcode used in pairing (default: 1234) or change the baud rate. In order to issue the AT commands against it, you will need to put the chip into CMD mode. The easiest way to interface HC-06 chip is through FTDI over USB. If you own an Arduino or an FTDI breakout, you will need to make the corresponding TX/RX connections from the FTDI chip to HC-06 in order to be able to talk to it. I bought the Arduino bluetooth shield from iteadstudio, which conveniently makes all the pins accessible.

You will need to attach to the virtual device on Linux that represents the FTDI chip. On Ubuntu, that’s usually /dev/ttyUSB0. I use “minicom” for serial comm:

minicom -D /dev/ttyUSB0 -b 38400

But it’s usually good to just run

minicom -s

and configure the serial port manually. Make sure both the software and the hardware flow control is turned off.

Once the connection is made, verify that HC-06 module can respond to the commands. As soon as you type

AT

it should repeatedly send back:

OK
OK
OK
...

Here is the bare minimum AT commands I had to issue to configure the HC-06 module for my setup:

# set as slave
AT+ROLE=0
# set uart baud rate with stopbit, no parity
AT+UART=38400,1,0
# connect to any address
AT+CMODE=1

Once the module is configured, it needs to be reset into “DAT” mode. This is the mode that can send/receive data from master nodes that are paired up against it. Remember that the “CMD” mode is only used for issuing AT commands for modifying the config on the chip.

A Bit About the Hardware & Firmware

The LED panel is relatively cheap, and is sold by Sure Electronics. Its dimension is 8×32, which makes 256 individual LED’s that can be manipulated.  These panels contain a driver chip, HT1632, which makes every LED on the panel addressible with just 3 pins. The source file ht1632.[hc] contains the logic to communicate with the driver chip, and supports rudimentary functions to draw, print limited set of fonts, and scroll text across the panel.

I am a total noob when it comes to PCB design, soldering, and circuitry. The mini breadboard serves me well here. I don’t need to keep highly accurate time, and don’t mind a minute or two drift per year benchmarked against the “true” time. There are a few electrical properties that creates this time deviation. But simply put, this “small” time drift can happen when the oscillations produced by the 32.678kHz crystal is affected by things like inherent capacitance (more like parasitic) in the breadboard, temperature, overall integrity of the circuitry, etc… In any case, I am not overly concerned about accuracy, and this toy is not designed to keep military-grade time!

Only few parts are needed to build the necessary circuit to recreate this:

  • 1 atmega168
  • 1 HC-06 module (possibly on breakout board)
  • 1 32.678kHz crystal oscillator
  • 1 push button
  • 1 breadboard & couple of jumper wires
  • 1 piezo buzzer
  • few discrete components: 2 resistors, 1 capacitor
  • Lego blocks
And lastly, the firmware. The main source “clockdisplay.c” implements time keeping and processing data received via UART. It keeps track of hours, minutes, and seconds via software using interrupts. The interrupt is triggered each second, and 32.678kHz is chosen precisely for this reason. It has a frequency that is exactly 2^15. Therefore, the hardware counters are much more conducive to scaling by factors of 2. For example, the hardware registers are configured so that the ticks are generated exactly at multiples of 2. The function below sets up atmega168′s timer counters so that the interrupt is generated exactly 1 second apart.
void rtc_init(void)
{
  TCCR2A = 0x02;//clear timer on compare match
  // TCCR2B = (1<  TCCR2B = 0x07; // prescale by 1024, gives 32 ticks
  TIFR2  = 0x02;//clear the interrupt flag
  TIMSK2 = 0x02;//enable timer2A output compare match interrupt
  OCR2A  = 31;  //Set the output compare to 31 since the count starts at 0 - this gives 32 interrups per sec
  ASSR   = 0x20;//enable asynchronous mode, disable this line if you are using the 32khz crystal as a the system clock source
}
Within the interrupt routine, the following function is called to update the global volatile variables that keeps track of hours, minutes, seconds:
void update_datetime(uint8_t noincrement)
{
  if(!noincrement)
    seconds++;

  if(seconds >= 60)
  {
    seconds = 0;
    minutes++;
  }
  if(minutes >= 60)
  {
    minutes = 0;
    hours++;
  }
  if(hours >= 24)
  {
    hours = 0;
    days++;
    if( (days == 32) ||
      (days == 31 && months == (4|6|9|11)) ||
      (days == 30 && months == 2 && leap_year != 0) ||
      (days == 29 && months == 2 && leap_year == 0) )
    {
      days = 1;
      months++;
      if(months == 13)
      {
        months = 1;
        years++;
        if( (years % 400 == 0) ||
          (years % 4 == 0 && years % 100 != 0) )
        {
          leap_year = 1;
        } else {
          leap_year = 0;
        }
      }
    }
  }
}
The interrupt is generated from this auxiliary clock source (crystal oscillator), but what about the execution of the main code block? I use the internal clock on Atmega168 to run the main code, the part that processes UART data, displaying text on LED panel, etc… The mcu is configured to run at 8MHz and this requires fuse settings to be modified. You can run this command to change the fuse bits. (WARN! misuse of this command can brick the chip) Modifying the fuse bits can be done using AVR ISP programmers. I used a cheap clone.


avrdude -F -p m168 -P usb -c avrispmkII -U lfuse:w:0xE2:m

Adding Bluetooth Capability
Here is the clock going through surgery (hopefully for fine enhancements!). Luckily, I haven’t used up the RXD/TXD pins used by Atmega168′s hardware UART (PD0, PD1). The HC-06 bluetooth module will communicate bi-directionally with the host mcu using only these 2 pins.

The UART interface is a standard serial protocol that relies on baudrate. As such, the RXD/TXD can be connected to other devices that can “talk” UART and will be able to communicate with the mcu. In fact, you can hook up the FTDI chip directly in lieu of HC-06 and manipulate this LED clock directly from a terminal. Any other wireless modules that use the UART serial communication could probably replace HC-06.

That’s it! The hardware setup is simple and rest of the communication is then managed by the software on the Android app. The firmware simply accepts characters from the uart library, acts on the command being sent, and then echo’s back some confirmation characters. The uart library for atmega168 is also very simple, and it’s the one I have been using with the nerdkit when I first got into mcu’s.

About these ads
Categories: uC and Electronics
  1. Tom
    February 7, 2012 at 6:56 pm | #1

    Hi,
    I’m trying to use the Bluetooth shield from itead to be the bluetooth interface between my mac and a roomba. As I visualize it I would pass the rx/tx stuff to the roomba command interface. However, I haven’t yet been able to figure out how to capture those signals — how much current can they supply, etc. I’m experienced on software but new to hardware.
    Any ideas or leads?

    • February 14, 2012 at 3:25 am | #2

      I haven’t worked with roomba before, so I can’t really say much about it. The itead bluetooth shield uses by default hardware UART on arduino. So if roomba also needs to be communicated via UART, you’ll need to use software serial library. You can find out Arduino’s I/O current limits here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1269902243/all
      However, serial communications don’t need that much current.

  2. August 20, 2012 at 11:16 pm | #3

    These are great devices, though you may also need to take into account latency if your application requires it:
    Check out the tests I did on this device here:

    http://adriangin.wordpress.com/2012/08/21/bluetooth-uart/

    • January 11, 2013 at 1:07 am | #4

      Great, thanks for that link. Yes, I did notice the latency. Especially when you try to communicate from prior inactivity. The firmware in the CRS chip on that module probably implements device sleep when there’s no bluetooth activity. I suspect waking up from that sleep introduces much latency.

  3. Radek
    September 27, 2012 at 1:43 pm | #5

    Hi.
    We are using the HC-05 module together with our Android application. Unfortunatelly we are experiencing really weird problems because sometimes (not every time) the connection fails with “Service discovery failed” error message. Although there are dozens of questions about this error message (e. g. http://stackoverflow.com/questions/8515572/service-discovery-failed-from-android-bluetooth-insecure-rfcomm), our problem is something different since it occurs only on some conditions. I susspect it has something to do with Bluetooth communication channel selection becase its more frequent when more Bluetooth devices is enabled around and sometimes it is solved by our device hard reset. Have you experienced similar problems with your HC-06 module and your Android application?

    • January 11, 2013 at 1:11 am | #6

      Apologies for the late response. I haven’t had issues yet, but I’ve seen “Service discovery failed” when I use these functions directly:
      bluetoothSocket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(uuid);
      // or
      bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);

      However, you may want to try something like:

      method = bluetoothDevice.getClass().getMethod(“createInsecureRfcommSocket”, new Class[] { int.class });
      // or
      method = bluetoothDevice.getClass().getMethod(“createRfcommSocket”, new Class[] { int.class });
      bluetoothSocket = (BluetoothSocket) method.invoke(bluetoothDevice, 1);

      I had issues with newer android phones when I tried to use the prior connection routines compared to the latter one.

  4. October 17, 2012 at 4:38 am | #7

    You may have it the wrong way round HC06 is slave only and HC05 is slave/master.

    • January 11, 2013 at 1:11 am | #8

      You are entirely correct! HC05 is the lower numbered version that’s both slave and master.

  5. Ankita Bhat
    March 30, 2013 at 4:14 pm | #9

    Hi,
    I am using HC-05.I tried to configure the chip using AT commands at the hyperterminal but the chip is not giving any response.I also tried using the TTL to RS232 convertor and DB9 male connector,Regulated the supply to 3.3V and supplied to the chip but the chip didnot respond to the AT commands.Kindly help.

  6. twins
    March 31, 2013 at 2:46 am | #10

    Hi,
    We are using HC-05 low cost bluetooth module for our project but finding difficult in configuring the module at the hyperterminal. its not giving any response for the AT commands to change name ,password etc.we tried using the TTL to RS232 convertor also but it dint work out.the module is getting detected and paired by its default password and name.Please help

    • March 31, 2013 at 3:39 pm | #11

      It would help if you can show me how you wired the module. What baud rate are you using? By default, these modules ship with 9600 baudrate I believe.

  1. February 7, 2012 at 8:20 pm | #1
  2. September 26, 2012 at 3:26 am | #2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: