Tags:
create new tag
view all tags

Remotely controlling the Robot through Bluetooth

In order to control the robot remotely some RF network connection is needed to send movement commands to the robot. The idea is to connect a joy stick to an Arduino Nano equipped with an HC-05 Bluetooth module that can be programmed as a Bluetooth master. The robot itself is equipped with an HC06 Bluetooth slave module. Depending on the readout of the joy stick, commands will be sent to the Arduino controlling the robot, which makes the robot drive forward, backward, left of right and with different speeds.

The HC-06

The HC-06 module is a serial transceiver module that is interfaced by a serial port with 3.3V signal level on one side and the Bluetooth RF on the other. In our case we will connect the HC-06 through a serial port to the Arduino and communicate with the remote controller through Bluetooth. This Bluetooth module allows to send ASCII text from the Bluetooth master to its client and back. Instead of being connected through a serial cable we are connected through the Bluetooth RF signal.

This is a photo of the HC-06:

hc-06.png

The 4 pins are connected to Vcc: 5V but also works on 3.3V, GND, Rx and Tx (both Rx and Tx should be of 3.3V level)

Testing the Bluetooth connection

Instead of trying to immediately remotely control the robot's motors we will, as a proof of principle, try to switch a LED on Arduino on and off.

In order to test everything my strategy is as follows: First I test the RF side. In order to do this I simply short circuit Rx and Tx on the HC-06. In this case everything that is written to the HC-06 via the RF side will immediately be sent back. Of course this only works once the Bluetooth controller on the PC is paired and connected with the HC-06. Finding the MAC address of the HC-06, pairing it and connecting it to the PC I do with bluetoothctl, a program I installed on my Ubuntu PC.

When powering the HC-06 and before connection, the LED on the HC-06 flashes at a frequency of ~ 2 Hz indicating that the module is powered and ready for connection.

bluetoothctl.png

The above screen shot shows the commands I used.

Finally I have to bind the HC-06 to /dev/rfcomm0 which I do with this command:

rfcomm bind -i 00:21:13:00:B8:A3

which creates a new device: /dev/rfcomm0. Now I start minicom, a terminal for serial ports. Everything I type will be sent to the serial device minicom is connected to and everything the serial device sends will be displayed on its screen. There are other programs like cute cutecom or others which works in a similar fashion and can be used instead of minicom. So... I start minicom with minicom -D /dev/rfcomm0.

Now anything I type, I will see on the screen. This seems ridiculous but in fact the characters pass through the HC-06 and if you take away the short circuit between Rx and Tx on the HC-06 then you can type whatever you want, no character will appear. This test tells me that the RF part is working fine. In fact you can choose any baudrate on minicom, since we pass through sockets to arrive at the HC-06 RF side this setting is not used and it work for any setting.

One more comment: When the HC-06 is connected then I do NOT see the LED steadily on, as described in several posts on the Internet, but I see it off for ~ 2s followed by 2 short flashes.

I had come this far before, except for the short circuiting bit. Now I know at least that one side of the HC-06 is fine (at that seems to be the more tricky bit). What still needs to be tested is the serial connection part. I had the HC-06 connected to the Arduino and I had written a sketch sending "Hello World!" once per second. In fact I saw characters arriving in minicom every second but it was only unreadable garbage. I had set up the serial baud rate to 9600 baud, which, a cording to the data sheet.

Replacing the Arduino by a USB to Serial Adapter

Instead of using the Arduino serial port I decided to try with this USB to serial adapter, which I connect to my PC. Therefore the data flow through USB, are converted to serial, pass on to the HC-11 and return to the PC vie the Bluetooth RF. Again I wrote a "Hello World" program sending the text to minicom at 9600 baud through the path described and again I only see rubbish (the helloWorld.c program is attached).

usb2ttlFront.png usb2ttlBack.png
USB to TTL adapter, front view Back view
Since something is arriving in minicom, I decided that the HC-06 is wrongly programmed and the default baud rate is NOT 9600 baud as written in the data sheet. I wrote a program that steps through several baudrate settings and send a text identifying the baud rate currently selected. What I see is this:

findBaudrate.png

You see the rubbish characters but a correct text when the selected baud rate is 38400 (and NOT 9600 as written in the data sheet!!!! )

Now I modify the helloWorld.c setting the baudrate to 38400 and... what a miracle... I see the text as expected in minicom:

