Project 2 - Measuring the distance of an object using ultrasonic sensor and also smoothen the sensor data using moving average filter
Project - 2 :
In this project the implementation of code for “Measuring distance of an object using ultrasonic distance sensor (28015 Ping) and also smoothen the sensor data using moving average filter”.
Goals :
- Measuring the distance in terms of inches of an object using Ultrasonic Distance Sensor (28015 Ping).
- The sensor data is smoothened by using Moving average filter technique to eliminate the high frequency variations from the sensor data.
Key Highlights of the Project :
- Measuring the distance travelled by the wave to the object and gets bounced back from the object to sensor.
- Smoothening the sensor data by moving average filter technique, the filter has a window of certain size. That window determines that how many data the filter can take.
Deliverables of this project :
- The distance travelled by a wave to the object for which we want to measure the distance using Ultrasonic Distance Sensor (28015 Ping).
- Eliminate the high frequency jitters from the sensor data by Moving Average filter to smoothen the sensor data.
Ans:
To measure the distance of an object using an ultrasonic distance sensor (e.g., HC-SR04 or 28015 Ping) and smoothen the sensor data using a moving average filter, we can use a microcontroller like Arduino.
Components Required:
1: 2 Arduino board (e.g., Arduino Uno)
2: 1 Ultrasonic distance sensor (e.g., HC-SR04)
3: 1 16X2 LCD display module
4: 1 Potentiometer 250 kohm
5: 1 Resistor 220 ohm
6: Breadboard and jumper wires
7: A computer with the Arduino IDE installed
Wiring Connections :
(1): Arduino Master (Transmitter) :
1: Connect the HC-SR04 ultrasonic sensor to the master Arduino as follows :
- VCC of the sensor to 5V on the master Arduino.
- GND of the sensor to GND on the master Arduino.
- Trig (Trigger) of the sensor to digital pin 7 on the master Arduino.
- Echo of the sensor to digital pin 6 on the master Arduino.
2: Connect the potentiometer as follows :
- Connect one outer leg of the potentiometer to 5V on the master Arduino.
- Connect the other outer leg of the potentiometer to GND on the master Arduino.
- Connect the middle leg (wiper) of the potentiometer to analog pin A0 on the master Arduino.
(2): Arduino Slave (Receiver) :
Connect the 16x2 LCD display to the slave Arduino as follows :
- Connect the VCC and GND pins of the LCD to 5V and GND on the slave Arduino.
- Connect the SDA (Serial Data) pin of the LCD to analog pin A4 on the slave Arduino.
- Connect the SCL (Serial Clock) pin of the LCD to analog pin A5 on the slave Arduino.
- Connect the LED+ pin of the LCD to a 220-ohm resistor, and then connect the other end of the resistor to 5V on the slave Arduino.
- Connect the LED- pin of the LCD directly to GND on the slave Arduino.
(3): Both Arduino Boards :
1: Connect the GND of the master Arduino to the GND of the slave Arduino. This provides a common ground reference between both boards.
2: Establish a serial connection between the master and slave Arduino boards using pins 2 (TX) and 3 (RX) on both boards :
- Connect digital pin 2 (TX) on the master Arduino to digital pin 3 (RX) on the slave Arduino.
- Connect digital pin 3 (RX) on the master Arduino to digital pin 2 (TX) on the slave Arduino.
Testing :
1: Upload the Code :
First, make sure that we have uploaded the correct code to each Arduino board. The master Arduino should be running the code that reads the distance and sends it via serial communication, while the slave Arduino should have the code to receive the data and display it on the LCD.
2: Verify Serial Communication :
- Open the Arduino IDE on your computer.
- Connect the master Arduino to your computer via USB.
- Open the Serial Monitor (Tools > Serial Monitor) for the master Arduino. Make sure the baud rate matches the one specified in your code (usually 9600).
- We should see distance values printed in the Serial Monitor. These values should change as you place objects closer to or farther away from the HC-SR04 sensor.
3: Check LCD Display :
- Connect the slave Arduino to your computer via USB.
- Make sure the LCD is displaying the initial text ("Distance (inches):") on the first line.
- If we have a potentiometer connected to the master Arduino for LCD contrast adjustment, adjust it to ensure that the LCD characters are clearly visible.
4: Test Distance Measurement :
- Place objects in front of the HC-SR04 sensor, and observe if the displayed distance on the LCD changes accordingly.
- Move objects closer and farther away to see how the distance readings on the LCD respond.
- Ensure that the LCD is updating with the latest distance values received from the master Arduino.
5: Debugging :
If you encounter any issues during testing, here are some troubleshooting steps :
- Double-check the wiring connections to ensure they match the instructions provided.
- Verify that we have correctly uploaded the code to each Arduino board.
- Ensure both Arduino boards are powered and running.
- Check for any error messages in the Arduino IDE's Serial Monitor for both master and slave Arduinos.
- Verify that the potentiometer (if used) is correctly connected and adjusted for LCD contrast.
Explanation of Code used :
1: The code starts by including the Wire library for I2C communication.
2: In the setup() function :
- The trigger pin (pin 12) is set as an OUTPUT, and the echo pin (pin 11) is set as an INPUT for the ultrasonic sensor.
- I2C communication is enabled with Wire.begin().
- The requestEvent() function is registered as the event handler for I2C requests using Wire.onRequest().
3: In the loop() function, there is nothing to do since the communication with the slave is event-driven.
4: The requestEvent() function is called when a request is received from the slave Arduino :
- It triggers the ultrasonic sensor by sending a short pulse to the trigger pin.
- It measures the duration of the echo pulse using pulseIn() and calculates the distance in centimeters.
- The distance in centimeters and inches is sent to the slave Arduino using Wire.write().
- The distance values are also printed to the Serial Monitor for debugging purposes.
Code :
(1): Master Code :
#include
int CM = 0; /* This declares an integer variable named CM and initializes it to the value 0. It is used to store the distance measurement in centimeters */
int inch = 0; /* This declares another integer variable named inch and initializes it to the value 0. It is used to store the distance measurement in inches */
int duration = 0; /* This declares an integer variable named duration and initializes it to the value 0. It is used to store the duration of an ultrasonic pulse, which is later used to calculate the distance */
void setup() /* This allows us to communicate with Arduino board via the Serial Monitor in the Arduino IDE and this function is typically used for one-time initialization tasks */
{
pinMode(12, OUTPUT); /* Trigger pin for ultrasonic sensor */
pinMode(11, INPUT); /* Echo pin for ultrasonic sensor */
Wire.begin(2); /* I2C communication begin */
Wire.onRequest(requestEvent); /* Register requestEvent() as the event handler for I2C requests */
Serial.begin(9600); /* Begins Serial Communication at 9600 baud rate */
}
void loop()
{
}
void requestEvent()
{
digitalWrite(12, LOW); /* Start ultrasonic sensor measurement */
delayMicroseconds(2);
digitalWrite(12, HIGH);
delayMicroseconds(10); /* Keep the trigger pin high for 10 microseconds */
digitalWrite(12, LOW);
duration = pulseIn(11, HIGH); /* Measure the duration of the echo pulse */
CM = 0.01723 * duration; /* Calculate distance in centimeters using the formula */
Serial.println(CM);
Wire.write(CM); /* Send distance in centimeters to the slave */
inch = (CM / 2.54); /* Convert to inches */
Wire.write(inch); /* Send distance in inches to the slave */
Serial.println(inch);
}
(2): Slave Code :
#include
#include
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); /* Define LCD Module Pins (RS,EN,D4,D5,D6,D7) */
void setup() /* This allows us to communicate with Arduino board via the Serial Monitor in the Arduino IDE and this function is typically used for one-time initialization tasks */
{
lcd.begin(16, 2); /* Initialize LCD display */
lcd.setCursor(0, 0); /* Sets Cursor at first line of Display */
lcd.print("C.saishailesh"); /* Prints C.saishailesh in LCD */
lcd.setCursor(0, 1); /* Sets Cursor at second line of Display */
lcd.print("Ultrasonicsensor"); /* Prints Ultrasonicsensor in LCD */
delay(2000); /* Delay for 2 seconds */
lcd.clear(); /* Clears LCD display */
Wire.begin(); /* Enable the I2C communication */
Serial.begin(9600); /* Begins Serial Communication at 9600 baud rate */
}
void loop()
{
Wire.requestFrom(2, 8); /* Address of slave and bytes quantity */
byte CMReceive = Wire.read(); /* Receive data from the slave */
lcd.begin(16, 2); /* Initialize LCD display */
lcd.setCursor(0, 0); /* Sets Cursor at first line of Display */
lcd.print("Distance in CM"); /* Prints Distance in CM in LCD */
lcd.setCursor(0, 1); /* Sets Cursor at second line of Display */
lcd.print(CMReceive); /* Prints CMReceive in LCD */
Serial.println("Distance in CM");
Serial.println(CMReceive);
delay(2000); /* Delay for 2 seconds */
lcd.clear(); /* Clears LCD display */
byte INCHReceive = Wire.read(); /* Receive data from the slave */
lcd.begin(16, 2); * Initialize LCD display */
lcd.setCursor(0, 0); /* Sets Cursor at first line of Display */
lcd.print("Distance in INCH"); /* Prints Distance in INCH in LCD */
lcd.setCursor(0, 1); /* Sets Cursor at second line of Display */
lcd.print(INCHReceive); /* Print INCHReceive in LCD */
Serial.println("Distance in INCH");
Serial.println(INCHReceive);
delay(2000); /* Delay for 2 seconds */
}
Uses of the application :
1: Distance Measurement and Monitoring :
It can be used for distance measurement and monitoring in various applications, such as detecting the presence of objects, measuring liquid levels, or monitoring the distance of moving objects.
2: Automation and Robotics :
In robotics, this application can be used for obstacle avoidance. Robots can use ultrasonic sensors to detect obstacles and navigate around them.
3: Parking Assist Systems :
It can be integrated into parking assist systems in vehicles to help drivers judge the distance between their vehicle and obstacles when parking.
4: Security Systems :
It can be used in security systems to detect motion or proximity of objects and trigger alarms or notifications.
5: Industrial Automation :
In industrial settings, it can be used for precise positioning and control of machinery or equipment.
6: IoT (Internet of Things) :
When combined with Wi-Fi or other connectivity modules, this application can be part of an IoTsetup, allowing remote monitoring and control of distances.
7: Home Automation :
It can be integrated into home automation systems for various purposes, such as turning on lights when someone is nearby or ensuring that doors are closed.
8: Education and Learning :
It's an excellent educational tool for teaching electronics, programming, and sensor integration to students and hobbyists.
9: Research and Development :
Scientists and researchers can use this application for various experiments and measurements where distance is a critical parameter.
10: Hobby Projects :
DIY enthusiasts and hobbyists can use this as a component in their projects, whether it's building a robot, creating an interactive art installation, or designing a smart gadget.
11: Prototyping :
Engineers and product developers can use this as a prototype for developing more advanced systems that require distance measurement capabilities.