Controlling an infrared helicopter with Arduino

So I’ve gone ahead and bought myself an infrared controlled helicopter in a local shop. At the time of writing, it’s Christmas so it’s kind of like a present I’ve got for myself. If you live in Belgium or the Netherlands, I’m talking about Action :-).

It looks like this and it retails for about € 15. However, buyer beware: I’m no expert but it flies like absolute *****. It does not really hover and despite my best efforts to calibrate it, it keeps on rotating.

On the bright side it uses infrared for it’s remote control. Since I’ve been teaching my students in one of my classes about infrared I’ve gotten up to speed on the basics. I was confident I could spoof the remote control commands with an IR LED and an Arduino. Turns out I was right.

Note: if your thinking about autonomous flight using this particular helicopter… move on to another model because in my opinion, it’s not really possible unless the helicopter can stabilize itself and remain in position. This helicopter simply can’t do that.

If you want to simply do a proof of concept then by all means, read on.

What you’ll need

  • An IR RC helicopter from Action
  • An Arduino (I’ve got a Leonardo)
  • An IR LED (38 kHz) and 220 Ohm resistor
  • A breadboard
  • Optional: an IR receiver

Deciphering the protocol

This step is optional and I’ve basically done all the work here. By using an IR receiver I’ve reverse engineered the commands sent by the real remote control. I’ve done this by using the IRLIB2’s rawRecv sample code, pressing buttons on the remote control and then looking at the data. This was a process that required some patience.

I’ve discovered that, per ‘command’, 36 ‘timing values’ are sent to the helicopter but only four of them are unique:

  • Approximately 1900
  • Approximately 315
  • Approximately 950
  • 1000

An intercepted ‘command’ looks, for example, like this:

1902 330
306 334 310 330 286 354 310 330
310 962 950 962
310 966 946 962
310 330
310 330 946 966 946 966
310 326 310 334
942 334 946 330
310 1000

Each value at approximately 315, we interpret as a ‘0’ and each value at approximately 950 we interpret as a 1. The values in red are regarded as a header and trailer and do not count as meaningful data.

The translated ‘command’ then looks like this:

1902 0
0 0 0 0 0 0 0 0
0 1 1 1
0 1 1 1
0 0
0 0 1 1 1 1
0 0 0 0
1 0 1 0
0 1000

The first 8 bits represent the throttle value. 00000000 is of course: 0 and 01111111 is the maximum at 127.

The next 4 bits represent the left / right value where 0111 is idle, 0000 is left and 1111 is right.

The next 4 bits represent the forward/backward value where 0111 is idle, 0000 is forward and 1111 is backward.

The next 2 bits represent the channel. 00 is A, 01 is B and 11 is C

The following bits contain calibration information but I have not really bothered with that. I’ve always left it at it’s defaults: 001111 0000

The last 4 bits is the checksum and this was a tough one for me to find. It’s basically the data grouped in pairs of 4 bits and then you XOR them together. The result is XOR’ed again with 0101.

So it’s like this:

0 0 0 0
0 0 0 0
0 1 1 1
0 1 1 1
0 0 0 0
1 1 1 1
0 0 0 0
0 1 0 1
 1 0 1 0

Now we have everything we need to spoof commands to the helicopter :-).

The schematics

Basically this is an Arduino with an IR LED and resistor. You connect an IR LED just the same as a normal LED.

The only caveat is that you should wire the LED to pin 9 on a Leonardo as this is different from the UNO.

The code

I’ve written a lot of comments in the code so it should be fairly self explanatory. Upload to the Arduino and you’re ready for takeoff.

 

Controlling an Arduino with a WiFi ESP8266 adapter using a Windows 10 Universal App

If you’ve followed my previous post ‘Controlling an Arduino with a Bluetooth HC-05 adapter using a Universal App‘ you’ll notice many similarities when reading the instructions below.

There are, however, some advantages of using a WiFi ESP8266 module instead of a Bluetooth HC05 module:

  • the wireless range is larger;
  • the amount of devices that use WiFi is greater.

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

What do you need?

  • some code in C# for the Windows 10 Universal App platform;
  • an Arduino. I’m using a Leonardo;
  • a cheap ESP8266-01 module and a FTDI cable.
Part 1: The Aduino / ESP8266-01

There are many tutorials out there that show you how to connect your Arduino to an ESP8266. For example: this one or this one so you’ll forgive me if I’m not going to explain how to do that. The basic schematic however is this:

Ideally you could place a voltage divider between the Arduino Tx and ESP8266-01 Rx so the input pin of the ESP8266 is not subjected to 5V.

You could also place a LED on the Breadboard somewhere and attach it to your Arduino. We’re going to use the built in LED on pin 13.

You’ll notice that we have connected the Arduino’s Tx to the ESP8266-01’s Rx and the Arduino’s Rx to the ESP8266-01’s Tx and we didn’t even bother to use the ESP8266-01’s GPIO. That’s because we will relay all the messages sent via TCP/IP from our computer to the ESP8266 via Serial to the Arduino.

