In this lesson, we’ll build a remote-controlled car using Arduino, a Bluetooth module, and motors. Get ready to drive your own creation wirelessly!
Objective: Learn how to control a remote control car using Arduino and a Bluetooth module.
Materials:
- Arduino Uno board
- Breadboard
- DC motors (2)
- Motor driver module (L298N)
- Bluetooth module (HC-05 or similar)
- Jumper wires
- 9V battery (or power supply)
- Remote control device (Android/iOS app or Bluetooth remote)
- Computer with Arduino IDE software installed
Steps:
- Review: Recap previous lessons and components, including motors, motor drivers, and Bluetooth communication.
- Meet the Bluetooth Module: Introduce the Bluetooth module and explain how it enables wireless communication.
- Building the Circuit: Connect the motors, motor driver, Bluetooth module, Arduino board, and battery to the breadboard.
Step 1: Connecting the Bluetooth Module
- Find the pins on the Bluetooth module: It usually has pins for RX, TX, VCC, GND, and sometimes a status indicator LED.
- Connect the Bluetooth module to the Arduino:
- Connect the module’s RX pin to the Arduino’s digital pin 10.
- Connect the module’s TX pin to the Arduino’s digital pin 11.
- Connect the module’s VCC pin to the Arduino’s 5V pin.
- Connect the module’s GND pin to the Arduino’s GND pin.
- Power on the Bluetooth module: You might need to use a separate power supply if it doesn’t have a 5V output.
Step 2: Connecting the Motor Driver and Motors (Review)
- Follow the steps from Lesson 7 to connect the motor driver (L298N) and the motors to the Arduino and the battery.
Step 3: Programming the Arduino
- Open the Arduino IDE.
- Download and install the SoftwareSerial library: Go to “Sketch” -> “Include Library” -> “Manage Libraries…” and search for “SoftwareSerial”. Install the library.
- Copy and paste the following code (adjust for your specific Bluetooth module and motor driver):
#include <SoftwareSerial.h> // Include SoftwareSerial library
SoftwareSerial bluetooth(10, 11); // Define Bluetooth module pins
const int motor1Pin1 = 9; // Motor 1 control pin 1
const int motor1Pin2 = 10; // Motor 1 control pin 2
const int motor2Pin1 = 5; // Motor 2 control pin 1
const int motor2Pin2 = 6; // Motor 2 control pin 2
void setup() {
bluetooth.begin(9600); // Initialize Bluetooth module
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
}
void loop() {
if (bluetooth.available() > 0) {
char data = bluetooth.read(); // Read data from Bluetooth
switch (data) {
case 'F':
forward(); // Move forward
break;
case 'B':
backward(); // Move backward
break;
case 'L':
left(); // Turn left
break;
case 'R':
right(); // Turn right
break;
case 'S':
stop(); // Stop
break;
}
}
}
// Define motor functions
void forward() {
// ...
}
void backward() {
// ...
}
void left() {
// ...
}
void right() {
// ...
}
void stop() {
// ...
}
- Implement the motor functions (forward, backward, left, right, stop):
- Refer to your motor driver’s documentation to figure out how to control the motors using the digitalWrite() function.
- Upload the code to the Arduino board.
Step 4: Pair the Bluetooth Module and Test
- Pair the Bluetooth module with your remote control device: Refer to the instructions for your Bluetooth module and remote control device.
- Send commands from the remote: Use the remote control device to send commands like ‘F’ (forward), ‘B’ (backward), ‘L’ (left), ‘R’ (right), and ‘S’ (stop).
- Observe the car’s movement: Your car should respond to the commands and move accordingly.
Congratulations! You’ve built a remote-controlled car!
Let’s Experiment:
- Adjust the speed: Change the delay() values in the motor functions to control the car’s speed.
- Add more functionality: Try adding more commands to control things like headlights, sounds, or other features.
- Customize the car: Design and build a chassis for your car to make it more robust and stylish!
Keep experimenting with motors, Bluetooth, and have fun with Arduino!