Introduction: Ultrasonic Sensor With Bharath Pi
In the world of DIY electronics and robotics, Bharath Pi has emerged as a popular choice for hobbyists and enthusiasts. One of the key reasons for its widespread adoption is the ease of integration with various sensors. Among these, the ultrasonic sensor stands out for its versatility and applications in distance measurement, obstacle avoidance, robotics, and more. In this blog, we'll explore the fundamentals of ultrasonic sensors and how to harness their power with Bharath Pi.
Supplies
To get started with an ultrasonic sensor project on Bharath Pi, you'll need the following components:
Hardware components
- Bharath Pi
- Ultrasonic sensor module (e.g., HC-SR04)
- Breadboard and jumper wires
- LCD display or LEDs for visual output
Software apps and online services
We are going to use Arduino IDE for programming the Bharat Pi. The Bharat Pi can be connected using a USB Type-C connector. Serial drivers are required to be installed for the detection of Bharat Pi. You can download the serial drivers from this depending on the operating system.
· Arduino IDE
Step 1: Story
What is the Ultrasonic Sensor
Ultrasonic sensors operate on the principle of sound waves. They emit high-frequency sound pulses, typically in the ultrasonic range (above 20 kHz), and then measure the time taken for these pulses to bounce back after hitting an obstacle. By knowing the speed of sound in air, which is approximately 343 meters per second at 20°C, the sensor can calculate the distance to the obstacle.
The HC-SR04 ultrasonic sensor uses SONAR to determine the distance of an object just like the bats do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package from 2 cm to 400 cm or 1” to 13 feet.
The operation is not affected by sunlight or black material, although acoustically, soft materials like cloth can be difficult to detect. It comes complete with an ultrasonic transmitter and receiver module.
What is Bharat Pi:
In this project, we are using Bharat Pi which has both ESP32 microcontroller and a SimCom A7672S 4G/LTE module.
Bharat Pi is an IoT prototyping board for students, innovators, startups, and developers. A simplified board with an all-in-one compute, network and storage on a single board to build rapid prototypes.
Bharat Pi bundles the power of ESP32, 4G/LTE module in a Arduino form factor. This makes Bharat Pi compatible with all Arduino and ESP32 programs and shields
Wireless monitoring can be done using WiFi or with a Sim based 4G/LTE connectivity.
Bharat Pi is a rugged board with good quality and could be used even for production and live customer deployments!
Step 2: Technical Specifications
· Power Supply − +5V DC
· Quiescent Current − <2mA
· Working Current − 15mA
· Effectual Angle − <15°
· Ranging Distance − 2cm – 400 cm/1″ – 13ft
· Resolution − 0.3 cm
· Measuring Angle − 30 degree
Step 3: Code
let's dive into the Bharath Pi code that enables the communication between the ultrasonic sensor and the microcontroller. We will use the NewPing library, which simplifies working with ultrasonic sensors.
#include <NewPing.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define TRIG_PIN 33
#define ECHO_PIN 32
#define MAX_DISTANCE 200 // Maximum distance to measure (in centimeters)
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); // Create a NewPing object
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change 0x27 to your LCD I2C address if different
void setup() {
lcd.begin();
lcd.backlight();
lcd.print("Ultrasonic Sensor");
lcd.print("By Bharath Pi");
}
void loop() {
delay(100);
unsigned int distance = sonar.ping_cm();
// Display the distance on the LCD
lcd.setCursor(0, 1);
lcd.print("Distance: ");
lcd.print(distance);
lcd.print(" cm ");
}
//With the setup and code in place, you can now upload the sketch to your Bharath Pi board and open the Serial Monitor in the Arduino IDE. As you move objects closer or farther from the ultrasonic sensor, you'll see the distance measurements being printed on the Serial Monitor.
Step 4: Application
The ultrasonic sensor's applications are vast, limited only by your imagination. Here are a few exciting projects you can explore:
Obstacle Avoidance Robot: Use multiple ultrasonic sensors to build a robot that can navigate around obstacles in its path.
Parking Assistant: Create a parking assistance system to help drivers gauge the distance between their car and obstacles while parking.
Security System: Build a security system that alerts you when someone enters a restricted area.
Water Level Measurement: Use the sensor to measure water levels in a tank or container.