Sunday, August 26, 2018

Arduino Remote Controlled Car – Part 1


For my vacation , I decided to build a remote-controlled car.  In the next few posts, I will detail how I made the car and what parts were used.  I will also share the code and show how everything is wired and works together.

For this car, I deciced that I was going to 3D print the entire body and remote therefore the first thing I had to do was to design and print the parts.  For this I found several things on thingiverse where I used a few of the parts as is but most of them I made modifications to.  I also created a number of the parts myself. If you would like to print the car/remote yourself, you can find the STL files for all of the parts here:  https://www.thingiverse.com/thing:3067342

The following video shows the car in action:


If you are going to be following along and building the car as we document it here, you should start off by printing the joystick_base.stl and the Arduino_joystick_stand.stl files. You will want to print these files first because we will start off configuring the Bluetooth modules and getting the Arduino used for the remote control communicating with the Arduino used by the car.  For this first post you will need the following parts:

2 Joystick breakout modules (Link is for a ten pack, only by one ten  pack)
2 Prototyping shields (Link is for a five pack, only by one five pack)
2 Portable phone charger batteries The link is not to the exact one that I use because they are no longer available but the one in the link are the similar physical size.



The following Fritzing diagram shows how to wire the two Bluetooth modules and the two joystick modules to the Arduino.  One note, we do not need a common ground between the two Arduinos because they will be two separate circuits.  One circuit for the remote and one circuit for the car.




The first thing we need to do is to configure the two Bluetooth modules, so they will automatically connect and begin communicating.  To do this we will need to create a small Arduino project that will allow us to configure the modules using the serial monitor in the Arduino IDE. Let’s look at the code for this.

This code will start off by including the Arduino SoftwareSerial library and creating an instance of the SoftwareSerial type.  The following code shows how to do this.

  #include <SoftwareSerial.h>
  SoftwareSerial HC05(10, 11); 
  bool addNewLine = false; 


The first line includes the SoftwareSerial library and the second line creates an instance of the type.  The Boolean variable in the last line will be used to tell the application when to add a new line in the serial console to make the output easier to read.  

Now we need to add code to the setup() function that will configure the serial console and the SoftwareSerial instance.  The following code shows the setup() function for this project.

  void setup()  {    
    Serial.begin(9600);    
    pinMode(9,OUTPUT);    
    digitalWrite(9,HIGH);    
    HC05.begin(38400);       
    Serial.println("Connected to HC-05.  Try connecting from any device or issue AT commands");  
  } 


This code starts off by setting up the serial console with a baud rate of 9600 and then defines that the digital 9 pin will be an output pin and set it to high.  The digital 9 pin is connected to the key pin on the HC-05 Bluetooth module.  We pull this pin high to enable the Bluetooth module.  We then configure the HC05 instance of the SoftwareSerial type with a baud rate of 38400 and print a message to the serial console letting the user know that everything is configured and ready to go.  You will note, in this first project we set the baud rate of the SoftwareSerial instance to 38400 because we are configuring the Bluetooth module.  Once the Bluetooth module is configured we set the baud rate to 9600 when we are sending/receiving data.

In the loop() function we will take any input from the Bluetooth module and print it to the serial console and any input from the serial console we will send to the Bluetooth module.  The following code will do this.

  void loop()  {    
    if (HC05.available()) {      
      if (addNewLine) {        
        Serial.write("\r\n");        
        addNewLine = false;      
      }      
      Serial.write(HC05.read());    
    }      
    if (Serial.available()) {      
      HC05.write(Serial.read());      
      addNewLine = true;    
    }  
  }


In this function the first thing we do is to see if there is any data available from the HC05 SoftwareSerial instance (the Bluetooth module) by using the available() function.  If there is data available, we check to see if we need to add a new line to the serial console by checking the addNewLine Boolean variable.  If we need to add a new line, we write a carriage return and line feed to the serial console and then set the addNewLine Boolean variable to false.  Finally we write the data that was received from the Bluetooth module to the serial console.

