In the previous post, I showed you how you can read accelerometer values using BLE from Arduino Nano IOT in our Android Phone. In this post, I will show you how you can read these data on an Arduino board and use it to control a robot. Yes an Arduino BLE Gesture Controlled Robot.

Arduino BLE Robot

Introduction to Arduino BLE Gesture Controlled Robot

Arduino Nano 33 BLE Sense

It is powered by Nina B306 module that support BLE as well as Bluetooth 5 connection. The inbuilt Bluetooth module consumes very low power and can be easily accessed using Arduino libraries.

This makes it easier to program and enable wireless connectivity to any of your projects in no time.You won’t have to use external Bluetooth modules to add Bluetooth capability to your project. Save space and power.

Arduino BLE – Bluetooth Low Energy

BLE is a version of Bluetooth which is optimized for very low power consuming situations with very low data rate. We can even operate these devices using a coin cell for weeks or even months.

Arduino have a wonderful introduction to BLE but here in this post, I will give you a brief introduction for you to get started with BLE communication.

Basically, there are two types of devices when we consider a BLE communication block.

  • The Peripheral Device
  • The Central Device

Peripheral Device is like a Notice board, from where we can read data from various notices or pin new notices to the board. It posts data for all devices that needs this information.

BLE Devices – Peripheral and Central Devices

Central Devices are like people who are reading notices from the notice board. Multiple users can read and get data from the notice board at the same time. Similarly multiple central devices can read data from the peripheral device at the same time.

The information that is given by the Peripheral devices are structured as Services. And These services are further divided into characteristics. Think of Services as different notices in the notice board and services as different paragraphs in each notice board.

If Accelerometer is a service, then their values X, Y and Z can be three characteristics.

Now let’s take a look at a simple Arduino BLE example.

Getting Started with Arduino and BLE

Using Arduino with BLE is pretty much simple. There are Arduino libraries which can be used to access the BLE chipset. Learn more about using BLE with Arduino.

Here I have explained everything you need to know about using BLE with Arduino Nano 33 IOT. If you don’t like to read, watch the below video tutorial.

Arduino BLE Gesture Controlled Robot Tutorial

Now we will start building our Arduino BLE Gesture Controlled Robot. For that, first take a look at the components required.

Components Needed

  • Arduino Nano 33 IOT
  • Arduino Nano 33 BLE Sense
  • Arduino Nano Motor Driver PCB

The Transmitter

As the transmitter, we only need an Arduino Nano 33 BLE Sense. Why no sensor? As I mentioned in before, Arduino Nano 33 BLE Sense has all the sensors you need to build your Arduino BLE Based Gesture Controlled robot. You can simply attach it to your glove and power it up using a power bank.[AdSense-C]

In the transmitter side, all you need to do is set the Arduino Nano 33 BLE Sense as a peripheral that will  read the accelerometer values from the sensor, set itself as a peripheral and broadcast the values periodically. This was explained in the previous post where I shoed you how to read Accelerometer values form the Arduino board. You can find the tutorial below.

You can use the same code for BLE Accelerometer Transmitter here this project.

The Receiver

Here, you will need a Robot Chassis with 2 DC Motors, an L298 motor Driver Board and Arduino Nano 33 IOT.

Custom Made L298 Motor Driver PCB

EasyEDA is an easier but powerful online PCB design tool which allows electronics engineers, hackers, educators, hobbyists, makers, and enthusiasts to design and share their projects’ schematics as well as PCB layout. This is a design tool integrated LCSC components catalog and JLCPCB PCB service that helps users to save time to make their ideas into real products.

Designing the PCB

To get Started, First Go to EasyEDA website and create a free account. Go to “Editor” and create a new project. For now, JLCPCB have 689 Basic components and 30k+ Extended components at your disposal. See the complete list of components here.

Make sure you add the components from this list while drawing the schematics in EasyEDA. You can even search for the components and check its availability.

Now you can get your layout done using inbuilt tools in EasyEDA. You can now download the Gerber file and use it to manufacture your PCB from JLCPCB.

Gerber File contains information about your PCB such as PCB layout information, Layer information, spacing information, tracks to name a few. BOM File or Bill Of Material contains the list of all components in the Layout. CPL file(Component Placement List / Pick & Place File (PNP) file), it is used by automated SMT Assembly machines to determine where each part should be located on the board.

JLCPCB PCB Ordering

Go to JLCPCBs website and Click on “Quote Now” and upload your Gerber File. Once the Gerber file is uploaded, it will show you a preview of your circuit board. Make sure this is the PCB Layout of the board you want. Below the PCB preview, you will see so many options such as PCB Quantity, Texture, Thickness, Color etc. Choose all that are necessary for you.

Click on “Assemble your PCB boards”.

Now, you will have to upload the BOM and CPL file that we downloaded earlier. Select all the components you want JLCPCB to assemble in your PCB.

Simply click on the confirm box to select the components. In this page, you can review your order. You can check the layout, see all the components and if there is any problem, you can click on “Go Back” to edit your order.

