Engineers - Reinvent Your Career with Robotics in the Next 90 Days
Consult with Experts in our Career Advancement Program for the Experienced Engineer & Tech Professional. Gain clarity & community. Apply Now!

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest

Control an Arduino Robot with a Bluetooth Keyboard and Computer

Disclosure: Some of the links below are affiliate links. This means that, at zero cost to you, I will earn an affiliate commission if you click through the link and finalize a purchase. Learn Robotics is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a way for websites to earn advertising revenues by advertising and linking to Amazon.com.

Liz Miller Learn Robotics

Attention: Engineer Looking for a New Career in Robotics

Liz here 👋, Engineer,  Robotics Expert & founder of this website.

If I offered to help you advance your 6-Figure+ Robotics, Automation, or AI Career with a $10,000 sign-on bonus in the next 90 days or less…

…without wasting time on unnecessary certifications…
…without studying another coding language or tactic…
…without needing to get another degree and wasting years…

…would you take me up on that offer?

LIMITED SPACE: I have 6 more client spots open this week!

Want a spot? Click the button to access my 12-min training below and then apply if it makes sense.

You just got an Arduino robot, and now you want to add a remote control. In this tutorial, we will show you how to configure the HC-06 Bluetooth module to drive a 2WD Arduino Mobile Robot with your Bluetooth keyboard.

Note: This tutorial uses the HC-06 Bluetooth module, which supports the Serial Port Profile (SPP). That means this module only works as a slave, and you’ll need to use a host computer as an intermediary between the robot and the keyboard.
hc-06 bluetooth module arduino keyboard control robot

This guide assumes you have already assembled, wired, and programmed your robot to move forward, backward, left, and right. Go ahead and configure autonomous controls for your robot first, and then come back to this tutorial to learn how to configure Bluetooth control.

DSD TECH HC-06 Wireless Bluetooth Serial...
Default serail port setting : 9600 1 Pairing code : 1234; Module working voltage 3.3V to 6V
$9.99

Connect the HC-06 Bluetooth module to the Arduino

The HC-06 Bluetooth module has 4 pins. They are TX, RX, VCC, GND. TX plugs into RX on the Arduino; RX plugs to TX on the Arduino; VCC goes to 5V; and GND to Ground.

Here’s a diagram of the setup.

hc-06 bluetooth module arduino keyboard control wiring diagram

If you have right-angle header pins, you can connect the module directly into the Arduino sensor shield without additional wiring. If you aren’t using the Arduino sensor shield, you can wire the module into a breadboard and mount that directly on your robot.

Download a blank loop() program to the Arduino via USB

We don’t want the robot to execute pre-existing programs from the Arduino when we power it on. Therefore, we will download a blank sketch (or empty loop() method) to our controller. Then when we power the robot via its battery pack, we won’t have any unexpected movements.

Step 1 – Open a blank Arduino sketch. Go to File > New.

Step 2 – Plug one end of your USB cable into the Arduino and the other end into the computer. Download your blank sketch to the board.

Power on the Mobile Robot

Now, we can power our robot. This will allow you to connect your computer to the Bluetooth module.

Connect your computer to the HC-06 module

Go into the Bluetooth settings on your computer and scan for available devices. You should see a connection called “HC-06”. Connect to this device as you would any other Bluetooth speaker or device. When you pair the Bluetooth module to your computer, the key is “0000” or “1234” by default.

Write the Bluetooth control program and download to your Arduino

Now that we’re connected, we can download programs via Bluetooth to our robot. Go to Tools > Port and select the one with HC-06.

hc-06 bluetooth module arduino keyboard control download options

Download the sketch by verifying and uploading. Next, we will set up the code for keyboard control using Serial communication.

Step 1 – Create a method for keyboard control

For this tutorial, we will create a method, keyboardControl() to read the value of our keyboard input into the Serial monitor.

First, we will check to see if there is a signal on the Serial port. Then we will read the data using the Serial.read() command.

Lastly, we will use a switch-case statement to compare the input data to one of five cases:

Case 1 – We press a “1” on our keyboard, which will command a left turn.
Case 2 – We press a “2” on our keyboard, which will command the robot to move backward.
Case 3 – We press a “3” on our keyboard, which will command a right turn.
Case 5 – We press a “5” on our keyboard, which will command the robot to move forwards.
Case 0 – We press a “0” on our keyboard, which will command the robot to stop.

Inside each case statement, we will call our autonomous commands for moving. In the sample code, below, you will see the methods, motors() and backwards() which are custom methods used in this example.

Note, you must write the methods to control your motors for this keyboardControl() method to work.

/*control robot via computer keyboard & bluetooth module
  * Uses serial monitor
  * Must be paired to bluetooth module HC-06 on host computer!
   */
