InoLab : Ultrasonic sensor + Motor

InoLab : Ultrasonic sensor + Motor © GPL3+

This project was made by Innovation Lab at Bongilcheon High School.

  • 17 views
  • 0 comments
  • 0 respects

Components and supplies

About this project

This is to move the motor with the ultrasonic sensor. When the ultrasonic sensor detects an object, motor moving.

Code

Untitled fileArduino
const int trigPin = 3;
const int echoPin = 4;
long distance;
long duration;
int motor_A = 5; 
int motor_B = 6; 
void setup()
{
  pinMode(motor_A,OUTPUT);
  pinMode(motor_B,OUTPUT);
  pinMode(2,OUTPUT);
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT);
}

void loop()
{
ultra();
motor();
}

void motor(){
 if(distance<30){
  analogWrite(2,255);
  digitalWrite(motor_A,HIGH);
  digitalWrite(motor_B,LOW);
   
 }
 else{
  analogWrite(2,255);
  digitalWrite(motor_A,HIGH);
  digitalWrite(motor_B,HIGH);
 }
}

void ultra(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
}

Schematics

__whfxRqYKNb.png
  whfxrqyknb

Comments

Add projectSign up / Login