1. Components Required
- L293D Motor Shield
- 4 DC Motors
- Arduino Uno R3: For help watch the video below.
- Arduino Power Wire
- Battery Casing (3 Chambers)
- 3 Lithium-Ion Batteries
- Breadboard
- DC Jack
- Bluetooth Module HC-05
- 20 Male-to-Male Wires
- 20 Female-to-Male Wires
- 4 Wheels
2. Wiring Instructions
Follow the wiring instructions to correctly assemble your Bluetooth-controlled car:
- HC-05 Bluetooth Module Connections:
VCC → 5V (Arduino Uno)GND → GND (Arduino Uno)TX → RX (Pin 0 on Arduino)RX → TX (Pin 1 on Arduino)- L293D Motor Shield to Arduino Uno: Place the motor shield on top of the Arduino Uno and connect the pins correctly.
- DC Motors to Motor Shield:
Motor 1 → M1 (L293D Motor Shield)Motor 2 → M2 (L293D Motor Shield)Motor 3 → M3 (L293D Motor Shield)Motor 4 → M4 (L293D Motor Shield)- Power Supply: Connect the battery casing to the DC jack on the motor shield.
For more help watch this video:
For more help watch this video:
4. Arduino Code
#include
AF_DCMotor motor1(1); // Motor 1
AF_DCMotor motor2(2); // Motor 2
AF_DCMotor motor3(3); // Motor 3
AF_DCMotor motor4(4); // Motor 4
char command;
void setup() {
Serial.begin(9600); // Set baud rate for Bluetooth module
Serial.println("Bluetooth-Controlled Car Ready");
}
void loop() {
if (Serial.available()) {
command = Serial.read();
switch (command) {
case 'F':
moveForward();
break;
case 'B':
moveBackward();
break;
case 'L':
turnLeft();
break;
case 'R':
turnRight();
break;
case 'S':
stopMotors();
break;
}
}
}
void moveForward() {
motor1.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(255);
motor4.setSpeed(255);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void moveBackward() {
motor1.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(255);
motor4.setSpeed(255);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
void turnLeft() {
motor1.setSpeed(150);
motor2.setSpeed(150);
motor3.setSpeed(255);
motor4.setSpeed(255);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void turnRight() {
motor1.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(150);
motor4.setSpeed(150);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
void stopMotors() {
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
5. Flutter Installation on Windows
- Install Git from Git's official website.
- Download and extract the Flutter SDK from Flutter's website.
- Add the Flutter
binfolder to your system's PATH. - Run
flutter doctorin the terminal to check for any missing dependencies.
Add Dependency
Add the flutter_blue_plus package to your pubspec.yaml:dependencies:
flutter_blue_plus: ^1.2.0
5. Flutter Code
import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
class BluetoothControlApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: BluetoothControlPage(),
);
}
}
class BluetoothControlPage extends StatefulWidget {
@override
_BluetoothControlPageState createState() => _BluetoothControlPageState();
}
class _BluetoothControlPageState extends State {
FlutterBluePlus flutterBlue = FlutterBluePlus.instance;
BluetoothDevice? connectedDevice;
BluetoothCharacteristic? writeCharacteristic;
@override
void initState() {
super.initState();
scanAndConnect();
}
void scanAndConnect() async {
flutterBlue.startScan(timeout: Duration(seconds: 5));
flutterBlue.scanResults.listen((results) {
for (ScanResult result in results) {
if (result.device.name == 'HC-05') {
setState(() {
connectedDevice = result.device;
});
connectToDevice(result.device);
flutterBlue.stopScan();
break;
}
}
});
}
void connectToDevice(BluetoothDevice device) async {
await device.connect();
List services = await device.discoverServices();
for (BluetoothService service in services) {
for (BluetoothCharacteristic characteristic in service.characteristics) {
if (characteristic.properties.write) {
writeCharacteristic = characteristic;
break;
}
}
}
}
void sendCommand(String command) {
if (writeCharacteristic != null) {
writeCharacteristic!.write(command.codeUnits); // Send the command to the HC-05 module
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Bluetooth RC Car Controller")),
body: connectedDevice == null
? Center(child: Text("Scanning for HC-05..."))
: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () => sendCommand('F'),
child: Text("Forward"),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () => sendCommand('L'),
child: Text("Left"),
),
SizedBox(width: 10),
ElevatedButton(
onPressed: () => sendCommand('S'),
child: Text("Stop"),
),
SizedBox(width: 10),
ElevatedButton(
onPressed: () => sendCommand('R'),
child: Text("Right"),
),
],
),
ElevatedButton(
onPressed: () => sendCommand('B'),
child: Text("Backward"),
),
],
),
);
}
}
void main() => runApp(BluetoothControlApp());
6. Required Permissions
Add the following permissions to your app's AndroidManifest.xml file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>