edge lab LIVE! starts Tuesday in Munich. Grab a workshop seat and take home the board you build on. Code FRIEND-HACKSTER saves 20%. () Learn more!
coloitu_
Published © CC BY-NC-ND

Ultrasonic Distance Sensor

Measure distances from your smartphone!

BeginnerFull instructions provided373
Ultrasonic Distance Sensor

Things used in this project

Hardware components

SparkFun Electronics Arduino Pro Micro
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free

Story

Read more

Custom parts and enclosures

Enclosure_Part1

Enclosure_Part2

Schematics

Schematic

Code

Code

Arduino
/*
*
*
*/
#include <Keyboard.h>
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
return pulseIn(echoPin, HIGH);
}
void setup() {
Serial.begin(9600);
Keyboard.begin();
}
void loop() {
Serial.println(0.01723 * readUltrasonicDistance(3, 5));
Keyboard.print(0.01723 * readUltrasonicDistance(3, 5));
Keyboard.press(0x20);
Keyboard.releaseAll();
delay(500);
}

Credits

coloitu_
19 projects • 4 followers

Comments