0% found this document useful (0 votes)
3 views2 pages

Arduino Code

This Arduino code measures the speed of sound using an ultrasonic sensor. It sends a trigger signal and calculates the ping time based on the echo received. If an echo is detected, it computes and displays the speed of sound in kilometers per hour; otherwise, it indicates no echo was detected.

Uploaded by

unasheshoniwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Arduino Code

This Arduino code measures the speed of sound using an ultrasonic sensor. It sends a trigger signal and calculates the ping time based on the echo received. If an echo is detected, it computes and displays the speed of sound in kilometers per hour; otherwise, it indicates no echo was detected.

Uploaded by

unasheshoniwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Arduino Code

int trigPin = 13;

int echoPin = 11;

float pingTime;

float speedOfSound;

float targetDistance = 16; // cm

void setup() {

Serial.begin(9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

void loop() {

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

pingTime = pulseIn(echoPin, HIGH);

Serial.print("Ping time: ");

Serial.println(pingTime);
if (pingTime > 0) {

speedOfSound = 2 * targetDistance / pingTime; // cm/us

speedOfSound = speedOfSound * 1000000 * 3600 / 100000; // km/h

Serial.print("The speed of sound is: ");

Serial.print(speedOfSound);

Serial.println(" kilometers per hour");

} else {

Serial.println("No echo detected!");

delay(1000); // 1 second pause

You might also like