Hackster is hosting Hackster Cafe: Open Hardware Summit. Watch the stream live on Friday!Hackster is hosting Hackster Cafe: Open Hardware Summit. Stream on Friday!
Arduino_Genuino
Published

Ultrasonic Range Detector Using Arduino and US-016

Make an ultrasonic range detector using Arduino and US-016 to measure distances and display it in the Arduino IoT Cloud!

BeginnerFull instructions provided1 hour4,778
Ultrasonic Range Detector Using Arduino and US-016

Things used in this project

Hardware components

Arduino Nano 33 IoT
Arduino Nano 33 IoT
Or any other IoT compatible board.
×1
Ultrasonic Sensor US-016
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Story

Read more

Code

Ultrasonic Range Detector Using Arduino and US-016

Arduino
/*
Sketch generated by the Arduino IoT Cloud Thing "Ultrasonic Range Detector"
https://create.arduino.cc/cloud/things/ac0a9cf6-f9b8-49e3-9c55-2b43b8802174
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
int distance;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
const int numReadings = 10;
int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average
int inputPin = A0;
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Your code here
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = analogRead(inputPin);
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = total / numReadings;
//assign average to our distance variable calibrate the values to cm
distance = (average/2) / 10;
// send it to the computer as ASCII digits
Serial.print(distance);
Serial.println("cm");
delay(1);        // delay in between reads for stability
}

Credits

Arduino_Genuino
91 projects • 11344 followers
Contact

Comments

Please log in or sign up to comment.