Search form

Raspberry Pi is very popular for IoT projects because of its seamless ability of wireless communication over the internet. Raspberry Pi 3 has inbuilt Wi-Fi and Bluetooth, and Bluetooth is a very popular wireless communication Protocol. Today we going to build a Remote Controlled Car using Raspberry Pi 3 and Bluetooth, here we will use Smart Phone as a remote to control the car. We have previously built created this RC car using Arduino.

Here we are using Raspberry Pi 3 which have inbuilt Bluetooth, so we don't need to use any external USB Bluetooth dongle. Here we are using RFCOMM Bluetooth protocol for wireless communication.

Programming for Bluetooth in Python follows the socket programming model and communications between the Bluetooth devices is done through RFCOMM socket. RFCOMM (Radio Frequency Communication) is a Bluetooth Protocol which provided emulated RS-232 serial ports and also called as Serial Port Emulation. Bluetooth serial port profile is based on this protocol. RFCOMM is very popular in Bluetooth applications because of its wide support and publically available API. It is bound to L2CAP protocol.

BlueZ is a open source project and official Linux Bluetooth protocol stack. It supports all the core Bluetooth protocols and now become part of official Linux Kernel.

Blueman provides the Desktop interface to manage and control the Bluetooth devices.

Finally we need python Library for Bluetooth communication so that we can send and receive data through RFCOMM using Python language:

sudo apt-get install python-bluetooth

Also install the GPIO support libraries for Raspberry Pi:

sudo apt-get install python-rpi.gpio

Now we are done with installing required packages for Bluetooth communication in Raspberry Pi.

Pairing Devices with Raspberry Pi over Bluetooth:

Pairing Bluetooth Devices, like mobile phone, with Raspberry Pi is very easy. Here we have paired our Android Smart phone with Raspberry Pi. We have previously installed BlueZ in Pi, which provides a command line utility called “bluetoothctl” to manage our Bluetooth devices.

Now open the bluetoothctl utility by below command:

sudo bluetoothctl

You can check all the commands of bluetoothctl utility by typing ‘help’. For now we need to enter below commands in given order:

[bluetooth]# power on
[bluetooth]# agent on
[bluetooth]# discoverable on
[bluetooth]# pairable on
[bluetooth]# scan on

After the last command “scan on”, you will see your Bluetooth device (Mobile phone) in the list. Make sure that your mobile has Bluetooth turned on and visible by nearby devices. Then copy the MAC address of you device and pair it by using given command:

pair <address of your phone>

Then you will be prompted for Passcode or Pin in your Terminal console then type passcode there and press enter. Then type the same passcode in your mobile phone when prompted and you are now successfully paired with Raspberry Pi. We have also explained this whole process in the Video given in previous GPIO controlling Tutorial. Here is the direct YouTube link.

As told earlier, you can also use Desktop interface to pair the Mobile phone. After installing Blueman, you will see a Bluetooth icon in right side of your Raspberry Pi desktop as shown below, using which you can easily do the pairing.

Selecting the Toy Car:

In this Raspberry Pi Controlled Car project we have used a toy car for demonstration. Here we have selected a RF toy car with moving left-right steering feature. After buying this car we have replaced its RF circuit with our Raspberry circuit. This car has two DC motors, one to rotate two front wheel and other one is to rotate two rear wheels. Front side motor is used for giving direction to car means turning left or right side (like real car steering feature). And rear side motor is used for driving the car in forward and backward direction. A Bluetooth of Raspberry is used to receive command wirelessly from android phone to control the car.

You can use any toy car which has two DC Motors to rotate front and Rear Wheels.

Circuit Diagram and Explanation:

In this Remote Controlled Car, we only need to connect Raspberry Pi with two motors using L293D module. For powering the Raspberry Pi and car we have used a mobile power bank. The mobile power bank is enough to power the Raspberry Pi and car’s motors but when we put power bank over car then due to the heavy weight of mobile power bank car would not be able to move properly. So we recommend to use the low weight power supply or Lithium batteries for powering the system. All the connections are shown in circuit diagram below. Also check our Robotics section to learn more about controlling Motors with different technologies.

Note: do not put more than 5v to the raspberry pi.

This circuit has been made on Perf Board for this project, so that there is less weight on the car.

Controlling Car Remotely with Android App BlueTerm:

Now after setting up all the things and successfully try paring the Smart Phone over bluetooth, we need to install an Android App for communicating with Raspberry Pi using a Bluetooth Serial Adapter, so that we can control the GPIO pins of Raspberry Pi. As told earlier RFCOMM/SPP protocol emulates serial communication over Bluetooth, so we installed here BlueTerm App which supports this protocol.

You can also use any other Bluetooth Terminal App which supports communication via RFCOMM socket.

Now after downloading and installing the BlueTerm App, run the below given Python Program from the terminal and connect the paired raspberrypi device from the BlueTerm App at the same time.

After successful connection you will see connected:raspberrypi at the top right corner of the App as shown below:

Now you can just enter the following commands from the BlueTerm app to make the car moving in the desired direction. Press ‘q’ to exit the program. You can use Google Voice Typing Keyboard to control this car using your Voice. Check the complete demo in the Video given at the end.

Commands:

F – Forward Move

B – Backward Move

S – Stop

L – Forward Left Move

R – Forward Right Move

A – Backward Left Move

P – Backward Right Move

Q – Quit

Python Programming:

Python Program for Controlling Raspberry Pi GPIO with Android App is very simple and self-explanatory. Only we need to learn a little bit about the code related to Bluetooth RFCOMM communication. Otherwise its same like controlling any robot or car by making motor’s pin high or low. The complete program is given below in the Code section.

First, we need to import the Bluetooth socket library which enables us to control Bluetooth with Python language; we have installed the library for the same in the previous section.

import Bluetooth

Then we included some more header files and define pins for motors put them default low.

After this we have created some functions that are responsible for moving car in the desired direction: def left_side_forward(), def right_side_forward(), def forward(), def left_side_reverse(), def right_side_reverse(), def reverse()def stop(). These functions will be called respectively when we press L, R, F, A, P, B, S from the Mobile blueTerm app and the car will move accordingly.