wiki:ControlSystems/Electrical/Training/FallTraining/ProjectSeven

Materials Needed

  1. For this project, you will need to use TinkerCAD since your Arduino kits do not have motors available.
  1. You will need a motor, an Arduino, a dark blue ultrasonic sensor with 4 pins, and a breadboard.

Steps

  1. Let's start by putting down a breadboard to hold the other components together.
  1. Then, take out an ultrasonic sensor (the dark blue one with 4 pins on the bottom) and place it so that the 4 pins touch 4 of the middle columns of holes as seen in the picture.
  1. Next, take out a motor, and connect the black pin to the row of holes next to the black line.
  1. Then, connect the row of holes next to the black line to the GND of the Arduino, and connect the rows of holes next to the red line to the 5v of the Arduino.
  1. Connect the red pin of the motor to digital pin 4.
  1. After that, connect the column of holes connected to the Vcc pin of the ultrasonic sensor to the row of holes next to the red line.
  1. Then, connect the column of holes connected to the GND of the ultrasonic sensor to the row of holes next to the black line.
  1. Next, connect the column of holes connected to the TRIG pin of the ultrasonic sensor to digital pin 3.
  1. Then, connect the column of holes connected to the ECHO pin of the ultrasonic sensor to digital pin 2.
  1. Finally, copy and paste the code below into the TinkerCAD code area with the code option set to text instead of blocks.
  1. Play the simulation, press the ultrasonic sensor, and move the circle around and see what happens (particularly with the amount of distance and how that makes the motor spin or turn off).

Code:

const int TRIG = 3;

const int ECHO = 2;

const int MOTOR = 4;

float distance, time;

void setup()

{

pinMode(TRIG, OUTPUT);

pinMode(ECHO, INPUT);

pinMode(MOTOR, OUTPUT);

Serial.begin(9600);

}

void loop()

{

digitalWrite(TRIG, LOW);

delayMicroseconds(2);

digitalWrite(TRIG, HIGH);

delayMicroseconds(10);

digitalWrite(TRIG, LOW);

time = pulseIn(ECHO, HIGH);

distance = (0.0343*time/2);

Serial.println(distance);

if (distance <= 100){

digitalWrite(MOTOR, LOW);

}

else if (distance > 100){

digitalWrite(MOTOR, HIGH);

}

}

Explanation of how it works

  1. This project will help you understand how the ultrasonic sensor works, which you will be using in your projects during the meetings.
  1. The ultrasonic sensor sends out an ultrasonic wave whenever the digital pin that connects to the TRIG pin provides a voltage (electronic signal) which happens due to the code.
  1. When the ultrasonic wave reflects off an object and hits the receiver of the ultrasonic sensor, the ultrasonic sensor sends a signal to the Arduino’s digital pin that the ECHO pin is connected to.
  1. The Arduino takes the time it took for the ultrasonic wave to be sent out, and received, and divides that in half (to get the amount of time to the object, but not the return trip).
  1. The Arduino can then use this time and multiplies it by the speed of sound in centimeters per microsecond to get the distance.
  1. The Arduino will turn on the motor if the distance is greater than 100 centimeters, and will turn the motor off if it is less or equal to than 100 centimeters.
Last modified 4 months ago Last modified on Jan 9, 2021, 3:53:24 PM

Attachments (1)

Download all attachments as: .zip