helloBluetooth.png

Once I know that I can communicate through Bluetooth, I now replace the USB to serial adapter by the Arduino and write a sketch for Bluetooth Control.

What we really want to do is to connect an HC-05 to an Arduino (e.g. the Arduino nano) and use it in master mode. To this Arduino we also connect the joystick. A HC-06 is connected to the Arduino, controlling the robot. The values from the joystick will then be transmitted from the fixed Arduino to the robot and depending on the joystick values the DC motors must be controlled.

Before being able to do all this we must setup the HC-05 to master mode and make sure it can be paired with the HC-06. This can be done by switching the HC-05 into AT command mode. You push the button on the module and then power it. When it AT mode the LEDs will be blinking with a frequency of ~ 1/2 Hz.

The https://iotworkshop.africa/pub/Embedded_Systems/RemoteControlThroughBluetooth/serial.tar.gz package contains programs to serially connect to the HC-05 and send AT commands via the USB to serial adapter connected to a USB port on the PC.

Here is the HC-05 AT command set.

Since the HC-05 and the HC-06 look like ordinary serial lines from the controller point of few we define a serial protocol to send commands from an Arduino Nano, equipped with a joystick that is part of the 37-sensor kit and the HC-05 configured as a Bluetooth master to the Arduino Uno on the robot equipped with the HC-06.

Finally we have to add motor control to the receiver part of the protocol and we are done.
Here are the final Arduino sketches:

A sketch to test joystick readout and conversion of the two joystick values into for motor movement values:

https://iotworkshop.africa/pub/Embedded_Systems/RemoteControlThroughBluetooth/joystick.ino

Then the protocol test sketches

https://iotworkshop.africa/pub/Embedded_Systems/RemoteControlThroughBluetooth/sender.ino and

https://iotworkshop.africa/pub/Embedded_Systems/RemoteControlThroughBluetooth/receiver.ino

and the final remote control sketch, which is essentially the protocol receiver sketch with motor control integrated into it.

https://iotworkshop.africa/pub/Embedded_Systems/RemoteControlThroughBluetooth/remoteControl.ino

-- Uli Raich - 2017-04-27

Comments

Topic attachments
I Attachment History Action Size Date Who Comment
PDFpdf HC-0305_serial_module_AT_command_set_201104_revised.pdf r1 manage 83.7 K 2017-10-27 - 14:40 UnknownUser  
Unknown file formatEXT MakeHello r1 manage 0.1 K 2017-04-27 - 11:37 UnknownUser  
PNGpng bluetoothctl.png r1 manage 67.5 K 2017-04-27 - 11:04 UnknownUser  
C source code filec findBaudrate.c r1 manage 2.0 K 2017-04-27 - 11:37 UnknownUser  
PNGpng findBaudrate.png r1 manage 14.0 K 2017-04-27 - 11:04 UnknownUser  
PNGpng hc-06.png r1 manage 69.7 K 2017-04-27 - 11:19 UnknownUser  
Unknown file formatino hc06Test.ino r1 manage 1.1 K 2017-10-23 - 14:15 UnknownUser  
PNGpng helloBluetooth.png r1 manage 2.1 K 2017-04-27 - 11:31 UnknownUser  
C source code filec helloWorld.c r1 manage 2.0 K 2017-04-27 - 11:37 UnknownUser  
Unknown file formatino joystick.ino r1 manage 2.1 K 2017-11-24 - 10:35 UnknownUser  
Unknown file formatino receiver.ino r1 manage 3.3 K 2017-11-24 - 10:35 UnknownUser  
Unknown file formatino remoteControl.ino r1 manage 5.3 K 2017-11-24 - 10:35 UnknownUser  
PNGpng rfcomm.png r1 manage 16.2 K 2017-04-27 - 11:04 UnknownUser  
Unknown file formatino sender.ino r1 manage 4.0 K 2017-11-24 - 10:35 UnknownUser  
Unknown file formatgz serial.tar.gz r1 manage 12.9 K 2017-10-27 - 14:09 UnknownUser  
PNGpng usb2ttlBack.png r1 manage 122.0 K 2017-04-27 - 11:04 UnknownUser  
PNGpng usb2ttlFront.png r1 manage 134.1 K 2017-04-27 - 11:04 UnknownUser  
Edit | Attach | Watch | Print version | History: r5 < r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r5 - 2017-11-24 - uli
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback