Controlling an Arduino with a Bluetooth HC05 adapter using a Universal App

Today we’re going to combine three of my favorite things in the world:

  • some code in C# for the Windows 10 Universal App platform;
  • an Arduino;
  • cheap wireless transmission like the Bluetooth HC-05 module.

The goal is to communicate using Bluetooth from a phone, computer, … with an Arduino equipped with a HC-05 module and drive a LED. A simple and fun proof of concept.

The HC05 is a cheap Bluetooth module that you can use to extend the wireless capacity of your Arduino. I’m not going to go into the basics of this module as there’s an excellent ‘how-to’ available on this page.

You can get a HC-05 module from sites like AliExpress or Banggood for 3$. If you want it faster or want to support your country’s economy you can probably buy it at a local reseller.

The HC-05 has 6 pins but only four of them really matter for our purposes of today: Vcc (typically 5V), GND (0V), Txd (transmit) and Rxd (receive).

It seems quite obvious that we’re going to communicate via the serial protocol from our Arduino to this module and back.

Connect your Arduino’s Tx to the HC-05’s Rx and connect your Arduino’s Rx to the HC-05’s Tx. Yes, it needs to be crossed.

Connect a LED to an output pin of your choice. Note that I have used an Arduino Leonardo instead of an Uno. Probably should not make too much difference.

We do not need to worry about the power supply. The HC-05 is 5V tolerant and the Arduino can supply more than enough power to the module since we’re only driving a LED.

Load this code up into your Arduino:

You could already go ahead and test this out by opening your Serial monitor. If you type in LED ON, the LED should start to light up.

The second part of this project is to use a computer (or smartphone) to drive this LED. We’re using the Windows 10 Univeral App platform so we’re striking two birds with one stone. Offcourse you’ll need a computer with a Bluetooth connection or a smartphone running Windows 10 Mobile.

I’ve made a class BluetoothClient that’s a wrapper for StreamSocket. It uses the Singleton design pattern so a consumer can only create one of them. For our conveniance, events are created so we are notified when something happens.

Only thing left now is a GUI to consume the class. As you’ll notice it’s pretty straightforward.

Don’t forget to add Bluetooth capabilities in the application manifest file.

The only thing now is to start up the application and fill in the MAC address of your HC-05 module. You can leave Address on 1. If you connect and click on LED On, you should be seeing your LED in action on the Arduino.

 

You can find the GIT repository here. Have fun!

Leave a Reply

Your email address will not be published. Required fields are marked *