Control HCSR04 Ultrasonic Sensor : If you’re working on a project involving an HC-SR04 ultrasonic sensor and want to send distance measurements to a smartphone via Bluetooth, this guide is for you. With this code, you can monitor real-time distance and calculate the fill percentage through a Bluetooth app like BlueBot. Below, you’ll find a step-by-step explanation of the code and how it works.
Full Code for HC-SR04 and Bluetooth Integration
#include <SoftwareSerial.h>
// Pin definitions for HC-SR04
const int trigPin = 5; // Trigger pin of the ultrasonic sensor
const int echoPin = 6; // Echo pin of the ultrasonic sensor
// HC-05 Bluetooth module
SoftwareSerial bluetooth(2, 3); // RX, TX pins for Bluetooth
// Variables for ultrasonic sensor
long duration;
int distance;
int maxDistance = 100; // Maximum measurable distance in cm
void setup() {
// Initialize Serial Monitor
Serial.begin(9600);
// Initialize Bluetooth
bluetooth.begin(9600);
Serial.println("Bluetooth initialized");
// Set pin modes for the ultrasonic sensor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Measure distance using the ultrasonic sensor
distance = getDistance();
// Calculate fill percentage based on distance
//int fillPercentage = map(distance, 0, maxDistance, 100, 0);
//fillPercentage = constrain(fillPercentage, 0, 100);
// Calculate fill percentage based on distance (increasing with distance)
int fillPercentage = map(distance, 0, maxDistance, 0, 100);
fillPercentage = constrain(fillPercentage, 0, 100);
// Format and send the data via Bluetooth
bluetooth.print(distance);
bluetooth.print(";");
bluetooth.print(fillPercentage);
bluetooth.println(";");
// Debugging: Print the values to Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.print(" cm, Fill Percentage: ");
Serial.print(fillPercentage);
Serial.println("%");
// Add a delay for stability
delay(1000);
}
// Function to measure distance using the HC-SR04 sensor
int getDistance() {
// Send a 10-microsecond pulse to the trigger pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the duration of the echo pulse
duration = pulseIn(echoPin, HIGH);
// Calculate the distance in cm
int distance = duration * 0.034 / 2;
return distance;
}
Step-by-Step Explanation of the Code
1. Setting Up the HC-SR04 Sensor
- The HC-SR04 sensor has two main pins for interaction:
trigPin
andechoPin
. - The
trigPin
sends an ultrasonic pulse, and theechoPin
receives the reflected signal. - In the code, we defined:
trigPin
as pin 5.echoPin
as pin 6.
2. Configuring the HC-05 Bluetooth Module
- The HC-05 module allows communication between the Arduino and the BlueBot app.
- The
SoftwareSerial
library is used to create a virtual serial connection on pins 2 (RX) and 3 (TX).
3. Distance Measurement Logic
- A short pulse of 10 microseconds is sent from the trigger pin.
- The duration of the returned pulse is measured using
pulseIn(echoPin, HIGH)
. - The formula
distance = duration * 0.034 / 2
converts the duration into distance in centimeters.- 0.034: Speed of sound in cm/µs.
- /2: Divides by 2 to account for the pulse traveling to and from the object.
4. Calculating the Fill Percentage
- The
map()
function translates the distance (0 tomaxDistance
) into a percentage value (0% to 100%). - This value is constrained to ensure it stays within valid limits.
5. Sending Data Over Bluetooth
- Data is sent in the format
distance;fillPercentage;
for easy parsing. - The app can extract these values to display real-time information.
6. Debugging Information
- The
Serial.print
statements help you verify the sensor’s output in the Serial Monitor. - These logs are helpful for debugging or testing without Bluetooth.
How to Use the Code
Components Required
- HC-SR04 Ultrasonic Sensor.
- HC-05 Bluetooth Module.
- Arduino Uno (or similar board).
- Jumper wires.
Connection Diagram
Component | Arduino Pin |
---|---|
HC-SR04 Trig Pin | 5 |
HC-SR04 Echo Pin | 6 |
HC-05 TX | 2 |
HC-05 RX | 3 |
VCC (both modules) | 5V |
GND (both modules) | GND |
Steps to Run
- Upload the code to your Arduino board.
- Pair your HC-05 module with your smartphone.
- Open the BlueBot app and connect to the HC-05 module.
- The app will display:
- Real-time distance in centimeters.
- Fill percentage (e.g., for tank or container levels).
Why This Project is Useful
- Real-Time Monitoring: Get distance and fill levels instantly on your phone.
- Custom Alerts: Use the app to trigger notifications or sound alarms.
- Versatile Applications: Perfect for monitoring tank levels, parking sensors, or obstacle detection.
Common Issues and Troubleshooting
- Bluetooth Not Connecting
- Ensure your smartphone is paired with the HC-05 module.
- Double-check the RX and TX connections.
- Incorrect Distance Values
- Verify the ultrasonic sensor connections.
- Ensure there are no obstructions near the sensor.
- No Data Received
- Check the baud rate in the BlueBot app matches
9600
.
- Check the baud rate in the BlueBot app matches
Control HCSR04 Ultrasonic Sensor : With this project, you’ll not only learn to interface an ultrasonic sensor and a Bluetooth module but also send and display data in a user-friendly format.