LEARN ULTRASONIC AND SERVO CIRCUIT USING TINKERCAD

by marcosan in Circuits > Arduino

486 Views, 2 Favorites, 0 Comments

LEARN ULTRASONIC AND SERVO CIRCUIT USING TINKERCAD

Screenshot 2022-09-07 223412.png

go nuts and learn about ultrasonic and servo

Supplies

All you need is a computer and internet connection for this tutorial

Screenshot 2022-09-07 214809.png
Screenshot 2022-09-07 214940.png

On the home page click on new and select circuit.

Screenshot 2022-09-07 215035.png

now you are here on the workspace. Use the search bar to search for the components(breadboard,arduino,ultrasonic,servo) just hold and drag it

1.png
Screenshot 2022-09-07 224152.png
Screenshot 2022-09-07 224203.png

both servo and ultrasonic has 3 pins the 2 pins ins for the ground and power the third is for signal connect it to the arduino any number will work.

CODE:

#include <Servo.h>


int distance = 0;


Servo servo_3;


long readUltrasonicDistance(int triggerPin, int echoPin)

{

  pinMode(triggerPin, OUTPUT);  // Clear the trigger

  digitalWrite(triggerPin, LOW);

  delayMicroseconds(2);

  // Sets the trigger pin to HIGH state for 10 microseconds

  digitalWrite(triggerPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(triggerPin, LOW);

  pinMode(echoPin, INPUT);

  // Reads the echo pin, and returns the sound wave travel time in microseconds

  return pulseIn(echoPin, HIGH);

}


void setup()

{

  servo_3.attach(3, 500, 2500);

}


void loop()

{

  servo_3.write(180);

  distance = 0.01723 * readUltrasonicDistance(4, 4);

  if (distance <= 100) {

    servo_3.write(90);

    delay(3000); // Wait for 3000 millisecond(s)

    servo_3.write(180);

  }

  servo_3.write(180);

}



Screenshot 2022-09-07 224822.png

logic:

the servo position at 180 degrees once the ultrasonic sensor distance is greater than equal to 100 degrees the servo position will change to 90 degrees and after for 3 sec it will go back to 180 degrees