Once everything is done, click on “Save To Cart”. In the next page, you can choose a shipping and payment option and Check Out Securely. You can either use Paypal or Credit/Debit Card to pay.

The PCB will be manufactured and shipped within days and will be delivered to your doorstep within the mentioned time period.

Here is the completely assembled PCB.

The  Robot  Kit

This is the robot chassis I used to make my BLE Robot. I got this kit banggood.com. Not only this one, they have so many types of robot frames, motors and almost all the sensors for doing arduino, raspberry pi and other electronics and hobby projects.

You will get all these things for a cheap price with really fast and quality shipping.

And the great thing about this kit is they provide all the tools you need to assemble the frame together.

Click Here to Buy

Arduino BLE Robot Code

Next connect your Arduino Board to your Computer and upload the below code.

#include <ArduinoBLE.h>
byte x,y,z;
// variables for button
const int buttonPin = 2;
int oldButtonState = LOW;
int bf, motor1_speed, motor2_speed;


#define MOTOR_A1_PIN 2
#define MOTOR_B1_PIN 4
#define PWM_MOTOR_1 3

#define MOTOR_A2_PIN 7
#define MOTOR_B2_PIN 8
#define PWM_MOTOR_2 5

void setup() {

// initialize the BLE hardware
BLE.begin();

Serial.println("Arduino BLE Robot");

// start scanning for peripherals
BLE.scanForUuid("1101");

}

void loop() {
// check if a peripheral has been discovered
BLEDevice peripheral = BLE.available();

if (peripheral) {
// discovered a peripheral, print out address, local name, and advertised service
Serial.print("Found ");
Serial.print(peripheral.address());
Serial.print(" '");
Serial.print(peripheral.localName());
Serial.print("' ");
Serial.print(peripheral.advertisedServiceUuid());
Serial.println();

if (peripheral.localName() != "Arduino Accelerometer") {
return;
}

// stop scanning
BLE.stopScan();

controlLed(peripheral);

// peripheral disconnected, start scanning again
BLE.scanForUuid("1101");
}

}

void controlLed(BLEDevice peripheral) {
// connect to the peripheral
Serial.println("Connecting ...");

if (peripheral.connect()) {
Serial.println("Connected");
} else {
Serial.println("Failed to connect!");
return;
}

// discover peripheral attributes
Serial.println("Discovering attributes ...");
if (peripheral.discoverAttributes()) {
Serial.println("Attributes discovered");
} else {
Serial.println("Attribute discovery failed!");
peripheral.disconnect();
return;
}

// retrieve the LED characteristic
BLECharacteristic Accel1Characteristic = peripheral.characteristic("2101");
BLECharacteristic Accel2Characteristic = peripheral.characteristic("2102");


if (!Accel1Characteristic) {
Serial.println("Peripheral does not have X Char");
peripheral.disconnect();
return;
} else if (!Accel1Characteristic.canRead()) {
Serial.println("Peripheral does not have a Readable X Char");
peripheral.disconnect();
return;
}

if (!Accel2Characteristic) {
Serial.println("Peripheral does not have Y Char");
peripheral.disconnect();
return;
} 
else if (!Accel2Characteristic.canRead()) {
Serial.println("Peripheral does not have a Readable Y Char");
peripheral.disconnect();
return;
}

while (peripheral.connected()) {
// while the peripheral is connected
Accel1Characteristic.readValue(x);
Accel2Characteristic.readValue(y);

Serial.print("Accelerometer X Value : ");
Serial.print(x);
Serial.print(" - Accelerometer Y Value : ");
Serial.println(y);
delay(100);
robotMove();
}
Serial.println("Peripheral disconnected");
}

void robotMove()
{

if((x<=80)&&(y>=81)&&(y<=119))
{
Serial.println("Forward");
forward();
}

else if((x>=120)&&(y>=81)&&(y<=119))
{
Serial.println("Backward");
}
else if((y>=120))
{
Serial.println("Left");
}
else if((y<=80))
{
Serial.println("Right");
}
else
{

Serial.println("Stop");
stop();
}
}


void forward()
{
motor1_speed=100;
motor2_speed=100;
analogWrite(PWM_MOTOR_1, motor1_speed);
analogWrite(PWM_MOTOR_2, motor2_speed);
digitalWrite(MOTOR_A1_PIN, HIGH); 
digitalWrite(MOTOR_B1_PIN, LOW);
digitalWrite(MOTOR_A2_PIN, HIGH); 
digitalWrite(MOTOR_B2_PIN, LOW);
Serial.println("Moving Forward");
}

void stop()
{
motor1_speed=0;
motor2_speed=0;
analogWrite(PWM_MOTOR_1, motor1_speed);
analogWrite(PWM_MOTOR_2, motor2_speed);
digitalWrite(MOTOR_A1_PIN, LOW); 
digitalWrite(MOTOR_B1_PIN, LOW);
digitalWrite(MOTOR_A2_PIN, LOW); 
digitalWrite(MOTOR_B2_PIN, LOW);
Serial.println("Stop");
}

