a Bluetooth module like the HC-05 or HC-06, which communicates over serial. The mobile app can send simple commands (like “F” for forward, “B” for backward, “S” for stop, etc.) via Bluetooth to control the motors.
Steps:
- Use SoftwareSerial to communicate with Bluetooth.
- Map incoming Bluetooth commands to motor actions.
- Modify the AFMotor control to respond to Bluetooth commands.
Motor Driver Shield (HW-130) Connections:
- Motor 1 (M1) is connected to M1 terminals on the motor driver shield.
- Motor 2 (M2) is connected to M2 terminals on the motor driver shield.
- Motor 3 (M3) is connected to M3 terminals on the motor driver shield.
- Motor 4 (M4) is connected to M4 terminals on the motor driver shield.
- Bluetooth Module (HC-05/HC-06):
- VCC -> 5V on Arduino
- GND -> GND on Arduino
- TXD -> Pin 10 (RX on SoftwareSerial)
- RXD -> Pin 11 (TX on SoftwareSerial)
Explanation of the Code:
#include <AFMotor.h> // Install "Adafruit Motor Shield" driver
#include <SoftwareSerial.h> // It comes bundled with the Arduino IDE by default.
// Define motor objects using AFMotor library
AF_DCMotor motor1(1, MOTOR12_1KHZ); // Motor 1
AF_DCMotor motor2(2, MOTOR12_1KHZ); // Motor 2
AF_DCMotor motor3(3, MOTOR34_1KHZ); // Motor 3
AF_DCMotor motor4(4, MOTOR34_1KHZ); // Motor 4
// Set up SoftwareSerial for Bluetooth communication
SoftwareSerial BTSerial(10, 11); // RX, TX (you can change pins as needed)
void setup() {
// Start Bluetooth serial communication
BTSerial.begin(9600); // Bluetooth module baud rate
Serial.begin(9600); // Debugging serial monitor
// Initialize motors to stop
motor1.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(255);
motor4.setSpeed(255);
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void loop() {
// Check if data is available from Bluetooth module
if (BTSerial.available()) {
char command = BTSerial.read(); // Read incoming Bluetooth data
// Display the received command on the serial monitor (for debugging)
Serial.println(command);
// Check for specific commands to control motors
switch (command) {
case 'F': // Forward command
moveForward();
break;
case 'B': // Backward command
moveBackward();
break;
case 'L': // Left command (turn left by moving motor 1 and motor 2 in opposite directions)
turnLeft();
break;
case 'R': // Right command (turn right by moving motor 3 and motor 4 in opposite directions)
turnRight();
break;
case 'S': // Stop all motors
stopMotors();
break;
default:
stopMotors(); // Default stop if invalid command
break;
}
}
}
// Function to move all motors forward
void moveForward() {
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
// Function to move all motors backward
void moveBackward() {
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
// Function to turn the vehicle left (motor 1 and motor 2 in opposite directions)
void turnLeft() {
motor1.run(FORWARD); // Motor 1 moves backward
motor2.run(FORWARD); // Motor 2 moves forward
motor3.run(RELEASE); // Motor 3 moves forward
motor4.run(RELEASE); // Motor 4 moves backward
}
// Function to turn the vehicle right (motor 3 and motor 4 in opposite directions)
void turnRight() {
motor1.run(RELEASE); // Motor 1 moves forward
motor2.run(RELEASE); // Motor 2 moves backward
motor3.run(FORWARD); // Motor 3 moves backward
motor4.run(FORWARD); // Motor 4 moves forward
}
// Function to stop all motors
void stopMotors() {
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
- SoftwareSerial BTSerial(10, 11);
- This initializes a SoftwareSerial port for Bluetooth communication on pins 10 (RX) and 11 (TX).
- Adjust these pins based on your setup. If you’re using a different set of pins for Bluetooth communication, modify it accordingly.
- Bluetooth Commands:
- The mobile Bluetooth control app sends specific commands via Bluetooth, which are received in the
loop()
. - Based on the command received, the motors are controlled accordingly:
'F'
= Move forward'B'
= Move backward'L'
= Turn left (one motor forward, one motor backward)'R'
= Turn right (one motor backward, one motor forward)'S'
= Stop all motors
- The mobile Bluetooth control app sends specific commands via Bluetooth, which are received in the
- Motor Control Functions:
- moveForward(), moveBackward(), turnLeft(), turnRight(), and stopMotors() are functions that control the motors using the AFMotor library.
Using a Mobile Bluetooth Control App:
- Install a Bluetooth control app (such as the Arduino Bluetooth Controller or Bluetooth RC Controller from the Google Play Store or Apple App Store).
- Pair the Bluetooth module with your phone via Bluetooth settings.
- Use the app to send commands like “F”, “B”, “L”, “R”, or “S” to control the car.