Next, we check to see if there is any data available from the serial console, also using the available() function and if so we write that data to the Bluetooth module.  We also set the addNewLine Boolean variable to true, so the next time data is received from the connected device, we will add a carriage return and line feed to the serial console.

Before we plug the Arduino in and run this code, we will need to set the HC-05 Bluetooth module into configuration mode.  To do this we will need to press and hold the button on the Bluetooth module and then plug the Arduino into the computer giving power to the Bluetooth module.  In just a couple of seconds the light on the Bluetooth module will start to blink very slowly where the light will be on for two seconds and then turn off for two seconds.  Once the light starts to blink, we can release the button and Bluetooth module is ready to be configured.  

To configure the Bluetooth module, we issue AT commands.  To send an AT command, you would use the following format:
  
  Set item: AT+{command}{new setting}  
  Query item: AT+{command}?

To set an item we type in the letters AT followed by the plus sign, the command and the new setting without any spaces.  For example, to set the role of the Bluetooth module to a slave role we would issue the following command (note: AT commands are case insensitive):
at+role0
To query the item, we would type in the letters AT followed by the plus sign, the command and then a question mark.  For example, to query the role of the Bluetooth module we would use the following command:
at+role?
To issue the command we type the command into the input box on the serial console and press enter.  We will need to set the serial console to add both a NL (new line) and a CR (carriage return).  The following image shows how to issue a AT command:



After we type in the at+role? Command we press the enter key or the send button to send the command to the Bluetooth module.  The Bluetooth module will respond with the results of the query as shown in the following image:



Let’s go ahead and configure one of the Bluetooth modules as the master and one as the slave.  It does not matter which is the master or which is the slave because either device can stream data to the other device.

Let’s start off by configuring the slave device.  To do this connect one of the Arduinos to the computer, run the application that we wrote at the beginning of this post and then run through the command that we will outline in the next few paragraphs.

The first thing we will want to do is to issue the test “AT” command to the Bluetooth module.  The module should respond back with an “OK” message.  If you do not get a response back, check to make sure that the serial console is configured to send both the NL & CR (new line and carriage return).  If you receive an error response, try issuing the “AT” command again. 

Once we are sure that the serial monitor, and the Bluetooth module are talking, we will want to see what the UART settings are currently set to for this module.  To do this send the “AT+UART?” command.  For the examples in these next few posts we are going to assume that the UART settings are 9600 Baud, 0 stop bits and 0 parity.  If this is not how your module is configured then issue the following command “AT+UART=9600,0,0”.

The next thing we want to do is to set the role of the device to a slave role.  To do this we issue the following command “AT+ROLE=0”.  Finally, we will want to retrieve the address of this Bluetooth module. The following command will retrieve the address “AT+ADDR?”.  Make sure the address is written down, because we will be using it when we configure the master device.
The commands that we ran to configure the slave module are:

Command
Response
AT
OK
AT+UART?
+UART:9600,0,0 (if not set it to this)
AT+ROLE=0
OK
AT+ADDR?
+ADDR:{address}

Now let’s configure the master.  To do this connect the other Arduino to the computer (remember to press and hold the button as you power up the module), run the code we wrote at the beginning of this section and issue the commands that we will go through in the next few paragraphs.  

As with the slave device, the first thing we will want to do is to issue the “AT” command to the Bluetooth module.  The module should respond back with an “OK” message. If you do not get a response back, check to make sure that the serial console is configured to send both the NL & CR (new line and carriage return).  If you receive an error response, try issuing the “AT” command again. 

Now we will want to see what the UART settings are for the module.  To do this send the “AT+UART?” command.  We will want to configure the UART settings to 9600 Baud, 0 stop bits and 0 parity.  If this is not how your module is configured then issue the following command “AT+UART=9600,0,0”.

The next thing we want to do is to set the role of the device to a master role.  To do this we issue the following command “AT+ROLE=1”.  Now we will want to set the connection mode to connect to a fixed address (mode 0).  To do this issue the following command “AT+CMODE=0”.  Since we are telling the Bluetooth module to connect to a fixed address, we need to give it the address of the slave device we need it to connect too. To do this issue the following command “AT+BIND=????,??,??????” where the question marks are the address of the slave device.  

