Hello! Im currently trying to make an ultrassound anemometer using 2 HC-SR04 sensors, and with a bme280 to get its values (to adjust the wind messurement to the ambient conditions, like high humidity). I can already read all the values from the sensors, but i feel kinda lost now.
My idea would be to put both the sensors looking at one another(with one against the wind and another on it's favor) in like 50-70cm apart (in something like an antenna to avoid walls that could block the wind), but even then i am not sure if i could get the result im expecting with just 2 sensors. I dont need the measurements to be perfect, i just need them to be close enough at the very least.
Code:
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
// Configuration for Ultrasonic Sensor 1 pins
const int TriggerPin1 = 9;
const int EchoPin1 = 10;
// Configuration for Ultrasonic Sensor 2 pins
const int TriggerPin2 = 11;
const int EchoPin2 = 12;
// Variables to store ultrasonic sensor timing
long Duration1 = 0;
long Duration2 = 0;
// Interval between measurements
int Delay_timer = 6000;
// I²C address for the BME280 sensor
#define BME280_ADDRESS 0x76
// Create an object for the BME280
Adafruit_BME280 bme;
// Function to calculate distance (in cm) from time
long Distance(long time) {
return ((time * 0.034) / 2); // Conversion to centimeters
}
void setup() {
// Serial initialization
Serial.begin(9600);
while (!Serial);
// Ultrasonic sensor pin configuration
pinMode(TriggerPin1, OUTPUT);
pinMode(EchoPin1, INPUT);
pinMode(TriggerPin2, OUTPUT);
pinMode(EchoPin2, INPUT);
// Initialize the BME280 sensor
Serial.println("Initializing BME280...");
if (!bme.begin(BME280_ADDRESS)) {
Serial.println("Failed to initialize BME280. Check connections and address.");
while (1); // Infinite loop to indicate an error
}
Serial.println("BME280 successfully initialized!");
}
void loop() {
// --- Ultrasonic Sensor Readings ---
// Sensor 1: Send signal and measure response
digitalWrite(TriggerPin1, LOW);
delayMicroseconds(2);
digitalWrite(TriggerPin1, HIGH);
delayMicroseconds(10);
digitalWrite(TriggerPin1, LOW);
Duration1 = pulseIn(EchoPin1, HIGH);
// Sensor 2: Send signal and measure response
digitalWrite(TriggerPin2, LOW);
delayMicroseconds(2);
digitalWrite(TriggerPin2, HIGH);
delayMicroseconds(10);
digitalWrite(TriggerPin2, LOW);
Duration2 = pulseIn(EchoPin2, HIGH);
// Calculate distances
long Distance1_cm = Distance(Duration1);
long Distance2_cm = Distance(Duration2);
// Display distances from ultrasonic sensors
Serial.print("Sensor 1 Distance = ");
Serial.print(Distance1_cm);
Serial.println(" cm");
Serial.print("Sensor 2 Distance = ");
Serial.print(Distance2_cm);
Serial.println(" cm");
// Calculate the time difference between the sensors
long TimeDifference = Duration1 - Duration2;
Serial.print("Time difference = ");
Serial.print(TimeDifference);
Serial.println(" microseconds");
// --- BME280 Sensor Readings ---
// Temperature
Serial.print("Temperature: ");
Serial.print(bme.readTemperature());
Serial.println(" °C");
// Pressure
Serial.print("Pressure: ");
Serial.print(bme.readPressure() / 100.0F);
Serial.println(" hPa");
// Altitude
Serial.print("Altitude: ");
Serial.print(bme.readAltitude(1013.25)); // Adjust sea-level pressure as needed
Serial.println(" m");
// Humidity
Serial.print("Humidity: ");
Serial.print(bme.readHumidity());
Serial.println(" %");
// Separator for clarity in the serial monitor
Serial.println("---------------------------");
// Wait before the next measurement
delay(Delay_timer);
}
Output:
18:54:42.906 -> ---------------------------
18:54:48.888 -> Sensor 1 Distance = 12 cm
18:54:48.888 -> Sensor 2 Distance = 12 cm
18:54:48.888 -> Time Difference = -15 microseconds
18:54:48.888 -> Temperature: 17.89 °C
18:54:48.888 -> Pressure: 1000.22 hPa
18:54:48.888 -> Altitude: 109.05 m
18:54:48.888 -> Humidity: 75.76 %
18:54:49.025 -> ---------------------------
Any help would be very much appreciated!
Kind regards.
Im simply trying to measure the speed of the wind with the ultrasonic sensors, but to do it im also gonna need to use the values of the bme280 to make the adjustments based on its values (because the ultrassonic sounds can be delayed in certains weather conditions)
Im trying to use the wind speed formula (someething like this: float windSpeed = (distance / 2) * ((1 / time1) - (1 / time2)); ) to achieve the windspeed. And then the bm280 values to make some small corrections
Ok, I see right way the second sensor is still receiving the signal from the first sensor when you trigger the second one.
Would your scheme not work better to just have a solid reflector in front of one sensor and rellate the echo time to wind speed? What is the purpose of two sensors?
The 2 sensors are going to send the ultrasounds to one another (with one of them against the wind and the other in favor). With this, one of the sensors is going to receive the sound quicker than the other, and subtracting this values and with some other calculations, in theory i could measure the wind speed like this.
yeah, i have seen those before. but the thing is i need to do it with just 2, even tho it might be not that perfect. Just wanted to know if it could be possible to pull off
Thanks for the approaches, but all of the ones i searched for require the costum made 4 sensors. This is a school project, it cannot be that "difficult"
That is not possible with HC-SR04 sensors, unless you modify the sensor board circuitry(*).
Try measuring the round trip time of a reflected pulse using just one sensor, with the wind and perpendicular to the wind, to see if there is a measurable difference.
(*) You can cut the PCB trace to or remove the transmitter from one board, point the sensors at each other, trigger both at the same time, and measure the pulse travel time to the modified board.
My teacher has a working portable anemometer, so i will compare the values from mine to his.
Based on my research with 4 its guarenteed, but with 2 its not totally precise and i need to have one against the wind and one in favor
Im studying those project and a couple of other ones to see if i could make this with just 2, but it is starting to seem it cannot be possible to make a decently accurate anemometer with jut 2 hc-sr04. Still trying to find something, thanks anyway for the help
As someone suggested, if you can detatch the receiver from each hc-sr04 and swap them, carefully solder them back with extension chords, one hc-sr04 should be able to measure the distance to its former receiver. If the regular sketch says the distance is 50 cm, the actual distance would be 100 cm, wouldn't it? Because the regular sketch assumes the sound travels back and forth.
If you're not detatching the receivers, I guess you're just measuring the distance between each hc-sr04. For that, they have to face each other. And you need a background plate to assure there will be an echo. Then you measure the distance one hc-sr04 at a time.
I don't understand your ... confusion ... about 2 vs 4. Of course if you use 2 you will only measure the wind speed component parallel to a line between the 2 (unless the system includes a wind vane that properly orients the pair parallel to the wind), and that ought to be sufficient to demonstrate the concept.
Great tip, but i dont trust myself to solder the stuff well, since i am a literal and total beginner.
Gonna try this out after checking with my teacher if he approves such concepts. Thanks!
Because im not sure if 2 would be enough. Every single project i have seen always uses 4. Never seen any project working with 2 or just with a single sensor. I just wanted to know if it would be possible to have a decent measurement with just 2