How I Hacked The mBot Ranger Kit For Autonomous Driving Capabilities

Two weeks ago, I wrote a review of the mBot Ranger kit. It was for the first time when I’ve been playing with a Makeblock kit, and I was impressed. Quality components, a lot of room for further extensions, etc. You can read more about my experience with the kit in the mBot Ranger review. In this post, I’ll continue my experience with the mBot Ranger, and I shared with you how I hacked the mBot Ranger kit to work autonomously using the ultrasonic sensor.

After 30 minutes of working, I assembled all the hardware components of the mBot kit into a nice robot tank. I was very happy, at least until I get into the software side. The kit can be programmed via PC or using an application. Very soon I found that I have some problems with the documentation, and the software version of the smartphone was not up to date because the kit hadn’t been released at that moment. Regarding the tablet version of the software, I don’t own a tablet. (Yes, I don’t want a tablet!)

Luckily, I knew that the brain of the robot is a Me Auriga brick based on Arduino Mega 2560. Being an Arduino fan, I know what to do to move forward.

The plan was to search in the libraries and find the pins to control the robot motors using the Arduino sketch. After a few minutes of research, I found the pins that I needed to control the two DC motors. Working with the ultrasonic sensor was much easier. I use the Makeblock library to read the distance from it.

The idea was to program the robot tank to detect an obstacle, and when the sensor detects one or more obstacles, the robot turns right until the sensor no longer detects any obstacle.

Here is the Arduino sketch and the result of my work with the mBot Ranger kit.

/**
 * @file         mBot Ranger Autonomous Mode.ino
 * @author       IntoRobotics.com
 * @version      V1.1
 * @date         2016/05/13
 * @description  this file is sample code for the mBot Ranger kit
 */
 
#include "MeAuriga.h"
#include <SoftwareSerial.h>

MeUltrasonicSensor ultraSensor(PORT_7); // Ultrasonic module

int distanceLimit=50;    // Maximum distance we want to ping for (in centimeters).
int maxPwm=255;
int halfPwm=200;
int turnPwm=150;
int d=500;
int frontDistance;


//Motor Left
const int pwmMotor1 = 11;
const int inMotor1_1 = 49;
const int inMotor1_2 = 48;

//Motor Right
const int pwmMotor2 = 10;
const int inMotor2_1 = 47;
const int inMotor2_2 = 46;

void setup()
{
  Serial.begin(9600);
 
  pinMode(pwmMotor1,OUTPUT);    //We have to set PWM pin as output
  pinMode(inMotor1_1,OUTPUT);  //Logic pins are also set as output
  pinMode(inMotor1_2,OUTPUT);
  pinMode(pwmMotor2,OUTPUT);    //We have to set PWM pin as output
  pinMode(inMotor2_1,OUTPUT);  //Logic pins are also set as output
  pinMode(inMotor2_2,OUTPUT);
 
}

void loop()
{
   GetFrontDistance();  //Get the ultrasonic distance in cm
 
   if(frontDistance>=distanceLimit){
      FullSpeedMode();  //The robot is in full speed mode
    }

   else {  
      ReduceSpeed();   //Reduce the speed of the robot
      Stop();          //Stop the robot
      do {  
          TurnRight();
          delay(200);  // Wait for sensor to stabilize
          GetFrontDistance();  //Get the ultrasonic distance in cm  
          } while(frontDistance<distanceLimit);
      }
}

 int GetFrontDistance(){
   frontDistance=ultraSensor.distanceCm();
   Serial.print("Front distance (in cm) is: "); //For debugging
   Serial.println(frontDistance);
   return frontDistance;
   }

 void FullSpeedMode(){
    digitalWrite(inMotor1_1, HIGH);
    digitalWrite(inMotor1_2, LOW);
    analogWrite(pwmMotor1,maxPwm);
    digitalWrite(inMotor2_1, LOW);
    digitalWrite(inMotor2_2, HIGH);
    analogWrite(pwmMotor2,maxPwm);
    Serial.println("Full speed mode"); //For debugging
   }

  void ReduceSpeed(){
    digitalWrite(inMotor1_1, HIGH);
    digitalWrite(inMotor1_2, LOW);
    analogWrite(pwmMotor1,halfPwm);//Set speed via PWM
    digitalWrite(inMotor2_1, LOW);
    digitalWrite(inMotor2_2, HIGH);
    analogWrite(pwmMotor2,halfPwm);//Set speed via PWM
    Serial.println("Reduce the speed"); //For debugging
    delay(d);
    }

  void Stop(){
    analogWrite(pwmMotor1, 0);
    analogWrite(pwmMotor2, 0);
    Serial.println("Stop");
    delay(d);
   }
 
   void TurnRight(){
    digitalWrite(inMotor1_1, HIGH);
    digitalWrite(inMotor1_2, LOW);
    analogWrite(pwmMotor1,turnPwm);//Set speed via PWM
    digitalWrite(inMotor2_1, HIGH);
    digitalWrite(inMotor2_2, LOW);
    analogWrite(pwmMotor2,turnPwm);//Set speed via PWM
    Serial.println("Turn the robot right"); //For debugging
    }

Makeblock mBot Ranger Kit Autonomous Mode

9 comments » Write a comment

  1. Hi!

    I have read this blog of yours and I am extremely impressed. I am trying to do something similar with mBot Ranger. Could you please share with me how did you figure out the pins to control the robot motors using the Arduino sketch?

    Kind Regards.

      • Hi!

        Thank you for your prompt reply. I think I may not have been very clear with my question. Following is attached a snippet of the code:
        //Motor Left
        const int pwmMotor1 = 11;
        const int inMotor1_1 = 49;
        const int inMotor1_2 = 48;

        //Motor Right
        const int pwmMotor2 = 10;
        const int inMotor2_1 = 47;
        const int inMotor2_2 = 46;

        In this snippet, you are initialising the pins. My question is: how did you figure out which pins the motor connects to on the MeAuriga board?

        I hope I am making more sense now. Thank you once again.

        • Hi,

          Well, now I understood the question 🙂
          I looked into the libraries. There you can find many more details about the pins, etc.

          I already write that in the article: “The plan was to search in the libraries and find the pins to control the robot motors using the Arduino sketch.”

  2. Hi!

    I ran this code and I am having the strangest problem. I thought to ask you since you have quite some experience working with mBot Ranger and arduino. When I uploaded this code to the Auriga board and the bot was still thethered to the computer, it was working brillaintly. However, when I took the cable off, as soon as the ultrasonic sensor sensed the wall, the motors would move a little and then stop and continue to do this. Turning the mBot off and turning it on again made no difference. I changed the batteries as well but the problem persisted. Could you tell me what I am doing wrong?

    Kind Regards

  3. Hello,

    I’m trying to simply make my motors move on the mBot ranger. I’ve successfully read stuff from sensors, by reading the firmware. However I’m stuck with the motors: I’ve tried many things – including your example above – but alas 🙁 nothing moves.

    The code executes fine, no errors. I think the pin numbers might not be the right for my board. How exactly did you find the pins for your board ? When i look at the firmware code for mBot, I see things like SLOT_1 and SLOT_2, not hard-coded numbers.
    Any idea on how to make sure i got the pins right ?

Leave a Reply to Josh Cancel reply

Required fields are marked *.