Mr. Seng Theara
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
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
4 Buttons
Joystick
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
Where
Divide by 2 because the sound travels to the object and back
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
#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);
}#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
4 Buttons
Joystick
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
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
Go to Library Manager and Install ESP32 Servo
#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