This project is based on a project by Project Hub user RZtronics
Want to make a tool that measures distance without using a ruler, then this mini project will be the best solution for it. Here we are gonna make an Arduino distance measuring device with an ultrasonic module similar to a bat. So let's get started!
Here's a quick guide to setting them up with the Arduino IoT Cloud
For this project you will need to use an IoT Cloud enabled Arduino board, we are using an Arduino Nano 33 IoT.
Step 1: WiringStart off by connecting the components to the breadboard. Follow the step by step guide included below.
- Place the Arduino board on the breadboard.
- Place the ultrasonic sensor on the breadboard.
- Connect the GND pin on the ultrasonic sensor to GND on the Arduino board.
- Connect the Out pin on the ultrasonic sensor to A0 on the Arduino board.
- Connect the Range pin on the ultrasonic sensor to GND on the Arduino board.
- Connect the VCC pin on the ultrasonic sensor to 5V on the Arduino board. It is important to enable 5V on your board. Find out more about it here.
When you have completed the circuit, it should look like this:
To get started with this step, you will need some very basic knowledge of the Arduino IoT Cloud service. If you have built any previous project using the service, then don’t worry, you know all that you need to know.
If you are new to the Arduino IoT Cloud, then take some time to read through the Getting started page and you will be good to go. There are also a bunch of tutorials available if needed.
In the cloud, you need to create a new Thing, and configure your device.
You should then add a Variable. Name it Distance. It should be of the variable type Integer Number. It just needs read permission since we only want to read the value from the sensor and display it on the Arduino IoT Cloud.
Now, you will need to create a Dashboard to be able to send information to the Arduino board. Go to the Dashboards section, and build a new dashboard.
Inside, create a new widget: To make the creation of the control widget super easy, we will create the widget from the variable. To do this, follow these steps:
- Press “Add” to open the drop-down menu, then click into the “Things” tab
- Select the thing where you have created the Integer Number variable, then just confirm and create the widget.
- Done!
This widget will, for now, not function, since we have not yet uploaded the sketch to our board, which we will do in the next step.
Step 3: CodeFor this project we need to collect the analog data from the ultrasonic sensor and translate it into cm. To deal with the sensor's fluctuation, smoothing() is applied which you can read in more detail about here.
If you want to skip ahead, you will find the entire sketch at the bottom of this page.
First we need to define all our variables and the array used for calculating the average which results in smoother results. Remember, in addition to the variables seen below we also created the variable Distance which we will use to hold our final value shown in the Arduino IoT Cloud.
// Define the number of samples to keep track of. The higher the number, the
// more the readings will be smoothed, but the slower the output will respond to
// the input. Using a constant rather than a normal variable lets us use this
// value to determine the size of the readings array.
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; //the pin to which the sensor is connected
After defining everything we need it’s time to set up the smoothing and initialisation.
void setup() {
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
Inside the loop function we write the code that feeds in our values from the analog sensor into our array used for smoothing.
void loop() {
// 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;
The final step is to assign our previously created variable distance to the average value of the ultrasonic sensor.
//assign average to our distance variable and calibrate the values to cm
distance = (average/2) / 10;
// print the distance in the Serial Monitor.
Serial.print(distance);
Serial.println("cm");
// delay in between reads for stability
delay(1);
}
ConclusionOnce you upload this sketch, you are done and able to see the distance to an object in the Arduino IoT Cloud.
Enjoy!
Comments
Please log in or sign up to comment.