/*
void backward()
{
motor1_speed=100;
motor2_speed=100;
analogWrite(PWM_MOTOR_1, motor1_speed);
analogWrite(PWM_MOTOR_2, motor2_speed);
digitalWrite(2, LOW); 
digitalWrite(3, HIGH);
digitalWrite(7, LOW); 
digitalWrite(8, HIGH);
Serial.println("Moving Forward");
}

void fforward()
{
motor1_speed=255;
motor2_speed=255;
analogWrite(PWM_MOTOR_1, motor1_speed);
analogWrite(PWM_MOTOR_2, motor2_speed);
digitalWrite(MOTOR_A1_PIN, HIGH); 
digitalWrite(MOTOR_B1_PIN, LOW);
digitalWrite(MOTOR_A2_PIN, HIGH); 
digitalWrite(MOTOR_B2_PIN, LOW);
Serial.println("Moving Forward");
}

void fbackward()
{
motor1_speed=255;
motor2_speed=255;
analogWrite(PWM_MOTOR_1, motor1_speed);
analogWrite(PWM_MOTOR_2, motor2_speed);
digitalWrite(MOTOR_A1_PIN, LOW); 
digitalWrite(MOTOR_B1_PIN, HIGH);
digitalWrite(MOTOR_A2_PIN, LOW); 
digitalWrite(MOTOR_B2_PIN, HIGH);
Serial.println("Moving Backward");
}


void ffrontright()
{
motor1_speed=255;
motor2_speed=50;
analogWrite(PWM_MOTOR_1, motor1_speed);
analogWrite(PWM_MOTOR_2, motor2_speed);
digitalWrite(MOTOR_A1_PIN, HIGH); 
digitalWrite(MOTOR_B1_PIN, LOW);
digitalWrite(MOTOR_A2_PIN, HIGH); 
digitalWrite(MOTOR_B2_PIN, HIGH);
Serial.println("Front Right");
}

void ffrontleft()
{
motor1_speed=50;
motor2_speed=255;
analogWrite(PWM_MOTOR_1, motor1_speed);
analogWrite(PWM_MOTOR_2, motor2_speed);
digitalWrite(MOTOR_A1_PIN, HIGH); 
digitalWrite(MOTOR_B1_PIN, HIGH);
digitalWrite(MOTOR_A2_PIN, HIGH); 
digitalWrite(MOTOR_B2_PIN, LOW);
Serial.println("Front Left");
}

void frontright()
{
motor1_speed=150;
motor2_speed=50;
analogWrite(PWM_MOTOR_1, motor1_speed);
analogWrite(PWM_MOTOR_2, motor2_speed);
digitalWrite(MOTOR_A1_PIN, HIGH); 
digitalWrite(MOTOR_B1_PIN, LOW);
digitalWrite(MOTOR_A2_PIN, HIGH); 
digitalWrite(MOTOR_B2_PIN, HIGH);
Serial.println("Front Right");
}

void frontleft()
{
motor1_speed=50;
motor2_speed=150;
analogWrite(PWM_MOTOR_1, motor1_speed);
analogWrite(PWM_MOTOR_2, motor2_speed);
digitalWrite(MOTOR_A1_PIN, HIGH); 
digitalWrite(MOTOR_B1_PIN, HIGH);
digitalWrite(MOTOR_A2_PIN, HIGH); 
digitalWrite(MOTOR_B2_PIN, LOW);
Serial.println("Front Left");
}

void left()
{
motor1_speed=100;
motor2_speed=100;
analogWrite(PWM_MOTOR_1, motor1_speed);
analogWrite(PWM_MOTOR_2, motor2_speed);
digitalWrite(MOTOR_A1_PIN, HIGH); 
digitalWrite(MOTOR_B1_PIN, LOW);
digitalWrite(MOTOR_A2_PIN, LOW); 
digitalWrite(MOTOR_B2_PIN, HIGH);
Serial.println("Moving Left");
}


void right()
{
motor1_speed=100;
motor2_speed=100;
analogWrite(PWM_MOTOR_1, motor1_speed);
analogWrite(PWM_MOTOR_2, motor2_speed);
digitalWrite(MOTOR_A1_PIN, LOW); 
digitalWrite(MOTOR_B1_PIN, HIGH);
digitalWrite(MOTOR_A2_PIN, HIGH); 
digitalWrite(MOTOR_B2_PIN, LOW);
Serial.println("Moving Right");
}

What this code do is, it will search for BLE peripheral device and check if there is any characteristics with the above mentioned UUIDs. If it is present, it will read the broadcasted values and it will be stored in 2 seperate variables. This variables is then used for driving the robot.

Testing

Now power up the transmitting arduino and Robot PCB. Try tilting Arduino Nano 33 BLE Sense. You should see the Robot Move.

Arduino BLE Robot

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *