0% found this document useful (0 votes)
50 views

Adriano Program For Ultrasonic Sensor With Motor

This Arduino sketch uses an ultrasonic sensor to measure distance and control a motor. It defines pins for the sensor's trigger, echo, and a motor. In setup, the pins are configured as outputs or inputs. The main loop uses the sensor to get distance measurements. If an object is closer than 20cm, the motor turns on, otherwise it turns off. Distances are also printed to the serial monitor.

Uploaded by

SirajChabru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Adriano Program For Ultrasonic Sensor With Motor

This Arduino sketch uses an ultrasonic sensor to measure distance and control a motor. It defines pins for the sensor's trigger, echo, and a motor. In setup, the pins are configured as outputs or inputs. The main loop uses the sensor to get distance measurements. If an object is closer than 20cm, the motor turns on, otherwise it turns off. Distances are also printed to the serial monitor.

Uploaded by

SirajChabru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Arduino program for ultrasonic sensor with motor

#define trigPin 13

#define echoPin 12

#define motorPin 8

void setup() {

Serial.begin (9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(motorPin, INPUT);

void loop () {

int duration, distance;

digitalWrite (trigPin, HIGH);

delayMicroseconds (1000);

digitalWrite (trigPin, LOW);

duration = pulseIn (echoPin, HIGH);

distance = (duration/2) / 29.1;

if (distance < 20) { // Distance from sensor

digitalWrite (motorPin, HIGH); // When in range, motor should start high

else {

digitalWrite(motorPin, LOW);

if (distance > 20) { // Distance from sensor

Serial.println("Out of range");

else {

Serial.print(distance);

Serial.println(" cm");
}

delay(500);

You might also like