A few month ago I bought a little rover (controlled by an Arduino Uno) for a very good price. The kit was very complete: Car chassis, 2 Car Wheels, 2 DC Gear Motors, a UNO R3, a L298N Dual H-Bridge Motor Controller and several other components.
This rover is meant to be programmed for autonomous operation. Hence a ultrasonic sensor and a servo is also added in the kit. Also a nice Arduino sensor shield 5 is in the kit. Yes, it was a real bargain ;-)
But my idea was to use both an Xbox One Controller and the Firmata protocol to drive this one by myself or one of my sons. And it works very good!
Here is a video of the final solution:
The main parts used in this project are:
- A rover kit (we use for this demonstration only a subset of the parts: the base plate, the wheels, the motors, the Arduino Uno, the 9 volts battery holder and the L298N Dual H-Bridge Motor Controller)
- An extra battery holder for 6 AA batteries
- A Bluetooth module (HC-06)
- An Xbox One controller with wireless adapter for windows (to connect it to a laptop)
- A laptop (with Windows 10 + VS2015 + Bluetooth dongle)
- A Flashing light for dramatic effect only
Building the kit
Building the kit is not that hard. Although the construction manual was in Chinese, it all seems pretty logical. There is only one base plate so I had to put some components on top of it and some at the bottom (now the motor controller):
In this picture two different battery holders are added. During programming I found out that I had to use a separate power supply just to get the motors running.
Connecting the motors – pin layout of controller
The hart of the hardware this time, is not the Arduino Uno but the motor controller.
“The L298 is a dual H-bridge driver for DC brushed motors and stepper motors. It supports a wide operating voltage range and can deliver 2 A per channel in a through-hole package that is accessible for do-it-yourself projects.”
This controller will control the speed and direction of each of the two motors, using signals coming from the Arduino.
The pin layout of the L298N Dual H-Bridge Motor Controller is:
- Plus + of DC motor 1
- Minus – of DC motor 1
- Power IN. I supply 9 volts from a separate battery holder
- Common ground. Connected to both my separate battery holder and the Arduino
- Power out. Can produce 5 volts (not used. My Arduino gets power from another battery holder)
- ENA connected to Arduino D10. This port is capable of PWM (white)
- IN1 connected to Arduino D9. (gray)
- IN2 connected to Arduino D8. (purple)
- IN3 connected to Arduino D7. (blue)
- IN4 connected to Arduino D6. (green)
- ENB connected to Arduino D5. This port is capable of PWM (yellow)
- Plus + of DC motor 2
- Minus – of DC motor 2
- The jumper next to 2 and 3 is NOT removed because I do not exceed the input power over 12 volts (up to 35 volts) (jumper not marked in the picture)
Note: the controller has it’s own power supply. The Arduino has one too. To keeps things running (without blowing thins up, give them a common ground. See bullet 4 above)
Connecting the motors – pin layout of Arduino
Connecting the Arduino is pretty straight-forward. We connect the ground and the 5 volts of the power supply to the Arduino. And we connect the six lines (ENA, IN1-4 and ENB) to pin D10 till D5.
It must be clear that port 7 and 8 (and 9 and 10 for the other motor) are just regular GPIO port. These will be used for the direction of the motor. If both ports (eg. 7 and 8) are LOW, the motor will do nothing (it stops). If one is HIGH and the other LOW the motor will run in one direction. If connected the other way around (the first one is set to LOW and the other to HIGH) the connected motor will run in the other direction.
But.. just setting these pins will result in nothing. No moving parts yet!
The magic will come from pin 5 and 10. These are ‘special’ pins which can generate a PWM signal. Setting a value between 0 and 255 will result in running the motor VERY slow (stopped) or at high speed.
Note: Each port on the Arduino Uno capable of PWM are marked with a tilde (the ~).
Connecting the Bluetooth
The Bluetooth module I use only needs to be connected to the RX and TX port (cross the lines) and it needs 5 volt power and ground from the Arduino.
Firmata sketch on the Arduino
The Firmata sketch “StandardFirmata” is all we need on the Arduino! Just get it from the Arduino IDE examples and upload it (maybe you need to unpin the TX/RX first to complete the upload).
Note: due to the lack of quality of my Bluetooth module, I always lower the baudrate which is hardcoded inside the sketch, and set it to 9600.
Gentlemen, start your engines
Or, test the connection…
So why am I using Firmata? because it’s easy. How easy? Very easy. And it can even be done without programming. Just start the Windows Remote Arduino Experience app (available in the store and also works on a Windows 10 Mobile device).
First you will have to connect to the already paired HC-6 Bluetooth module.
When paired, go to the PWM page. Enable digital pin 5 and give it a value of, say, 128.
Beware: the next step will make your motor running. Helmet on and wheels off the flour. You will know why when you see it.
Then go to the Digital page. And flip the switch of digital pin 6.
Now, if everything is connected and running, one of the motors will spin!
If so, you can check out other speeds (using a higher or lower PWM value) or change the direction (set digital pin 6 to 0 volt and digital pin 7 to 5 volt).
And there you have it. You can control that wheel!
But there is more, the same goes for pin 10 (PWM) and digital pin 9 and digital pin 8.
Both wheels running. Now start coding…
The UWP app as the match maker
It must be clear we need an new UWP app between the Xbox One controller and the rover.
I already blogged about using the Xbox One controller here. This time we will combine that knowledge with out rover.
Let’s look at the interface of our UWP app:
It’s pretty dull, two buttons and a text block. I will have nothing more to do then first connecting to the Arduino using Firmata. And once the connection is made, the controller button will have to start one giant loop to read the controller input and change it into useful commands.
And what we want to create is this classics tank controls with two handles. All we need is to check the two thumbsticks on the Xbox One controller. Moving them forwards and backwards will also start the corresponding motor forwards and backwards:
Note: If you start a new UWP app, do not forget to add the Bluetooth capability. And you will have to install the nuget package for Firmata.
First we add the XAML snippet (see code section for Xaml source code) in the mainform grid so we have some buttons.
Next we add the code behind (see code section for C# source code) of the main form. I basically is devided into two parts. First we make a connection over Bluetooth with the Firmata protocol. Next we start listening for input on the Xbox One controller.
Within the loop for checking the controls, we decide if a thumbstick is pointing forwards or backwards or is in the ‘stop’ range. After that we decide what the value will be for the PWM pin.
It’s interesting to see that writing a PWM value is represented in Firmata as writing an analog value to a digital port. There is no special PWM method in the Arduino class.
I have added two methods “ArduinoDigitalWrite” and “ArduinoAnalogWrite” to prevent writing the same value over and over to the Arduino. This will clutter up the communication and degrade the performance of the Arduino. (In a much richer design I added a buzzer. Without ignoring duplicate commands the performance of the buzzer was extremely poor and terrible to hear).
When the motors get the low PMW pulses, the motor is not directly starting to run, it will whine with a very distinctive sound. This is normal behavior. And that’s how I found out I had to add two power sources. First time I connected everything, I thought the controller was broken. Nothing happened. Until I unplugged one of the motors and the other started to whine.
So that’s how it works:
Footnote: the flashing light is not only for dramatic effect. Whenever your creations start moving, please be aware of safety. The contraption is dumb, you are not! Therefor I had put this light on top of it in the first place.
Comments