Bluetooth Voice Controlled Robot

About the project

Here, we'll show you how to create a bluetooth-controlled Robot.

Project info

Difficulty: Moderate

Platforms: Espruino

Estimated time: 3 hours

Items used in this project

Hardware components

An Espruino board with Bluetooth and L293D Motor driver soldered on An Espruino board with Bluetooth and L293D Motor driver soldered on http://www.espruino.com/L293D x 1
An assembled, two-motor Robot An assembled, two-motor Robot http://www.espruino.com/Robot x 1

Software apps and online services

Espruino  Web IDE Espruino Web IDE

Story

What you need

What to do

Connect the four outputs of the Motor Driver to each of the 4 wires from the 2 motors - the order doesn't matter.

Connect the battery, and connect USB to your computer

Now, copy in the following code into the left-hand side of the Web IDE - changing the Pin names for MOTORS to the ones that were used when you wired your L293D motor driver up:


  1. // Pins for the motors
  2. var MOTORS = [B0,B1,A7,A6];
  3. // Pin values to set
  4. var GO = { FORWARD: 0b1010, BACK : 0b0101, LEFT : 0b0110, RIGHT : 0b1001 };
  5.  
  6. function move(motorState, time) {
  7. digitalWrite(MOTORS, motorState);
  8. setTimeout("digitalWrite(MOTORS, 0);", 500);
  9. }


Now, type the following (with the robot's wheels off the ground!):

  1. move(0b1010, 500);

This will move the motors for half a second. The first argument is a binary number which defines which of the 4 motor driver outputs should be on and which should be off.

Keep running the above command but experiment with different orderings of the 1s and 0s in 0b1010 until you have both motors moving forwards. When you have this done, press the up-arrow to move back to the var GO = ... line, and change the number after FORWARD to the one that you found worked. Press the 'Page down' key to move your cursor to the end of the text, and press enter.


You should now be able to type:


  1. move(Go.FORWARD, 500);
And the robot will move forwards.


Now repeat the steps for BACKLEFT and RIGHT until the robot does what you want.

Wireless Control

You can now control it wirelessly! Copy and paste the following code in:


  1. Serial1.setup(9600);
  2. Serial1.on('data', function(command) {
  3. if (command=="w") move(GO.FORWARD, 500);
  4. if (command=="s") move(GO.BACK, 500);
  5. if (command=="a") move(GO.LEFT, 500);
  6. if (command=="d") move(GO.RIGHT, 500);
  7. });


This looks on Serial1 (the serial port that Bluetooth is connected to) and if it gets one of the letters w,s,a or d, it moves the robot.

Try connecting your phone/tablet to Espruino and starting up a terminal application. See the Bluetooth page for more details.

You can now type the letters w,s,a and d on the keyboard of your device, and it'll control the Robot!

You can do a bit better though - if your device has voice typing (most Android ones do) you can control your Robot with your voice!

Just copy and paste the following in:


  1. Serial1.setup(9600);
  2. var command = "";
  3. Serial1.on('data', function(data) {
  4. command += data;
  5. if (command.indexOf(" ")>=0 || command.indexOf("n")) {
  6. command="";
  7. } else {
  8. print(command);
  9. if (command=="forward") move(GO.FORWARD, 500);
  10. if (command=="back") move(GO.BACK, 500);
  11. if (command=="left") move(GO.LEFT, 500);
  12. if (command=="right") move(GO.RIGHT, 500);
  13. }
  14. });


This will watch for the words 'forward', 'back', 'left' and 'right' being sent over bluetooth and separated by spaces and will move the robot when it sees them. Click the little microphone symbol on your Android Device's keyboard to enable voice typing and try it out!


It might take a few attempts to respond - the voice typing expects sentances, not individual commands and so it sometimes gets confused.


If you leave USB connected, you'll see what words are being sent over bluetooth - you could easily add your own commands to do other things!

Note: When you unplug Espruino from USB, the 'console' (that interprets commands you type) will be automatically moved to Serial1, and will stop the data handler for Serial1 from working. To work around this, we'd suggest you add the following:


  1. function onInit() {
  2. USB.setConsole();
  3. }

Then, when you save() your code, every time the Espruino restarts it'll make sure that the console is set to USB, allowing the Serial1 data handler to work.

Code

GitHub repo

Credits

Photo of Espruino

Espruino

Espruino, Espruino Pico and Puck.js are low-power Microcontrollers that run JavaScript. Espruino is a JavaScript Interpreter for Microcontrollers that is designed to make development quick and easy. The Espruino interpreter is firmware that runs on a variety of different microcontrollers, but we also make Espruino Boards that come with the interpreter pre-installed and are the easiest devices to get started with. However Espruino itself isn't just the interpreter firmware or hardware - there's also the Web IDE, command-line tools, documentation, tutorials, and modules that form a complete solution for embedded software development.

   

Leave your feedback...