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

Arduino IDE Code

This document contains an Arduino code for measuring distance using an ultrasonic sensor. It initializes the sensor's trigger and echo pins, calculates the distance based on the time taken for the ultrasonic pulse to return, and prints the distance to the serial monitor every second. The code includes setup and loop functions to handle the sensor readings and output.

Uploaded by

help.tejasvir
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Arduino IDE Code

This document contains an Arduino code for measuring distance using an ultrasonic sensor. It initializes the sensor's trigger and echo pins, calculates the distance based on the time taken for the ultrasonic pulse to return, and prints the distance to the serial monitor every second. The code includes setup and loop functions to handle the sensor readings and output.

Uploaded by

help.tejasvir
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

const int trig = 2; //Trigger pin of ultrasonic Sesnor

const int echo = 3; //Echo pin of ultrasonic Sesnor


long time_taken;
int dist,distance;

void setup()
{
Serial.begin(9600); //tells the arduino to get ready to exchange messages with
the serial monitor at a data rate of 9600 bits/sec.
pinMode(trig, OUTPUT); //configures the specified pin to behave as output/input.
pinMode(echo, INPUT);
}

/*###Function to calculate distance###*/


void calculate_distance(int trigger, int echo)
{
digitalWrite(trigger, LOW);
delayMicroseconds(2);
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);

time_taken = pulseIn(echo, HIGH);


dist= time_taken*0.034/2;

void loop()
{
calculate_distance(trig, echo);
distance=dist; //getting distance of ultrasonic sensor
Serial.println(distance);
delay(1000);
}

You might also like