Ultrasonic-Servo Motor

ICT 361: Introduction to Robotics

Mr. Seng Theara

Learning Objective

By the end of the lesson, student will be able to:

  • Understand how a joystick works electrically

  • Explain pull-up button logic

  • Read analog and digital signals

  • Map controller inputs to robot movements

  • Design basic control logic for robots

Learning Objective

By the end of the lesson, student will be able to:

  • Explain how ultrasonic distance sensing works

  • Calculate distance from echo time

  • Control a servo motor using PWM

  • Combine ultrasonic + servo for automation

  • Debug common timing and signal problems

4 Buttons

Joystick

Ultrasonic Sensor

4 Buttons

Joystick

Ultrasonic Sensor

  • Trigger pin(output) send ultrasonic sound signal to the object
  • Echo pin(Input) wait for ultrasound to reflect back from the object

Connection 

Ultrasonic Sensor

Expansion Board or External Shield

Ultrasonic refers to sound waves with frequencies that exceed the range of what human ears can hear. 

An ultrasonic sensor is an electronic device used to measure distance by sending out high-frequency sound waves (ultrasound) and calculating how long it takes for the sound to bounce back from an object.

Distance Formula

\text{Distance} = \frac{t \times v}{2}

Where

\begin{aligned} t &= \text{time of flight (seconds)} \\ v &= \text{speed of sound in air} \approx 343\,\text{m/s} \end{aligned}

Divide by 2 because the sound travels to the object and back

How Do We Start the Ultrasonic Sensor?

 

The ultrasonic sensor does nothing until we send a trigger pulse.

To start measurement:

  • Set TRIG = LOW (2µs)

  • Set TRIG = HIGH (10µs)

  • Set TRIG = LOW again

After trigger:

  • Sensor sends 8 ultrasonic pulses at 40 kHz

  • Sound travels forward

  • Sound reflects from object

  • ECHO pin goes HIGH

  • ECHO stays HIGH until wave returns

 Coding

#define TRIG_PIN 13
#define ECHO_PIN 39

long duration;
float distance;

void setup() {
  Serial.begin(115200);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
}

void loop() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  duration = pulseIn(ECHO_PIN, HIGH, 30000);
  if (duration == 0) {
    Serial.println("No echo");
  } else {
    distance = (duration * 0.0343) / 2.0;
    Serial.print(distance);
    Serial.println(" cm");
  }

  delay(500);
}

 Coding & Testing

#define TRIG_PIN 13
#define ECHO_PIN 39

long duration;
float distance;

void setup() {
  Serial.begin(115200);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
}

void loop() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  duration = pulseIn(ECHO_PIN, HIGH, 30000);
  if (duration == 0) {
    Serial.println("No echo");
  } else {
    distance = (duration * 0.0343) / 2.0;
    Serial.print(distance);
    Serial.println(" cm");
  }

  delay(500);
}

4 Buttons

Joystick

Servo Motor

4 Buttons

Joystick

Introduction to Servo Motor

A servo motor is a precision motor that uses a servo motor working principle based on closed-loop feedback control, which can rotate to specific angles with exceptional accuracy

If you want to rotate an object to specific angles or distances, servo motors are the ideal choice due to their servo mechanism with built-in position feedback.

Servo motors are usually rated in kg/cm. For example, a 6kg/cm servo can lift 6kg when the load is 1cm from the motor shaft.

4 Buttons

Joystick

How to control Servo Motor

PWM Control Signal Specifications

  • Pulse Period: 20ms (50Hz frequency)

  • 0.5 ms pulse width: Servo rotates to 0° position

  • 1.5ms pulse width: Servo moves to 90° (neutral position)

  • 2.5ms pulse width: Servo rotates to 180° position

Connection

Library Install

Go to Library Manager and Install ESP32 Servo 

Coding

#include <ESP32Servo.h>

Servo myServo;

void setup() {
  myServo.attach(17);
}

void loop() {
  myServo.write(0);
  delay(1000);

  myServo.write(90);
  delay(1000);

  myServo.write(180);
  delay(1000);
}

From this code, you will see your servo moves from 0 degree to 90 degree and then 180 degree