Distance Calculator
In this blog, we will discuss how to build a distance calculator using a CPU, a sonar sensor, and the Arduino IDE. This project is designed to calculate the distance between the object and the sensor using the sonar sensor and display the value on the serial monitor of the Arduino IDE. We will be using the Modular library in the Arduino IDE to interface the sonar sensor with the CPU. This project can be useful in various applications such as robotics, automation, and security systems.
Materials Needed
CPU: A microcontroller board that serves as the brain of the robot and can be programmed or Bluetooth-controlled for various functions of the robot.
Sonar Sensor: A sonar sensor is a device that uses sound waves to measure distances. It works by sending out a sound wave and then listening for the echo that bounces back after hitting an object. By timing how long it takes for the echo to return, the sensor can figure out how far away the object is. Sonar sensors are often used in robots to help them navigate and avoid obstacles. They work well both indoors and outdoors and are relatively easy to use. However, they can be affected by factors like wind, humidity, and temperature and may not be as accurate as other sensors in some situations.
Micro USB Cable: A cable used to connect the CPU to a computer for programming or to power the CPU with a power bank.
Power Bank: A portable battery pack is used to power the CPU.
Snap Wires: Wires with pre-attached connectors that can be easily snapped on and off the components, making it easy to connect and disconnect components.
Wiring the Circuit
-
Connect the sonar sensor to port no. 2 of the CPU,
- Use the snap wires to make the connections from the CPU to the sonar sensor.
Download Arduino IDE
You can take the following actions to download the Arduino IDE:
- Go to the official Arduino website at: www.arduino.cc
- Click on the “Software” menu item in the top navigation bar.
- Select the “Downloads” option from the drop-down menu.
- Scroll down to find the section for the Arduino IDE and select the appropriate version for your operating system (Windows, Mac, or Linux).
- Click on the download link for the selected version to start the download.
- Once the download is complete, run the installation file and follow the instructions to install the Arduino IDE on your computer.
- You can launch the Arduino IDE and begin configuring your Arduino board after installation.
Install Library in Arduino IDE
Libraries are pre-written code modules that can be used to extend the functionality of the Arduino IDE and make it easier to write code for the Arduino boards. Installing a library saves time and effort as you don’t have to write the code for the required functionality from scratch. You can simply use the pre-written code in the library and modify it according to your needs.
To install the ZIP library in the Arduino IDE, you can follow these steps:
- Download the ZIP library from a trusted source such as the official Arduino library repository or a verified third-party library repository.
- Open the Arduino IDE on your computer.
- Click on the “Sketch” menu and select “Include Library” and then “Add .ZIP Library“.
- Navigate to the location where you saved the downloaded ZIP library, select the ZIP file, and click on the “Open” button.
- Wait for the installation process to complete. You will see a notification in the Arduino IDE indicating that the library was successfully installed.
Note: Download and install the libraries given below. Ignore if already installed.
Writing the Code
Now that we have connected the sonar sensor to the CPU board, we can write the code to calculate the distance. We will be using the Modular library to interface the sonar sensor with the CPU.
#include <modular.h>
Modular distance;
void setup() {
distance.sonarBegin(2);
distance.usbBegin(9600);
}
In the code above, we first include the Modular library in the code. Then, in the setup() function, we initialise the sonar sensor by calling distance.sonarBegin(2), where `2` is the port number of CPU. We then initialise the USB communication by calling distance.usbBegin(9600).
void loop() {
int value = distance.sonarRead (2);
distance.usbPrint(“Distance is – “);
distance.usbPrint(value);
distance.usbPrintln(” Cm”);
delay(1000);
}
In the loop() function, we first read the distance value from the sonar sensor by calling distance.sonarRead(2). The value is then stored in the variable value. We print the distance value on the serial monitor using the distance.usbPrint() function. We also add a string Cm to indicate that the distance value is in centimeters. Finally, we add a delay of 1 second using the delay(1000) function.
Full Code for Distance Calculator
Testing?
Here are some points to test the above project:
- Move an object in front of the sonar sensor to see the distance value change. Now that we have written the code and wired the circuit, we can test the project.
-
Upload the code to the Arduino board using the Arduino IDE.
- Open the serial monitor of the Arduino IDE by clicking on the magnifying glass icon at the top right corner of the IDE.
- The distance value in centimeters will be displayed on the serial monitor.
Conclusion
In this project, we built a distance calculator using a CPU, a Sonar Sensor and the Arduino IDE. We have learned how to interface the sonar sensor with the CPU using the Modular library in Arduino IDE. This project can be extended to include features such as alarms and automated systems that respond to changes in the distance value.
Leave A Comment