void keyboardControl(){
  if (Serial.available() > 0){ //Listen for signal in the serial port
    int data = Serial.read(); 
    Serial.println(data);
    switch (data) { // Switch-case of the signals in the serial port 
    case '1' : // Case Number 1 is received, 
    Serial.println("left");
    motors(0,100,1000); //replace with your leftTurn method
    break;
    case '2' : //// Case Number 2 is received,
    Serial.println("backward");
    backwards(100, 100, 1000); //replace with your backwards method
    break;
    case '3' : // Case Number 3 is received,
    Serial.println("right"); 
    motors(100,0,1000); //replace with your rightTurn method
    break;
    case '5': // Case Number 5 is received,
    Serial.println("forward"); 
    motors(80,100,1000); //replace with your forward method
    break;
    case '0': // Case Number 0 is received, 
    Serial.println("stop");
    digitalWrite(INL1, LOW); //disable motors
    digitalWrite(INL2, LOW);
    digitalWrite(INR1, LOW);
    digitalWrite(INR2, LOW);
    default : 
    break;
    }
  }
}

Step 2 – Initialize Serial communication in the setup() method

Include this line of code in your setup() method to use the Serial Monitor.

Serial.begin(9600);

Step 3 – Call the keyboardControl() method in loop(). Then download the sketch onto the Arduino. We’re now ready to test drive!

Open the Serial Monitor for Testing

Once downloaded, open the Serial Monitor in the Arduino IDE. Navigate to Tools > Serial Monitor.

When you press “5” the robot will move forward; press “2” the robot will move backward; press “1” it’ll go left, and press “3” it’ll turn right. To stop, press “0”.

Press keys on your keyboard – the robot will start to move!

One of the drawbacks to the Serial Monitor is that you must press the enter key to submit values to the Arduino.

Therefore, when you drive the robot, you will press one of the five command keys followed by the enter key. It takes a little practice to drive, but once you have the hang of it, it’s pretty fun to walk around and control the robot via your Bluetooth keyboard!

Improving the RC keyboard controls

Now that we have a template for controlling the robot with our Bluetooth keyboard, we can tweak our system to make it more reliable and enjoyable to drive.

A few improvements I would recommend include:

  • Making turns (left & right) sharper and cleaner
  • Adding the option to type in a numerical speed for on-demand speed control
  • A button or key to switch out of RC keyboard control to autonomous mode and vice-versa
  • Programmable turns by typing in a numerical degree for precise turns

With this foundation and a couple of recommended upgrades, you will be well on your way to teleop control!

Liz Miller Learn Robotics

Attention: Engineer Looking for a New Career in Robotics

Liz here 👋, Engineer,  Robotics Expert & founder of this website.

If I offered to help you advance your 6-Figure+ Robotics, Automation, or AI Career with a $10,000 sign-on bonus in the next 90 days or less…

…without wasting time on unnecessary certifications…
…without studying another coding language or tactic…
…without needing to get another degree and wasting years…

…would you take me up on that offer?

LIMITED SPACE: I have 6 more client spots open this week!

Want a spot? Click the button to access my 12-min training below and then apply if it makes sense.

6 Responses

  1. Your title is misleading you’re not actually connecting a bluetooth keyboard to the arduino robot directly. You are using a computer to control a robot via bluetooth. And that should be the title here.

    1. Chris, the HC-06 Bluetooth module communicates serially, so the only way to use it in this example is with a host computer. If you wanted to put a computer (i.e. Raspberry Pi) on your robot, you could do that instead to have direct control.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Did this content help you? 🤖
...say thanks and send us a coffee!

Experience Engineers & Tech Pro’s:

Gain personalized guidance and direct mentorship from Liz in her Robotics Mentorship Program.

This 12-week professional advancement program will help you soar to the next level in your engineering career. My clients earn $100k+ with a $10k+ sign on bonus.

You’ll uncover strategies they *don’t* teach you in engineering school to help you earn more and live a meaningful life, all while working in high-impact robotics, automation, and AI roles.

Are you next? Check out the 12-min training I put together and then apply if it makes sense.

WHAT IS LEARN ROBOTICS?

Learn Robotics helps self-starters, hobbyists, and tech professionals get started in the robotics industry. 

We leverage a simple formula to make it easy to gain highly marketable and high-value 6-figure robotics & automation skills. 

Learn Robotics was founded by Robotics Engineer, Liz Miller, and leverages the Robotics Success Equation, our unique methodology that helps people learn to build robots while enhancing their careers for the future.

liz's featured robotics PROJECT ON YOUTUBE

Learn Robotics Botly Favicon

MORE LEARN ROBOTICS ARTICLES

Apply to Work with Liz in her Robotics Mentorship Program for Tech Professionals

Liz Miller Learn Robotics