Smartphone Controlled Arduino Car via Bluetooth
Tags: arduino
Published: 2022-02-08 21:27:02

## Required Components

- A chassis
- Arduino Uno
- Maker Drive driver
- Adafruit Bluefruit LE UART Friend
- Smartphone (Android or iPhone)
- Batteries (Example: 1 x 9V and 4 x AAA (1.5V))
- Servo Motor
- 2 x DC Motors

## Circuit Diagram

![Circuit Diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1hbazs0z3vhisqnxmu7n.png)

## Code

The software is based on the [Adafruit Bluefruit](https://github.com/adafruit/Adafruit_BluefruitLE_nRF51) and [Cytron Motor Driver](https://github.com/CytronTechnologies/CytronMotorDriver) libraries.

You can check out the code [here at GitHub](https://github.com/halldorstefans/Bluetooth-Arduino-Car).

## Note

1. **The Chassis**
    
    You could use most chassis with this tutorial. However, you might need to make some changes to the code where relevant. For example, if you only have two wheels, you need to change the servo code to make the motors turn the robot. 
    
    The chassis I used is based on [this Robot Chassis Kit](https://thepihut.com/products/adafruit-mini-3-layer-round-robot-chassis-kit-2wd-with-dc-motors). I took the top plate and connected it with the bottom, sort of like an extension. 

![Connection under chassis](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mli8e83y1zfcmgkdgg8p.jpg)

I built a custom steering system and joined that with the chassis. The steering system is not particularly good, but it does the job for now. I’m planning on building a better one later.

![Front-Wheel Steering](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uaqc9w3tvw7aqh7ox465.jpg)

2. **9V Battery**
    
    I used a 9V battery to power some of the peripherals in this project. The 9V battery seems to be a perfect power source for Arduino projects. But, [reading a bit upon](https://cybergibbons.com/arduino/arduino-misconceptions-6-a-9v-battery-is-a-good-power-source/) power source uses for Arduino, the battery is OK to use but inefficient. I recommend researching the subject if you want to use a 9V battery in your projects. 
    

## Setting up Bluefruit LE UART Friend

These steps are based on the [official documentation](https://learn.adafruit.com/introducing-the-adafruit-bluefruit-le-uart-friend/introduction), and I recommend going through those as well.

1. **Download the app**
    
    Start by downloading the Bluefruit [iOS App](https://learn.adafruit.com/bluefruit-le-connect-for-ios) or [Android App](https://play.google.com/store/apps/details?id=com.adafruit.bluefruit.le.connect&hl=en). Easily and quickly done.
    
2. **Solder the pins - IMPORTANT!**
    
    The module comes with pins to make it easier to connect with a breadboard. I highly recommend soldering those pins to the module. The connection can be flaky otherwise and cause the module to be unreliable. 
    
3. **Wiring**
    
    I suggest starting with the default pinout in the [documentation](https://learn.adafruit.com/introducing-the-adafruit-bluefruit-le-uart-friend/wiring). That’s to make sure it works as intended. Then later, when adding peripherals to the project, test out other pins if needed.
    
4. **Software Configurations**
    
    The code I used is built on the [supplied Adafruit code](https://learn.adafruit.com/introducing-the-adafruit-bluefruit-le-uart-friend/software). There are a few code changes that are needed for this project. 
    
    First, make sure to uncomment this part in the `controller.ino` file:
    
    ```arduino
    // Create the bluefruit object, either software serial...uncomment these lines
    SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN);
    
    Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN,
                          BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN);
    ```
    
    To match the wiring setup, you must adjust the PIN variables in `BluefruitConfig.h`. 
    
    Default setup:
    
    ```arduino
    // SOFTWARE UART SETTINGS
    #define BLUEFRUIT_SWUART_RXD_PIN       9    // Required for software serial!
    #define BLUEFRUIT_SWUART_TXD_PIN       10   // Required for software serial!
    #define BLUEFRUIT_UART_CTS_PIN         11   // Required for software serial!
    #define BLUEFRUIT_UART_RTS_PIN         8   // Optional, set to -1 if unused
    ```
    
    Final setup for the project:
    
    ```arduino
    // SOFTWARE UART SETTINGS
    #define BLUEFRUIT_SWUART_RXD_PIN       2   // Required for software serial!
    #define BLUEFRUIT_SWUART_TXD_PIN       7   // Required for software serial!
    #define BLUEFRUIT_UART_CTS_PIN         4   // Required for software serial!
    #define BLUEFRUIT_UART_RTS_PIN         5   // Optional, set to -1 if unused
    ```
    
5. **Hardware Configuration**
    
    This simple step is also critical and can easily be missed. 
    
    Set the MODE switch on the module to **CMD** mode.


![CMD mode for Bluefruit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vsokj76y8zha4955sslw.jpg)
    
6. **Upload (and Update)**
    
    Once everything has been connected and configured, it’s time to upload the code to the Arduino.
    
    When you first connect to the module, it will most likely need [updating](https://learn.adafruit.com/introducing-the-adafruit-bluefruit-le-uart-friend/dfu-updates). An update notification will pop automatically up in the app when you connect to it, and it won’t take long.
    
7. **Control servo motor (for Front Wheel Steering)**
    
    I used a servo motor to control the front-wheel steering in this project. 
    
    > The DC motors can steer the car if you’re not using front-wheel steering. See [this tutorial](https://tutorial.cytron.io/2019/03/28/line-following-robot-using-arduino-maker-drive/) as an example.
        
    
    Connecting the servo motor and adding relevant code changes is relatively straightforward. 
    
    Before fastening the servo motor down, double-check everything works and what angles to use. Finding the correct angles for the servo motor to work well with the steering can be a bit of trial and error. 
    
    These are the variables I added in  `BluefruitConfig.h`: 
    
    ```arduino
    // SERVO SETTINGS
    #define SERVO_PIN                     6
    #define DEFAULT_ANGLE                 90
    ```
    
    A servo motor object needs to be added to the main `.imo` file, just under the Bluefruit object is good:
    
    ```arduino
    // Create a new servo object:
    Servo servoSteering;
    ```
    
    In the `setup(void)` method, we need to initialise it:
    
    ```arduino
    // Attach the Servo variable to a pin:
      servoSteering.attach(SERVO_PIN);
      servoSteering.write(DEFAULT_ANGLE);
    ```
    
    Finally, we can add the code to change the angle of the motor, for steering, based on which buttons are pressed in the app, for example:
    
    ```arduino
    if (pressed) {
          Serial.println(" pressed");
          if (buttnum == 7) {
            servoSteering.write(45);
          }
          if (buttnum == 8) {
            servoSteering.write(120);
          }
    }
    ```
    

**Warning!** - Be aware that when using the [Servo Arduino library](https://www.arduino.cc/reference/en/libraries/servo/), it disables `analogWrite()`
 (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins. This took me a bit of time to figure out. ☹️

## Running Servo Motor, Two DC Motors & Bluetooth Module At The Same Time

Now to the fun part. Make the car drive.

1. **Connect motors**
    
    The [documentation](https://www.cytron.io/p-maker-drive-simplifying-h-bridge-motor-driver-for-beginner) for the Maker Drive motor driver is also superb. In addition, it has [this tutorial](https://tutorial.cytron.io/2019/03/28/line-following-robot-using-arduino-maker-drive/) that can be helpful as a guideline to connect everything correctly.
    
2. **Modify software**
    
    We need to add a few bits to our code to get the motors to run. We can use the [Cytron Motor Driver](https://github.com/CytronTechnologies/CytronMotorDriver) library to help us with that.
    
    Similarly, as before, we add some settings for the pins in  `BluefruitConfig.h`: 
    
    ```arduino
    // MAKER DRIVE MOTOR SETTINGS
    #define DRIVE_MOTOR_MODE              PWM_DIR
    #define 1A_PIN                        11
    #define 1B_PIN                        10
    #define 2A_PIN                        3
    #define 2B_PIN                        9
    ```
    
    We also create the motor objects at a similar place we added the servo object: 
    
    ```arduino
    // Configure the motor driver.
    CytronMD motor1(DRIVE_MOTOR_MODE, 1A_PIN, 1B_PIN);
    CytronMD motor2(DRIVE_MOTOR_MODE, 2A_PIN, 2B_PIN);
    ```
    
    We then add a few methods to change the speed and directions of the motors:
    
    ```arduino
    void robotStop()
    {
      motor1.setSpeed(0);
      motor2.setSpeed(0);
    }
    
    void robotForward()
    {
      motor2.setSpeed(100);
      motor1.setSpeed(100);
    }
    
    void robotReverse()
    {
      motor1.setSpeed(-100);
      motor2.setSpeed(-100);
    }
    ```
    
    And finally, we update the `loop(void)` method where the buttons are pressed with the servo code changes:
    
    ```arduino
    if (pressed) {
          Serial.println(" pressed");
          if (buttnum == 7) {
            servoSteering.write(45);
          }
          if (buttnum == 8) {
            servoSteering.write(120);
          }
          if (buttnum == 5) {
            robotForward();
          }
          if (buttnum == 6) {        
            robotReverse();
          }
        } else {
          Serial.println(" released");
          if (buttnum == 7 || buttnum == 8) {
            servoSteering.write(DEFAULT_ANGLE);
          }
    
          if (buttnum == 5 || buttnum == 6) {
            robotStop();
          }
        }
}
    ```
    
    Once that’s set up, built and uploaded, we’re almost ready to start driving around.
    
3. **Have multiple power sources**
    
    We need to make sure everything runs simultaneously alongside the Arduino. At first, I only had the 4 x AA batteries, but every time I tried to make a turn, the car just froze. 
    
    That was because we have many peripherals and not enough power to supply them all. 
    
    So I added a 9V battery to run the servo motor and Bluetooth module, while the AA batteries ran the DC motors, motor driver and the Arduino. 
    
## Voila!

It drives and turns 😃

{% twitter 1491148750475177987 %}

---

I enjoyed working on this project, and I'm excited to start on the next one.

Thank you for reading!

If you have any suggestions on improving, please don't hesitate to get in touch. I'm always keen on learning what can be done better.