First, connect your ESP8266-01 via the FTDI cable to your computer. Make sure it’s in bootloader mode.

Then you’ll need to configure your Arduino IDE to compile for the ESP8266-01 (and not for Uno or Leonardo or …).

You can check out the code below as a reference. It doesn’t do much except it connects your ESP8266-01 to your SSID and retries if the network was not available.

The ESP8266-01 then waits for someone to connect to it and sends it a message. You’ll notice we don’t use the GPIO pins from the ESP8266-01. Instead we just forward the message trough it’s serial pins to our Arduino.

Program for the ESP8266-01

Program for the Arduino:

You could try to connect to your ESP8266-01 with a program like Putty and make sure everything is OK. Sending the message LED ON should light up your LED and LED OFF should dim it.

Part 2: The Universal App for Windows 10

Now, all the above… It’s not the main focus of today. I want to show you how easy it is to use the power of the Windows 10 Universal App API to connect to an ESP8266 module (or any other WiFi client).

The class TCPClient is kind of a wrapper around StreamSocket. It connects to a host and provides us with Events we can consume in the GUI. You could even recycle most of it if you would like to use Bluetooth.

The consumer (our GUI) is really basic and just allows us to connect and send a message over TCP/IP using an IP adress and a port.

If you start the application you’ll notice you can connect and there are two buttons. Clicking LED ON should drive the LED high and clicking LED OFF should drive the LED low.

You can find the complete GITHub repository here.

In conclusion

I must admit I kind of have a love / hate relationship with those tiny ESP8266-01 modules…

They are very small and cheap but they have some disadvantages that you should be aware of.

ESP8266-01’s only have 2 GPIO pin’s but they need to be in a certain state when the ESP8266-01 is powered on or else you get into the bootloader mode. This makes your I/O more difficult. You could use ESP8266-12 instead.

If you want to use an Arduino to expand the IO pins then know that these modules require 3.3V. If you connect them to the 3.3V pin, the Arduino can barely supply enough power. Depending on what you’re planning to drive, the 3.3V supply from the Arduino is not enough and you should look for an alternative solution (like a voltage divider on the 5V pins or a 3.3V voltage regulator or an external power supply).

With all that being said I must tell you that I grew out of using the ESP8266-01. They are too much of a hassle for me. I’m now in the habit of going nuclear and just use a Raspberry PI 2 / 3 with Windows 10 IOT. It’s just easy to be able to remote debug over TCP/IP, have a GUI, NuGet packages, … They are worth the extra price for me.

If you need to have exact timing (certain sensors, a LED strip), you’ll need a microcontroller like the Arduino. I would suggest to just get the Arduino Uno WiFi.

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!

Reading out the Xbox One controller in a Universal App

One of the reasons I bought an Xbox instead of a PS4 is that Microsoft made a promise that every console could be unlocked for developers. Since I’m fairly familiar with C#, Visual Studio and the Windows ecosystem in general, the choice made sense.

Let’s see what you can get out of the Xbox One controller using the standard Universal App API’s. Seems it’s fairly simple and uses a minimum amount of code.

Note: unfortunately you cannot use the Xbox one controller directly on a Raspberry PI 2/ 3 with Windows 10 IOT since there is, at the moment of writing, no driver for it…

Create a background task that polls the controller and translates everything into an Event:

Registering events to consume them later:

Consume the event and show something in the GUI:

All of this is sort of like a proof of concept. It’s up to you to extend this now.

If you want to, you can check out the GitHub repository here.

Using WiFi RSSI to estimate distance to an Access Point

As I was discussing the problem of having a reliable WiFi indoor positioning systems with a colleague of mine, I decided to have a go at it myself.

RSSI is short for ‘Received Signal Strength Indication’. In a nutshell: the further away you move from an access point, the weaker the signal.

If you measure the strength of the signal, you have an indication of your distance relative to the source.

This has been done over and over and over again using WiFi, Bluetooth, insert some wireless standard here… So all in all not much of a challenge.

However: it hasn’t been done that many times using C# in a Universal App project. The advantage is that you can run your code on anything that boots Windows 10. Even mobile phones or Raspberry PI.

It turns out that you can have the RSSI measurement amazingly fast and with just a few lines of code.

Listing the adapters:

Iterating over the adapters and reading RSSI of every Access point, order by SSID:

Getting distance from RSSI value using a formula:

Consult the full GitHub repository here.

The formula to calculate the distance is pretty straightforward.

d = 10 ^ ((TxPower – RSSI) / (10 * n))

  • TxPower is the RSSI measured at 1m from a known AP. For example: -84 dB.
  • n is the propagation constant or path-loss exponent. For example: 2.7 to 4.3 (Free space has n = 2 for reference).
  • RSSI is the measured RSSI
  • d is the distance in meter

Be aware that this is a very basic attempt and should perhaps be considered more of a ‘proof of concept’ as to see how well you can estimate distance.

You could extend this by using triangulation so you can have an X, Y and Z coordinate instead of just a radius.