Hardware Set Up
You can always use a USB, WiFi, or Ethernet connection to get started, but let’s cover simple hook up of a Bluetooth device and an LED that we will turn on and off over Bluetooth using the Windows Remote Arduino library!
- Connect the power and ground rails on the breadboard to the 5V and GND pins, respectively, on the Arduino. Using color coded wires (red and black) will make it easy to keep track of the power connections.
- Plug your bluetooth device on the breadboard and connect the VCC and GND pins to the power and ground rails, respectively, on the breadboard.
- Connect the TX-0 pin on the bluetooth device to the RX pin on the Arduino. Similarly, connect the RX-1 pin on the bluetooth device to the TX pin on the Arduino.
- Notice the yellow wire in the image goes from the transmit pin of the bluetooth device to the receive pin of the Arduino and vice versa for the orange wire. This step is critical to establish serial communication between the bluetooth device and the Arduino, allowing the messages transmitted from one device to be received by the other.
- Make sure that your code is already uploaded on the Arduino before making this connection. The Arduino Uno uses the same serial (TX and RX) pins for flashing the device, which prevents any code from being uploaded to it when another device is connected to these serial pins.
- Add an LED to the breadboard. Note that the longer (or bent) leg is the anode (positive) and the shorter leg is the cathode (negative).
- Connect the cathode of the LED to the ground rail of the breadboard using a 330Ω resistor. A 330Ω resistor is striped orange, orange, brown, gold as shown in the image.
- Connect the anode of the LED to any digital I/O pin on the Arduino. We are using pin 13 in the example.
- You setup is now ready! It should look similar to the setup shown in the image below.
Arduino Sketch (Firmata)
Install Arduio Firmata Library:
Goto Sketch menu / Include Library / Manage Library and search for “Firmata” and install latest version of the library.
Now open & upload “StandardFirmata” example from File / Examples / Firmata / StandardFirmata.
It’s done at Arduino side. Now we will see for Windows Universal Platform App.
Windows Universal Platform App
Now that we’re all set up, let’s get into some code!
- Create your project
I’ve set up a project called RemoteBlinky by following the steps in the setup guide. In the screenshot below, you will see the code-behind file MainPage.xaml.cs which simply creates a Bluetooth connection object and passes it to the RemoteDevice class in the constructor. You’ll see that I’ve specified my device name in this example. You may also enumerate the available devices by invoking the static
.listAvailableDevicesAsync() function on BluetoothSerial (and USBSerial) class before constructing your object.
Note for USB:
USBSerial has many options available to specify your device. In the constructor, you can provide the VID and PID of your device, the VID only, or a
DeviceInformation object (obtained from the above mentioned
listAvailableDevicesAsync function). Similarly,
BluetoothSerial allows you to provide a device id (as a string), device name (also a string), or the
You can obtain the VID & PID combination of your USB device by following these steps:
- Open Device Manager through the Control Panel or by pressing bothWindows + Pause keys and choosing the Device Manager link on the left.
- Expand the Ports (COM & LPT) menu
- Right-click your Arduino Device and select Properties
- On the Details tab, select Hardware Ids from the drop-down menu.
- You may see multiple entries in the Value box, but any entries will have matching PID and VID.
- The entries will have the format “USB\VID_****&PID_****” where **** are the numeric ID values.
USBSerial usb = new USBSerial( "VID_2341", "PID_0043" ); is guaranteed to work only for the following hardware device:
Next, I’m going to add a callback function to the DeviceReady event on the RemoteDevice object. This function will automatically be called when the Bluetooth device is connected and all necessary settings have been initialized. You’ll notice that I haven’t implemented anything in that function at this time. Last, call
.begin() on the connection object to tell it to connect.
Note for USB:
The parameters to the
.begin() function do not matter for Bluetooth, but you must use the same baud rate on both the Arduino and the UsbSerial object (first parameter). Also, the 2nd parameter must be “
SerialConfig.8N1". The rest of the example will work exactly the same regardless of which connection type you are using.
- Jump over to the MainPage.xaml file and create a couple buttons that will turn an LED on and off. You’ll notice I’ve added button callbacks to the
Clickevent & set the
IsEnabledproperty to false, and you’ll see why in the next step!
- I’ve implemented three functions in this step. First, the
OnDeviceReadyfunction now enables the buttons on the UI thread! This guarantees that the buttons will be enabled only when the Bluetooth connection is ready, as it typically takes a few seconds for this to happen.
- I’ve also set up the
.digitalWrite()calls in the button callbacks
- Build and deploy! Your buttons will be enabled when the connection is established, and you can freely toggle your LED on and off at will! Here is a screenshot of this basic example running on Windows Phone 10.
I really hope you enjoy replicating this project and using it as a baseline for an incredible new set of Maker projects!