Ultrasonic Sensor with Angular Servo Motor

Introduction

In this example, the angular servo motor moves based on input from the ultrasonic distance sensor. The measured distance is mapped to its corresponding angle, which is then used to position the servo.

Circuit Assembly

Connect the ultrasonic sensor and servo motor to any digital pin. In our example, we're using D11/D10 for the ultrasonic sensor and D3/D2 for the angular servo motor.

The pin that you connect your ultrasonic sensor to corresponds to a particular echo/trig value in your code. In our case, the trig is 11 and echo is 10. You can remix yours to look like this:
• D11/D10 → ultra_trig = 11, ultra_echo = 10;
• D9/D8 → ultra_trig = 9, ultra_echo = 8;
• D7/D6 → ultra_trig = 7, ultra_echo = 6;
• D5/D4 → ultra_trig = 5, ultra_echo = 4;
• D3/D2 → ultra_trig = 3, ultra_echo = 2;
• D1/D0 → ultra_trig = 1, ultra_echo = 0;

The pin that you connect your angular servo with corresponds to a pin value on your code. We're using D3/D2, which corresponds to 3. You can remix yours to look like this:
• D11/D10 → servo_pin = 11;
• D9/D8 → servo_pin = 9;
• D7/D6 → servo_pin = 7;
• D5/D4 → servo_pin = 5;
• D3/D2 → servo_pin = 3;
• D1/D0 → servo_pin = 1;

Code

Related