Obstacle Avoidance Robot (Pinky) – Part 3

Part 1 | Part 2 | Part 3 | Part 4 | Part 5

Ultrasonic sensor (HC-SR04) is the most important part in an obstacle avoidance robot. Before we can used the HC-SR04, we need to include the library. To do so, go to the Arduino IDE and click Sketch > Include Library > Manage Libraries and then search SR04 from gamegine.

#include <PWMServo.h>
#include <HCSR04.h>

HCSR04 hc(4,7);    // initialisation class HCSR04 (trig pin , echo pin)

PWMServo myservo;  // create servo object to control a servo
float distance;

void setup() {
  myservo.attach(SERVO_PIN_A);  // attaches the servo on pin 9 to the servo object
  Serial.println("Test Ultrasonic + Servo Fix 90 Degree");

void loop() {
  distance = hc.dist();

Obstacle Avoidance Robot (Pinky) – Part 2

Part 1 | Part 2 | Part 3 | Part 4 | Part 5

For an obstacle avoidance robot using an ultrasonic sensor (one sensor only), we can use a servo motor to do a rotation in angle. To do the rotation, we need a servo library. In Arduino IDE, there is a built-in function for servo, however, for the first time used, the library always reset my Maker Uno (I do not why! 🙂 ). Therefore, I have installed a new library (PWMServo). To do so, go to the Arduino IDE and click Sketch > Include Library > Manage Libraries and then search PWMServo.

//   Board                     SERVO_PIN_A   SERVO_PIN_B   SERVO_PIN_C
//   -----                     -----------   -----------   -----------
//   Arduino Uno, Duemilanove       9            10          (none)
//   Arduino Mega                  11            12            13
//   Sanguino                      13            12          (none)
//   Teensy 1.0                    17            18            15
//   Teensy 2.0                    14            15             4
//   Teensy++ 1.0 or 2.0           25            26            27
//   Teensy LC & 3.x                 (all PWM pins are usable)
#include <PWMServo.h>
#include <HCSR04.h>

PWMServo myservo;  // create servo object to control a servo

void setup() {
  myservo.attach(SERVO_PIN_A);  // attaches the servo on pin 9 to the servo object
  Serial.println("Test Servo");
  myservo.write(90);            // Initial Position

void loop() {
  myservo.write(90);            //90 
  myservo.write(45);            //45 
  myservo.write(0);             //0 
  myservo.write(45);            //45 
  myservo.write(90);            //90 
  myservo.write(135);           //135 
  myservo.write(180);           //180 
  myservo.write(135);           //135 

Obstacle Avoidance Robot (Pinky) – Part 1

Part 1 | Part 2 | Part 3 | Part 4 | Part 5

In robotics, obstacle avoidance is the task of satisfying some control objective subject to non-intersection or non-collision position constraints or in other words, an Obstacle Avoidance Robot is an intelligent robot, which can automatically sense and overcome obstacles on its path.

Before we can do an obstacle avoidance robot, we need a brain (microcontroller), robot chassis, dc motor, dc motor driver and ultrasonic sensor/infrared. However, a few things we need is a jumper wire, toggle switch (to turn ON/OFF battery) and battery.


Maker Uno, an Arduino UNO compatible board designed and developed specially for students/hobbyist to learn coding and microcontroller. The best part of Maker Uno is the built-in LEDs as an indicator, a buzzer (Pin 8) and onboard programmable button (Pin 2).

Maker Uno

Robot Chassis (Including dc motor and caster)

Aluminum Chassis in Blinka purple makes for an excellent robot body. It’s a single piece of bent 2mm thick aluminum, with a variety of holes, slots and cutouts for mounting boards, sensors, and servos. The sides are notched to fit two DC Gear ‘TT’ motors perfectly.

Motor Driver

Maker Drive is a simplifying H-Bridge motor driver for beginner.

Ultrasonic Sensor

HC-SR04 is a sensor for distance measurement and object detection.
Ultrasonic sensor is created based on the principle of echolocation used by animals like bats and dolphins. Since ultrasonic sensor uses sonar to determine the distance to an object, its operation is not affected by sunlight, spotlight and object’s surface color which will affect the readings of any infrared distance sensors. However acoustically soft materials such as clothes can be difficult to detect.

Lastly, for improvement for future development, we can add a servo motor (will be discussed in Part 2) for ultrasonic rotation. Tower Pro SG90 micro servo motor is a low-cost plastic gear RC servo with 1.80kg.cm holding torque (at 4.8V). It is a perfect solution for robotic projects.

Therefore, this is a common electronic and hardware parts for developing an obstacle avoidance robot. The name of the robot is Pinky! :).


micro:bit is a tiny programmable computer, designed to make learning and teaching easy and fun!. micro:bit is a handheld, programmable micro-computer that can be used for all sorts of cool creations, from robots to musical instruments – the possibilities are endless.


Classifier Comparison: Rescale Data

Data transforms is part of machine learning process. Different algorithm/classifier will make different assumptions of raw data and it may require different view of data. Code below use a MinMaxScaler method from Scikit-learn. The comparison without data transformation method can be found here:> https://norasmadi.unimap.edu.my/2019/03/10/classifier-comparison/

# Run Algorithms for n-Times and Determine the Average Value based on Kfold Method
# Data Transformation Method: Rescale
from pandas import read_csv
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
from sklearn.utils import shuffle
from sklearn.preprocessing import MinMaxScaler

# load dataset
url = "https://bit.ly/2GX9wC5"
names = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
dataframe = read_csv(url, names=names)

# For plotting
fig, ax = plt.subplots()

# prepare models
model1 = LogisticRegression(solver = 'lbfgs',max_iter=1000) #LR
model2 = LinearDiscriminantAnalysis() #LDA
model3 = KNeighborsClassifier(n_neighbors = 3) #KNN
model4 = GaussianNB() #GNB
model5 = DecisionTreeClassifier(max_depth=5) #DT
model6 = QuadraticDiscriminantAnalysis() #QDA

# evaluate model
resultslr = []
resultslda = []
resultsknn = []
resultsgnb = []
resultsdt = []
resultsqda = []

scoring = 'accuracy'
times = 10             # How many times to repeat

for x in range (times):
    # shuffle the data for each times
    dataframe = shuffle(dataframe)
    array = dataframe.values
    X = array[:,0:8]
    Y = array[:,8]
    # Rescale Data
    scaler = MinMaxScaler(feature_range=(0,1))
    rescaledX = scaler.fit_transform(X)
    kfold = KFold(n_splits=10)
    # Logic Regression
    cv_results = cross_val_score(model1, rescaledX, Y, cv=kfold, scoring=scoring)
    # Linear Discriminant Analysis
    cv_results = cross_val_score(model2, rescaledX, Y, cv=kfold, scoring=scoring)

    # K-Nearest Neighbor
    cv_results = cross_val_score(model3, rescaledX, Y, cv=kfold, scoring=scoring)

    # Gaussian Naive Bayes
    cv_results = cross_val_score(model4, rescaledX, Y, cv=kfold, scoring=scoring)

    # Decision Tree
    cv_results = cross_val_score(model5, rescaledX, Y, cv=kfold, scoring=scoring)

    # Quadratic Discriminant Analysis
    cv_results = cross_val_score(model6, rescaledX, Y, cv=kfold, scoring=scoring)

# Plot the result
t = np.arange(1, times+1, 1)        # to plot from 1 to n-Times

ax.set(xlabel='times', ylabel='Accuracy (%)',
       title='Pima Indian Database')
ax.set_xlim(1, times)
ax.set_ylim(60, 80)
ax.legend(['Logic Regression','Linear Discriminant Analysis','K-Nearest Neighbor','Gaussian Naive Bayes','Decision Tree','Quadratic Discriminant Analysis'])