When we queried the address of the slave device the address was returned separated with colons like this “98d3:31:300e42”. When entering the address in the bind command the address needs to be separated by commas like this “98d3,31,300e42”.

The commands that we used to configure the master device are:

Command
Response
AT
OK
AT+UART?
+UART:9600,0,0 (if not set it to this)
AT+ROLE=1
OK
AT+CMODE=0
OK
AT+BIND=????,??,?????? (question marks are the addr of the slave devie)
OK

Now if we reset both devices by recycling the power the two Bluetooth modules should connect.  Start off by recycling the power on the slave device and you will see the LED blinking rapidly.  Then recycle the power on the master and once the two devices connect the LED on both devices will blink rapidly twice, then turn off for two seconds and then repeat. This light sequence indicates that the two devices are connected.  Now that we have the two Bluetooth modules connected, we will need to stream the joystick data from the Arduino that are connected to the joysticks to the other one.  We will start off by defining the protocol that we will use when streaming the data.

When we are streaming data, or sending large amounts of variable length data, we need some way to tell the receiving device where a new message starts and where it ends.  Luckily for us, there are built in ASCII codes that allow for this.  The 0x01 SOH (Start of Heading) and the 0x04 EOT (End of Transmission) codes can be used to tell the receiving device when a message starts and when it ends.  For our remote-controlled car, the protocol that we will define is when the receiving device receives an 0x01 ASCII character, it will know that a new message has begun.  When it receives an 0x04 ASCII character, it will know that the message has ended.  Everything between the 0x01 and the 0x04 characters are the message itself.  

The message itself will contain two bytes of data, one that indicates the vertical position of the left joystick and one that indicates the vertical position of the right joystick.  Therefore, a complete transmission will contain a total of four bytes likes this:

  0x01 ßStart of header
  0xDD ßvertical position left (221 decimal)
  0xDD ßvertical position right (221 decimal)
  0x04 ßEnd of transmission


Now that we have the protocol that will be used to transmit the joystick position from one Arduino to the other, let’s begin by writing the code that will run on the Arduino that the joystick breakout modules is connected to.  
The position of the joystick is read through the two analog pins that are connected to it. The first thing we will need to do in the code is to include the SoftwareSerial library for the Bluetooth module, create an instance of the SoftwareSerial type and define the pins that the joystick module is connected to.  The following code will do this:

  #include <SoftwareSerial.h>

  #define BT_PIN 9 // digital pin Bluetooth
  #define LEFT_VER_PIN 1 // analog pin 
  #define RIGHT_VER_PIN 3 // analog pin 

  
  SoftwareSerial HC05(10, 11); 

In the setup() function we will need bring the BT_PIN high and initialize both the serial console and the instance of the SoftwareSerial instance. We initialize the serial console in all examples in case we need to log anything for debugging.  The example code does not use it but it is there if you need it.  Here is the code for the setup() function:

  void setup() {    
    pinMode(BT_PIN,OUTPUT);      //Set Mode BT_PIN     
    digitalWrite(BT_PIN,HIGH);   //Set BT_PIN high    
    HC05.begin(9600);            //Initialize the SoftwareSerial instance    
    Serial.begin(9600);          //Initialize serial console        
    Serial.println("Connected to HC05.");  
  }

In our loop() function we will need to read the vertical positions of both joysticks and then write the message to the Bluetooth module.  The following code will do this:

  void loop() {    
    int left_ver_pos = analogRead(LEFT_VER_PIN) / 4;    
    int right_ver_pos = analogRead(RIGHT_VER_PIN) / 4;     
    HC05.write(0x01);    
    HC05.write(left_ver_pos);    
    HC05.write(right_ver_pos);    
    HC05.write(0x04);    
    delay(50);  
  }


In this code we read the vertical position for both the left and right joysticks.  We then use the HC05 instance of the SoftwareSerial type to write the data to the Bluetooth module.  A short delay was added at the end of the function before we loop back and start over.  

