2: Bluetooth Controlling
Arduino Project

Magicbot Bluetooth Controling

Make Bluetooth controlled remote car using Magicbot

Components Required

Story

We will learn about how to make a Bluetooth control remote car using Magicbit & Magicbot platform,

 

Theory and Methodology

The theory is very simple. we send data to Magicbit using our remote. Then Magicbit reads that data and then controls the motors according to that command. In this case, our remote is a smartphone. To communicate with Magicbit, we use a specific app. You can find many types of apps from the play store or the app store to do that. We used serial data protocol for sending the data. When we power on the Magicbit, it searches for Bluetooth devices around it. Then it connects with a related device and gets data from it. According to the data sent by the smartphone, the ESP32 processor of the Magicbit will generate motor driver signals. These signals will use by the inbuilt motor driver of the Magicbit to control both motors.

 

Software Setup

This has two sub parts. The first one is the mobile application part and the second one is the Arduino programming part. So let’s look at our first part. To control our car we use “Carino” Bluetooth app which is available in the play store. But you can find a bunch of apps to control the remote car. If you are using an iPhone you can use another app to connect with Magicbit. To connect with Magicbit, click the Bluetooth symbol on the app and choose the ESP32 device. After small time your phone will connect with Magicbit. Now your remote is ready.

Bluetooth Control Car Using Magicbit Pic 4

 

You can directly download firmware from here and upload using other option in Magicbit Uploader. Or you can continue to read for Ardunio program.

In the Arduino code, we will use a Bluetooth library to establish a connection between ESP32 and the phone. This library is already included in your Magicbit library. In the main function we search for other devices. If Magicbit is connected with some other device, then it will begin to receive data. So in the loop function we check if there any data is available. If some data is available we store that data in a variable. This data type is a characteristic data type. If we click on a vehicle controlling command button in the App, then Magicbit receives that data and generates motor outputs according to that command. In this app we can’t change the character. But in other apps we can change that key values as we like. After storing that character we compare that with defined characters of the movements. So if they are equal the robot move according to that command. The LCD part is optional. If you want you can remove it. To control the motors we use 16, 17 18 and 26 GPIO pins of ESP32. When we want to turn on the motors, we should set one pin HIGH and set the other pin LOW. To reverse the direction we have to reverse the signals. The important thing is we didn’t use additional PWM pins to control the speeds. Because we give PWM signal through high state pin. In this case we don’t use PWM theory. We only want to turn on and off the motors.

Bluetooth Control Car Using Magicbit Pic 5

Select COM port and board type as Magicbit and upload the code. First check your motor directions are correct while running. If not then change the configuration of the pins of the motors in the code. Now you have a very simple Bluetooth controlled toy car.

Code

#include "BluetoothSerial.h" //Header File for Serial Bluetooth
BluetoothSerial ESP_BT; //Object for Bluetooth
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
#include <ESP32Servo.h>

Adafruit_SSD1306 display(128, 64);
String incoming;// varible store received data
int M1A = 17; // motor pins
int M1B = 16;
int M2A = 18;
int M2B = 27;

void setup() {
Serial.begin(9600);

display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(1000);
display.clearDisplay();

Serial.begin(9600); //Start Serial monitor in 9600
ESP_BT.begin("MagicBot"); //Name of your Bluetooth Signal
Serial.println("Bluetooth Device is Ready to Pair");
pinMode(M1A, OUTPUT); // configure outputs
pinMode(M1B, OUTPUT);
pinMode(M2A, OUTPUT);
pinMode(M2B, OUTPUT);

}

void loop() {

if (ESP_BT.available()) //Check if we receive anything from Bluetooth
{
incoming = char(ESP_BT.read()); //Read what we received
}
Serial.println(incoming);
if (incoming == "F") { // forward
Serial.println("Forward");
Display("Forward");
Forward();
}
else if (incoming == "B") { // reverse
Serial.println("Reverse");
Display("Reverse");
Backward();
}
else if (incoming == "R") { //turn right
Serial.println("Right");
Display("Right");
Right();
}
else if (incoming == "L") { // turn left
Serial.println("Left");
Display("Left");
Left();
}
else if (incoming == "O") { // turn left
Serial.println("Horn");
Display("Horn");
tone(25,500,500);
}




else {
Stop();

Display("Stop");
}
delay(20);
}

void Forward() {

digitalWrite(M1A, HIGH);
digitalWrite(M1B, LOW);
digitalWrite(M2A, HIGH);
digitalWrite(M2B, LOW);

}

void Backward() {

digitalWrite(M1B, HIGH);
digitalWrite(M1A, LOW);
digitalWrite(M2B, HIGH);
digitalWrite(M2A, LOW);

}
void Right() {

digitalWrite(M1A, HIGH);
digitalWrite(M1B, LOW);
digitalWrite(M2B, LOW);
digitalWrite(M2A, LOW);

}
void Left() {

digitalWrite(M1B, LOW);
digitalWrite(M1A, LOW);
digitalWrite(M2A, HIGH);
digitalWrite(M2B, LOW);

}

void Stop() {
digitalWrite(M1B, LOW);
digitalWrite(M1A, LOW);
digitalWrite(M2A, LOW);
digitalWrite(M2B, LOW);

}


void Display(String commond) { // display data
display.clearDisplay();

display.setTextSize(3); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 20); // Start at top-left corner
display.println(commond);
display.display();

}

If you need help or couldn’t understand a step be sure to check out our youtube video by clicking here: Youtube Video

Related Posts
Leave a Reply