HC-SR04 Ultrasonic sensor

From Interaction Station Wiki
Revision as of 11:27, 22 March 2024 by Annasa (talk | contribs) (Created page with "==Basic code== <syntaxhighlight inline lang="c"> // defines pins numbers const int trigPin = 2; const int echoPin = 3; // defines variables long duration; int distance; void...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Basic code

// defines pins numbers const int trigPin = 2; const int echoPin = 3; // defines variables long duration; int distance; void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(9600); // Starts the serial communication } void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance = duration * 0.034 / 2; // Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.println(distance); }

Noise reduction code

// Constants const int WINDOW_SIZE = 10; // Adjust the window size as needed const int sensorTriggerPin = 2; // Connect to the trigger pin of HC-SR04 const int sensorEchoPin = 3; // Connect to the echo pin of HC-SR04 const int glitchThreshold = 2000; // Threshold for excluding glitchy readings // Variables int readings[WINDOW_SIZE]; // Array to store recent sensor readings int index = 0; // Index for circular buffer long sum = 0; // Sum of recent readings void setup() { Serial.begin(9600); pinMode(sensorTriggerPin, OUTPUT); pinMode(sensorEchoPin, INPUT); } void loop() { // Trigger the sensor digitalWrite(sensorTriggerPin, LOW); delayMicroseconds(2); digitalWrite(sensorTriggerPin, HIGH); delayMicroseconds(10); digitalWrite(sensorTriggerPin, LOW); // Read the echo pulse duration long duration = pulseIn(sensorEchoPin, HIGH); // Calculate distance in centimeters float distance_cm = duration * 0.034 / 2; // Exclude glitchy readings if (distance_cm < glitchThreshold) { // Add the new reading to the window sum -= readings[index]; // Subtract the oldest reading readings[index] = distance_cm; // Store the new reading sum += distance_cm; // Add the new reading // Increment the index (circular buffer) index = (index + 1) % WINDOW_SIZE; // Calculate the moving average float movingAverage = static_cast<float>(sum) / WINDOW_SIZE; // Convert movingAverage to an integer int filteredDistance = static_cast<int>(movingAverage); // Print the filtered distance (as an integer) Serial.print("Filtered Distance: "); Serial.println(filteredDistance); // Add any additional logic or actions based on the filtered value // (e.g., control a motor, trigger an alarm, etc.) } // Add any other necessary logic here delay(100); // Adjust the delay as needed }