The Arduino analog pins have a range from 0 to 1023 however we want to limit the positional information, that we transmit, to a single byte which can have a range from 0 to 255 therefore when we read the vertical position of the joysticks we divide the number by 4 so the vertical position will be within the size of a single byte.

Now that we have the code that will run on the Arduino that the joystick is connected to, we need to write the code that will run on the Arduino that will receive the data.  This code will need to start off by including the SoftwareSerial library for the Bluetooth module and create an instance of the SoftwareSerial type.  We will also need to define a buffer that will be used to store the data as it comes in through the Bluetooth module.  The following code will do this:

  #include <SoftwareSerial.h>  
  #define MAXBUF 16  
  #define BT_PIN 8
  
  SoftwareSerial HC05(12, 13);   
  byte buf[MAXBUF];

This code starts off by including the SoftwareSerial library and then defines the max size for the input buffer which is 16. While we could limit the size of the buffer to four because we know that each message will be four bytes in size, we always want to have extra space in the buffer, especially with wireless communication, in case the message gets messed up in transmission.  

In the setup() function we will initialize the serial console and the SoftwareSerial instance.  We will also need to pull the key pin for that Bluetooth module high. The following code does this:

  void setup()  {    
    Serial.begin(9600);    
    pinMode(BT_PIN,OUTPUT);    
    digitalWrite(BT_PIN,HIGH);    
    HC05.begin(9600);    
    Serial.println("Connected to HC05");   
  }


Now in the loop() function we will want to continuously read the input from the Bluetooth module until we receive and End of Transmission (0x04) byte.  As we read the data in, it will be stored in the byte array and once the 0x04 byte is read in we will print out the message and then loop back. Here is the code for the loop() function:

  void loop()  {    
    memset(buf, 0, MAXBUF);    
    int counter = 0;    
    while (counter < MAXBUF) {      
      if (HC05.available()) {        
        byte val = HC05.read();        
        buf[counter] = val;        
        counter++;        
        if (val == 0x04) {          
          break;        
        }      
      }
    
    }    
    for(int i=0; i<counter; i++) {      
      Serial.print(buf[i]);      
      Serial.print(" ");    
    }    
    Serial.println(" ");   
  }


This function starts off by using the memset() function to initialize the buffer with all zeros.  We then create an integer variable that will count how many bytes are read in.

A while loop is used to continuously loop until the maximum number of bytes have been read.  Within the while loop we use the available() function from the HC05 SoftwareSerial instance to see if there is any values to read from the Bluetooth module.  If there is a value to read, we use the read() function to read the value in, store it in the buf byte array and increments the counter.  We then check to see if the value that was read in is equal to 0x04 and if so we use the break statement to break out of the while loop.

Finally we create a for loop that will loop through the values in the buffer and print them to the serial console.  If we execute the code on both Arduinos and move the joystick around, we will see output similar to the following screen shot:




As we can see from the output, each message starts with the 0x01 byte and ends with the 0x04 byte.  In between these two bytes is the joysticks positions of the vertical axis.

We know that the packets are supposed to be four bytes in length, in a production environment we would want to toss out any messages that were not four bytes in length because we know that if the message is not four bytes in length then the message got messed up in transmission.  

Now that we are able to transmit data between the two Arduino’s we need to build the remote with the joystick_base and the Arduino_joystick_stand.  The remote should look like the following image where we use one of the prototype shields to connect the Bluetooth and joystick modules to the Arduino.  The portable phone battery, used to power the Arduino, should be able to rest behind the joystick base to make the remote easy to hold.




In the next post we will begin building the car’s body and add the motors to the remote-controlled car.  If you are building the car as you are reading these posts, you may want to begin printing the pieces for the car because it will take a few days to print. 

In my new book, Mastering Arduino , which will be released in September of 2018, I dedicate a chapter to Bluetooth Classic and the HC-05 Bluetooth module.  In this chapter we take an in depth look at Bluetooth Classic and how the HC-05 works. We also look at all of the different configuration options for the Bluetooth module explain why I prefer Bluetooth classic, for streaming data, over Bluetooth LE.