DEV Community

Cover image for Smartphone Controlled Arduino Car via Bluetooth
Halldor Stefansson
Halldor Stefansson

Posted on • Originally published at engineeracar.com

Smartphone Controlled Arduino Car via Bluetooth

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

Code

The software is based on the Adafruit Bluefruit and Cytron Motor Driver libraries.

You can check out the code here at GitHub.

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. I took the top plate and connected it with the bottom, sort of like an extension.

Connection under chassis

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

  1. 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 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, and I recommend going through those as well.

  1. Download the app

    Start by downloading the Bluefruit iOS App or Android App. 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. 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. There are a few code changes that are needed for this project.

    First, make sure to uncomment this part in the controller.ino file:

    // 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:

    // 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:

    // 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

  1. 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. An update notification will pop automatically up in the app when you connect to it, and it won’t take long.

  2. 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 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:

    // 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:

    // Create a new servo object:
    Servo servoSteering;
    

    In the setup(void) method, we need to initialise it:

    // 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:

    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, 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 for the Maker Drive motor driver is also superb. In addition, it has this tutorial 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 library to help us with that.

    Similarly, as before, we add some settings for the pins in BluefruitConfig.h:

    // 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:

    // 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:

    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:

    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 😃


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.

Top comments (0)