Remote Robot Control With The HC-06 Bluetooth Module

HC-06 Bluetooth Module

Following on from the previous post about adding the HC-06 bluetooth adapter to the breadboard, it’s time to use it to do something useful.

What you should have working up to this point is being able to connect to the HC-06 with a smartphone using the bluetooth terminal app and send some characters which are then received by the Arduino and sent to the serial monitor.

Let’s look at an example for controlling an LED based on the character received over the serial port.

Example 1 : LED Control

 

Upload this code to the Arduino, connect to the HC-06 via bluetooth and send some characters using the bluetooth terminal.

As you can see from the code, sending the letter ‘a’ will turn the LED on and sending the letter ‘d’ will turn the LED off.

Take a few minutes to go through the code and try to understand what is going on.

Let’s take a look at void loop():

Line 27 checks to see if any serial data has been received from the HC-06. If no data has been received, the loop just keeps checking. If a character is received, the if statement is true and we enter into it.

If a character is received, line 29 then checks what that character is and copies it into the ‘msg’ variable. At this poing ‘msg’ will contain whatever character was received like ‘a’ or ‘1’ or ‘!’ for example.

Then on line 31 we have another if statement that checks to see if the character in ‘msg’ is ‘a’. If it is ‘a’, then the LED pin gets set HIGH and we send a message back to the HC-06 saying that the LED is on (This message will be displayed on your phone).

If the character is not ‘a’, we then move on to the ‘else if’ statement on line 36. This line checks to see if the character is ‘d’. If it is ‘d’, then the LED is turned off and a message is sent back to the HC-06.

Line 42 prints the received character to the Arduino serial monitor so you can check to see what the Arduino is receiving.

 

Code Challenge:

Can you see how this type of code could be used to remote control the robot itself?

Try changing this code so that it controls the motors, for example, you could replace line 33 with forward() and replace line 38 with stop(). This would mean the motors would turn forward when you sent an ‘a’ and then stop when you sent ‘d’.

Add to the code so that you can get the robot to go forward, reverse, turn left, turn right, and stop all based on the serial character received from the phone.

 

 

 

 

 

This entry was posted in Tutorial and tagged , , , . Bookmark the permalink.

1 Response to Remote Robot Control With The HC-06 Bluetooth Module

  1. Pingback: Blanchardstown Library 3 Day Workshop | RoboJAM!

Leave a Reply

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