vinay y.n
Published © GPL3+

BLE Robot Car Using Realtek Ameba RTL8722DM (AMB21) Board

Here I am showcasing BLE Robot Car Controlling With Bluefruit Connect App.

IntermediateProtip2 hours686
BLE Robot Car Using Realtek Ameba RTL8722DM (AMB21) Board

Things used in this project

Hardware components

Ameba RTL8722DM IoT Development Board
×1
Geared DC Motor, 12 V
Geared DC Motor, 12 V
×4
Dual H-Bridge motor drivers L298
SparkFun Dual H-Bridge motor drivers L298
×1
Rechargeable Battery, 12 V
Rechargeable Battery, 12 V
×1
Wheel 10cm Diameter
×4
chasis
×1
Toggle Switch, (On)-Off-(On)
Toggle Switch, (On)-Off-(On)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Adafruit Bluefruit Connect

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free
Solder Flux, Soldering
Solder Flux, Soldering
Multitool, Screwdriver
Multitool, Screwdriver
Tape, Double Sided
Tape, Double Sided

Story

Read more

Schematics

Connection diagram

Code

Code

Arduino
The code is based on C
#include "BLEDevice.h"
const int ml1 = 3; //Motor Driver IN1
const int ml2 = 4; //Motor Driver IN2
const int mr1 = 5; //Motor Driver IN3
const int mr2 = 6; //Motor Driver IN4

#define UART_SERVICE_UUID      "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"

#define STRING_BUF_SIZE 1000

BLEService UartService(UART_SERVICE_UUID);
BLECharacteristic Rx(CHARACTERISTIC_UUID_RX);
BLECharacteristic Tx(CHARACTERISTIC_UUID_TX);
BLEAdvertData advdata;
BLEAdvertData scndata;
bool notify = false;

void readCB (BLECharacteristic* chr, uint8_t connID) {
  printf("Characteristic %s read by connection %d \n", chr->getUUID().str(), connID);
}

void writeCB (BLECharacteristic* chr, uint8_t connID) {
  printf("Characteristic %s write by connection %d :\n", chr->getUUID().str(), connID);
  uint16_t datalen = chr->getDataLen();
  if (datalen > 0) {
    if (chr->readData8() == '!') {
      uint8_t command[datalen];
      chr->getData(command, datalen);
      
 // " B"  IS PREFIX TO IDENTIFY THAT RECEIVED DATA IS CONTROL PAD DATA FROM THE BLUEFRUIT APP    
 
      if (command[1] == 'B') 
      { 
        Serial.print("Command 2:");
        Serial.print(command[2]);
        Serial.println();
        Serial.print("Command 3:");
        Serial.print(command[3]);
        Serial.println();
        Serial.print("Command 4:");
        Serial.print(command[4]);
        Serial.println();
        
        if((command[4] == 54) && (command[3] == 49)) //FORWARD COMMAND
        {
          Serial.println("forward");
          digitalWrite(ml1, HIGH);
          digitalWrite(ml2,LOW);
          digitalWrite(mr1,HIGH);
          digitalWrite(mr2,LOW);
        }
         if((command[4] == 55) && (command[3] == 48))//STOP COMMAND 
        {
          Serial.println("STOP");
          digitalWrite(ml1, HIGH);
          digitalWrite(ml2,HIGH);
          digitalWrite(mr1,HIGH);
          digitalWrite(mr2,HIGH);
        }

       
      if((command[4] == 53) && (command[3] == 49))//REVERSE COMMAND
        {
          Serial.println("Reverse");
          digitalWrite(ml1, LOW);//REVERS
          digitalWrite(ml2,HIGH);
          digitalWrite(mr1,LOW);
          digitalWrite(mr2,HIGH);
        }
if((command[4] == 54) && (command[3] == 48))//STOP COMMAND 
        {
          Serial.println("STOP");
          digitalWrite(ml1, HIGH);
          digitalWrite(ml2,HIGH);
          digitalWrite(mr1,HIGH);
          digitalWrite(mr2,HIGH);
        }

      
         if((command[4] == 52) && (command[3] == 49))//LEFT COMMAND
        { 
          Serial.println("LEFT");
          digitalWrite(ml1, HIGH);//LEFT
          digitalWrite(ml2,LOW);
          digitalWrite(mr1,HIGH);
          digitalWrite(mr2,HIGH);
        }
          if((command[4] == 53) && (command[3] == 48))//STOP COMMAND 
        { 
          Serial.println("STOP");
          digitalWrite(ml1,HIGH);
          digitalWrite(ml2,HIGH);
          digitalWrite(mr1,HIGH);
          digitalWrite(mr2,HIGH);
        }

        
         if((command[4] == 51) && (command[3] == 49))//RIGHT COMMAND
        {
          Serial.println("RIGHT");
          digitalWrite(ml1, HIGH);
          digitalWrite(ml2,HIGH);
          digitalWrite(mr1,HIGH);
          digitalWrite(mr2,LOW);
        }
        if((command[4] == 52) && (command[3] == 48))//STOP COMMAND 
        {
          Serial.println("STOP");
          digitalWrite(ml1, HIGH);
          digitalWrite(ml2,HIGH);
          digitalWrite(mr1,HIGH);
          digitalWrite(mr2,HIGH);
        }
        
delay(100);
      }
    } else {
                  Serial.print("Received string: ");
                  Serial.print(chr->readString());
                  Serial.println();
    }
  }

void notifCB (BLECharacteristic* chr, uint8_t connID, uint16_t cccd) {
  if (cccd & GATT_CLIENT_CHAR_CONFIG_NOTIFY) {
    printf("Notifications enabled on Characteristic %s for connection %d \n", chr->getUUID().str(), connID);
    notify = true;
  } else {
    printf("Notifications disabled on Characteristic %s for connection %d \n", chr->getUUID().str(), connID);
    notify = false;
  }
}

void setup() {
  Serial.begin(115200);
  pinMode(ml1, OUTPUT);
  pinMode(ml2,OUTPUT);
  pinMode(mr1, OUTPUT);
  pinMode(mr2,OUTPUT);
  advdata.addFlags(GAP_ADTYPE_FLAGS_LIMITED | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED);
  advdata.addCompleteName("AMEBA_BLE_DEV");
  scndata.addCompleteServices(BLEUUID(UART_SERVICE_UUID));

  Rx.setWriteProperty(true);
  Rx.setWriteCallback(writeCB);
  Rx.setBufferLen(STRING_BUF_SIZE);
  Tx.setReadProperty(true);
  Tx.setReadCallback(readCB);
  Tx.setNotifyProperty(true);
  Tx.setCCCDCallback(notifCB);
  Tx.setBufferLen(STRING_BUF_SIZE);

  UartService.addCharacteristic(Rx);
  UartService.addCharacteristic(Tx);

  BLE.init();
  BLE.configAdvert()->setAdvData(advdata);
  BLE.configAdvert()->setScanRspData(scndata);
  BLE.configServer(1);
  BLE.addService(UartService);

  BLE.beginPeripheral();
}

void loop() {
  if (Serial.available()) {
    Tx.writeString(Serial.readString());
    if (BLE.connected(0) && notify) {
      Tx.notify(0);
    }
  }
  delay(100);
}

Credits

vinay y.n

vinay y.n

26 projects • 41 followers
An electronic product engineer with 8 years of experience in the field. The passion for electronics began as a hobby 11 years ago.

Comments