Should i delete the post then? I tought since this was the same project but a different problem a new post should be made, since this is about triggers
Thanks anyway
The moderator may merge the threads. Cross posting wastes everyone's time and is against forum rules.
Your two or more topics on the same or similar subject have been merged.
Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.
Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you wonât have to keep explaining your project repeatedly.
Repeated duplicate posting could result in a temporary or permanent ban from the forum.
Could you take a few moments to Learn How To Use The Forum
It will help you get the best out of the forum in the future.
Thank you.
I suspect that. Know that the code execution time from beginning of pulse 1 until waiting for pulse 2 is very short. Echoes might be flying around.
This has been the trouble in several other projects using several ultra sonics.
Get rid of all the unnecessary wifi/cloud code, and test wind sensor operation with the simplest possible code. The serial monitor is all you need to debug the program.
In your next post, include just the simplified code, along with a clearer description of the problem(s) you observe.
Okay, i will try this tomorrow. Thanks for this and for teaching me that posts like this should not be made anew.
float Distance(long time) {
return (((float)time * 0.034) / 2.0); // Conversion to centimeters
}
You can solve that conundrum by answering the following questions:
-
If the speed of sound is 0.034 cm/us and the distance between the sensors is 31 cm, how many microseconds will it take a pulse from one sensor to reach the other? Show your math.
-
And if you plug that time into your Distance function, what distance does it return? Show your math.
-
What does that say about your Distance function?
If that's not enough to reveal the problem, explain why your Distance function has the "/ 2.0" in it.
I tried sending the second pulse after the first echo like this:
void loop() {
// Leituras dos sensores ultrassĂŽnicos
// Sensor 1
digitalWrite(TriggerPin1, LOW);
delayMicroseconds(2);
digitalWrite(TriggerPin1, HIGH);
delayMicroseconds(10);
digitalWrite(TriggerPin1, LOW);
Duration1 = pulseIn(EchoPin1, HIGH);
// Waiting time
delay(20);
// Sensor 2
digitalWrite(TriggerPin2, LOW);
delayMicroseconds(2);
digitalWrite(TriggerPin2, HIGH);
delayMicroseconds(10);
digitalWrite(TriggerPin2, LOW);
Duration2 = pulseIn(EchoPin2, HIGH);
And with this, the second sensor reads again, but reads values just like the first one, averaging 13 cm:
-> Distance from Sensor 1 = 13.243 cm
14:22:05.312 -> Distance from Sensor 2 = 13.872 cm
14:22:05.312 -> Time Difference = -37 microseconds
14:22:05.312 -> Temp: 16.75 °C | Humidity: 69.59 % | Pressure: 992.38 hPa
14:22:05.312 -> Speed of Sound (Thermodynamic): 341.32 m/s
14:22:05.312 -> Wind Speed: 8.73 m/s (Strong Breeze)
(the distance it should display is 29-30 cm in each sensor. When i had a different trigger on each sensor, the distance was the 29-30 desired, but the measurements could not be proven they worked
FYI
My library has code how to compensate distance for temperature and humidity.
Your code just uses a fixed value.
Code with nothing to do with the cloud, just the anemometer:
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
// Configuration of ultrasonic sensor 1 pins
const int TriggerPin1 = 11;
const int EchoPin1 = 10;
// Configuration of ultrasonic sensor 2 pins
const int TriggerPin2 = 11;
const int EchoPin2 = 12;
// Variables to store the time measured by ultrasonic sensors
long Duration1 = 0;
long Duration2 = 0;
// Distance between sensors (in meters)
const float DistanceBetweenSensors = 0.3; // 30 cm
// I2C address for the BME280 sensor
#define BME280_ADDRESS 0x76
// Create an object for the BME280 sensor
Adafruit_BME280 bme;
// Function to calculate distance (in cm) from measured time
float Distance(long time) {
return (((float)time * 0.034) / 2.0); // Conversion to centimeters
}
// Function to calculate the speed of sound using the thermodynamic formula
float calculateThermodynamicSoundSpeed(float temperature) {
const float gamma = 1.4; // Ratio of specific heats for dry air
const float R = 8.314; // Universal gas constant [J/(mol·K)]
const float M = 0.0289644; // Molar mass of dry air [kg/mol]
// Convert temperature from Celsius to Kelvin
float temperatureK = temperature + 273.15;
// Apply thermodynamic formula
return sqrt((gamma * R * temperatureK) / M);
}
// Function to calculate wind speed
float calculateWindSpeed(float distance, long t1, long t2) {
float t1_s = t1 * 1e-6; // Convert to seconds
float t2_s = t2 * 1e-6; // Convert to seconds
return (distance / 2.0) * (1.0 / t1_s - 1.0 / t2_s); // Wind speed formula
}
// Function to classify wind speed based on the Beaufort scale
String beaufortScale(float windSpeed) {
if (abs(windSpeed) < 0.3) return "Calm";
if (abs(windSpeed) < 1.6) return "Light Air";
if (abs(windSpeed) < 3.4) return "Light Breeze";
if (abs(windSpeed) < 5.5) return "Gentle Breeze";
if (abs(windSpeed) < 7.9) return "Moderate Breeze";
if (abs(windSpeed) < 10.7) return "Fresh Breeze";
if (abs(windSpeed) < 13.8) return "Strong Breeze";
if (abs(windSpeed) < 17.1) return "Near Gale";
if (abs(windSpeed) < 20.7) return "Gale";
if (abs(windSpeed) < 24.4) return "Strong Gale";
if (abs(windSpeed) < 28.4) return "Storm";
if (abs(windSpeed) < 32.6) return "Violent Storm";
return "Hurricane";
}
void setup() {
// Initialize serial communication
Serial.begin(9600);
while (!Serial);
// Configure ultrasonic sensor pins
pinMode(TriggerPin1, OUTPUT);
pinMode(EchoPin1, INPUT);
pinMode(TriggerPin2, OUTPUT);
pinMode(EchoPin2, INPUT);
// Initialize the BME280 sensor
if (!bme.begin(BME280_ADDRESS)) {
Serial.println("Failed to initialize BME280 sensor. Check connections.");
while (1); // Infinite loop in case of critical error
}
Serial.println("Setup completed!");
}
void loop() {
// Ultrasonic sensor readings
// Sensor 1
digitalWrite(TriggerPin1, LOW);
delayMicroseconds(2);
digitalWrite(TriggerPin1, HIGH);
delayMicroseconds(10);
digitalWrite(TriggerPin1, LOW);
Duration1 = pulseIn(EchoPin1, HIGH);
// Wait before triggering the second sensor
delay(20); // Interval to avoid interference
// Sensor 2
digitalWrite(TriggerPin2, LOW);
delayMicroseconds(2);
digitalWrite(TriggerPin2, HIGH);
delayMicroseconds(10);
digitalWrite(TriggerPin2, LOW);
Duration2 = pulseIn(EchoPin2, HIGH);
// Calculate the distance measured by the sensors
float Distance1_cm = Distance(Duration1);
float Distance2_cm = Distance(Duration2);
// BME280 sensor readings
float temperature = bme.readTemperature();
float humidity = bme.readHumidity();
float pressure = bme.readPressure() / 100.0F;
// Calculate the speed of sound and wind speed
float soundSpeedThermodynamic = calculateThermodynamicSoundSpeed(temperature);
float windSpeed = calculateWindSpeed(DistanceBetweenSensors, Duration1, Duration2);
// Classify wind speed on the Beaufort scale
String beaufortCategory = beaufortScale(windSpeed);
// Display distances measured by ultrasonic sensors
Serial.print("Distance from Sensor 1 = ");
Serial.print(Distance1_cm, 3);
Serial.println(" cm");
Serial.print("Distance from Sensor 2 = ");
Serial.print(Distance2_cm, 3);
Serial.println(" cm");
// Display time difference between sensors
long TimeDifference = Duration1 - Duration2;
Serial.print("Time Difference = ");
Serial.print(TimeDifference);
Serial.println(" microseconds");
// Display BME280 sensor readings
Serial.print("Temp: ");
Serial.print(temperature);
Serial.print(" °C | Humidity: ");
Serial.print(humidity);
Serial.print(" % | Pressure: ");
Serial.print(pressure);
Serial.println(" hPa");
// Display the speed of sound
Serial.print("Speed of Sound (Thermodynamic): ");
Serial.print(soundSpeedThermodynamic);
Serial.println(" m/s");
// Display the wind speed
Serial.print("Wind Speed: ");
Serial.print(windSpeed);
Serial.print(" m/s (");
Serial.print(beaufortCategory);
Serial.println(")");
Serial.println("---------------------------");
delay(3000);
}
The problem is the following: I tough i had the project complete. I had code that measured both of the sensor's distances accurately (with about 4 microseconds in difference, wich i tough was very good, considering im feeding the HC-SR04 with 3.3v (i have seen it should be 5v, but i would need 2 resistors and with 3.3v it was showing very good info). My problem was when me and the teacher went to test the prototype, even with him using an anemometer (this one):
Since the windspeed on both was very different. And even with us blowing into them in different angles, the wind speed output barely changed every single time. After that, he told me that the readings could be both sensors interfering with each other, and recommended me to put both of them in the same trigger, but i have not been able to achieve sucess with it.
Note: I wondered if its because his anemometer measures only in the spot where the fan turns, and my project measures along a great distance, but idk how i could prove that
If the speed of sound is 0.034 cm/us and the distance between the sensors is 31 cm, how many microseconds will it take a pulse from one sensor to reach the other? Show your math
Speed of sound = 0.034 cm/”s
Time = Distance Ă· Speed = 31 cm Ă· 0.034 cm/”s=911.76 ÎŒs
And if you plug that time into your Distance function, what distance does it return? Show your math.
Input = 911.76 ”s
Distance=(911.76 ÎŒsĂ0.034 cm/”s) Ă· 2 = 15.5 cm
What does that say about your Distance function?
The function assumes a round trip (to target and back), so it divides by 2 to calculate only the one-way distance.
If that's not enough to reveal the problem, explain why your Distance function has the "/ 2.0" in it.
To account for the pulse traveling to the target and returning, giving the one-way distance.
Note: The sensors are 31cm apart, because looking at the sensors sideways, the ultrasound leaves the sensor aprox in the middle of the silver part, therefor it should make sense to be 0.3 cm in the code and not 0.31
Thanks, gonna see if maybe i can put it in the code, but can't guarentee since this is only a proof of concept. Does it work on HC-SR04 readings? Thanks still
What is the point of having two sensors, if you are measuring round trip distance with each one? That is what your most recently posted code does.
Would you expect wind to make any measurable difference in the timing?
Yeah, imagine this scenario:
The wind is against the position of sensor 2, and in favor of sensor 1, for example. The ultrasound will take more time to reach from the sensor 1 into the sensor 2, and it will take less time to reach from the sensor 2 into the sensor 1, because when the ultrasound is traveling against the wind, it has bigger air resistance, therefore increasing the TOF, and vice versa.
Good luck with that idea!
What do you mean? Is it implausible? Undoable or just senseless
Measuring the round trip time is senseless.
A good idea would be to work your way through the example I posted in #36 above, and understand both the method and the code.
Im not talking about the round trip, because that would be always the same values, since both sensors signals would make the 2 trips.
The trip only occurs once, since the signal from the sensor 1 reaches the receiver on sensor 2, and vice versa.
The code you posted measures the round trip time.
Was it written by your favorite chatbot, and accepted without a glance?
digitalWrite(TriggerPin1, LOW);
delayMicroseconds(2);
digitalWrite(TriggerPin1, HIGH);
delayMicroseconds(10);
digitalWrite(TriggerPin1, LOW);
Duration1 = pulseIn(EchoPin1, HIGH);
A good idea would be to work your way through the example I posted in #36 above, and understand both the method and the code.
Maybe if you triggered both at the same time, then read only one, ignoring the other, then delayed and re-triggered both at the same time and read the other, ignoring the first, you might capture the one-way times from one to the other.
You want/need the interference of reading the ping from the opposite sensor.