hello sir I am getting a 0 value as a distance very frequently as I observe in the live expressions for e.g like this : 89, 0, 90, 0, 91, 90, 0, 92 and so on please help me to resolve this
@@NizarMohideen To integrate the buzzer in the code of the ultrasonic sensor and if I want it to sound in a distance less than one meter, it would only be to put an if inside the while? and put if (distance
Inside the while loop between Distance = (Value2-Value1)* 0.034/2; and HAL_Delay(50); as below Distance = (Value2-Value1)* 0.034/2; if (Distance < 100) { // Switch on the buzzer } else { // Switch off the buzzer } HAL_Delay(50);
Hi, can you tell me how to use this code and these devices to make sure that when approaching a laptop (I'm going to make a smart stand) to the stand where the HC-SR04 is built in, a ventilator (cooler) is turned on, or at least a 5 volt signal is sent to one of the terminals of this sensor (HC-SR04)? could you give an example of the code for the STM IDE?
It waits for echo pins value in a while loop. In case if there is any problem with ultrasonic sensor, the program will get stuck in this loop. In order to escape from such situation, I wait for only 10 miili seconds for the echo pin value with the help of HAL_GetTick().
Hi Ali Kaan, (Value2-Value1) is the time taken for the ultrasound to go and comeback We need to multiply by speed of sound to get the distance Speed of sound is 343 m/s Since the sound travels two times the distance, we need to divide by 2 Considering unit conversion between metre → centremetre and millisecond → second We get 0.034 Details explanation is in th-cam.com/video/ZejQOX69K5M/w-d-xo.html
In this video I set Clock Configuration tab → HCLK (MHz) to 72 You can set the speed of STM32 to any value you want up-to maximum of 72 for STM32F103C8T6. Most of the time 72 is better. I set prescaler to 71 The reason is I needed to measure micro-seconds for the sensor Time = 1 / Frequency 1 us = 1 / 1MHz To have micro-second, timer frequency needs to be 1MHz. Because I already set clock to 72MHz, I need to divide it by 72 for timer frequency. You can divide it using prescalor. Prescalor is always one less so it is 71 Another example: If you set clock to 64MHz, you need to divide it by 64 for timer frequency. Prescalor is 63
Hi Katia, (Value2-Value1) is the time taken for the ultrasound to go and comeback We need to multiply by speed of sound to get the distance Speed of sound is 343 m/s Since the sound travels two times the distance, we need to divide by 2 Considering unit conversion between metre → centremetre and millisecond → second We get 0.034 Details explanation is in th-cam.com/video/ZejQOX69K5M/w-d-xo.html
Thank you very much , please i have a question , i have a ph sensor with analog output (0 5v ) and i want to watch the value of voltage of my ph sensor in the putty or in the live expression how can i do it can you help me please
Use a voltage divider to get 0-3.3V from 0-5V. You can use 10k and 5k resister for this purpose You can re-scale back to 0-5V in the coding And then try video no 16 in this TH-cam channel to get the data to live expression Or try video no 64 to get the data to putty or database.
The module I am using here is HC-SR04. It has separate TRIG and ECHO pins. Ultrasonic ranger v2.0 has only one pin for both transmit and receive so the setup and code I used in here can’t be used with it. I am sorry to say that I don’t have Ultrasonic ranger v2.0 with me to do a demo
HI, probably generic question, but I have a problem, so my problem is I want to use ultrasonic sensor and run it in task using freertos, and when it run's iIhave a lot's of different measurment of very high values compare to run in endless while loop without freertos.. When it run's in normall loop without freertos everything is normall and values are about 2000 to 3000 with 0-15 centimeters, and I also seen the Pmils is increasing a little bit slower with freertos enalbed values 1 and value2 P I used code from your youtube video so what i do (currently I' am suing a STM32F103C8T6, but in the future a want to use stm32f46g-disco because there is a screan and this is why i need a task to use queue): 1. i choose the MCU/CPU (STM32F103C8T6) 2. In RCC i set High Speed Clock as a Crystall/Ceramic Resonator 3. in SYS i set Timebase Source as TIM2 4. in timers i set timer TIM1 with following values: Clock source as internal Prescaler to 71 5. in freertos is set Interface as a CMSIS_V1 i added new task with following parameters myTask02 osPriorityNormal Stack Size (words) 128 Entry Function StartTask02 Code Generation Option Default Parameter NULL allocation Dynamic Buffer Name NULL unpossible to set Control Block Name unpossible to set I also check the valu of priority in Task1 it is the same as in the seconds one, so it's osPriorityNormal in advance settings i set USE_NEWLIB_REENTRANT as enabled. so my point is how to run HC-SR04 measurment with freertos and task I would be gratefull i you could help me, I think it would help others, because HC-SR04 can be use widely. you can treat it as a idea for next video, sorry for bad grammar i hope you will inderstand. If you have any problem or you need any extra question. text me here in coments
i try some weird things and there and i find a very very weird solution here is a part of code from task, i get a proper distance From HAL_Get_tick, how the hell it works, I'm so confused right now, small disclaimer GPIO 10 is for testing purpose there is LED for(;;) { //HAL_GPIO_TooglePin(GPIOA, GPIO_PIN_9); HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_10); HAL_GPIO_WritePin(TRIG_PORT, TRIG_PIN, GPIO_PIN_SET); // pull the TRIG pin HIGH __HAL_TIM_SET_COUNTER(&htim1, 0); while (__HAL_TIM_GET_COUNTER (&htim1) < 10); // wait for 10 us HAL_GPIO_WritePin(TRIG_PORT, TRIG_PIN, GPIO_PIN_RESET); // pull the TRIG pin low pMillis = HAL_GetTick(); // used this to avoid infinite while loop (for timeout) // wait for the echo pin to go high while (!(HAL_GPIO_ReadPin (ECHO_PORT, ECHO_PIN)) && pMillis + 10 > HAL_GetTick()); Value1 = __HAL_TIM_GET_COUNTER (&htim1); pMillis = HAL_GetTick(); // used this to avoid infinite while loop (for timeout) // wait for the echo pin to go low while ((HAL_GPIO_ReadPin (ECHO_PORT, ECHO_PIN)) && pMillis + 50 > HAL_GetTick()); Value2 = __HAL_TIM_GET_COUNTER (&htim1); Distance = (Value2-Value1)* 0.034/2; DISTANCE1 = HAL_GetTick(); // HAL_Delay(50); osDelay(400); }
HI, probably generic question, but I have a problem, so my problem is I want to use ultrasonic sensor and run it in task using freertos, and when it run's iIhave a lot's of different measurment of very high values compare to run in endless while loop without freertos.. When it run's in normall loop without freertos everything is normall and values are about 2000 to 3000 with 0-15 centimeters, and I also seen the Pmils is increasing a little bit slower with freertos enalbed values 1 and value2 P I used code from your youtube video so what i do (currently I' am suing a STM32F103C8T6, but in the future a want to use stm32f46g-disco because there is a screan and this is why i need a task to use queue): 1. i choose the MCU/CPU (STM32F103C8T6) 2. In RCC i set High Speed Clock as a Crystall/Ceramic Resonator 3. in SYS i set Timebase Source as TIM2 4. in timers i set timer TIM1 with following values: Clock source as internal Prescaler to 71 5. in freertos is set Interface as a CMSIS_V1 i added new task with following parameters myTask02 osPriorityNormal Stack Size (words) 128 Entry Function StartTask02 Code Generation Option Default Parameter NULL allocation Dynamic Buffer Name NULL unpossible to set Control Block Name unpossible to set I also check the valu of priority in Task1 it is the same as in the seconds one, so it's osPriorityNormal in advance settings i set USE_NEWLIB_REENTRANT as enabled. so my point is how to run HC-SR04 measurment with freertos and task I would be gratefull i you could help me, I think it would help others, because HC-SR04 can be use widely. you can treat it as a idea for next video, sorry for bad grammar i hope you will inderstand. If you have any problem or you need any extra question. text me here in coments
i try some weird things and there and i find a very very weird solution here is a part of code from task, i get a proper distance From HAL_Get_tick, how the hell it works, I'm so confused right now, small disclaimer GPIO 10 is for testing purpose there is LED for(;;) { //HAL_GPIO_TooglePin(GPIOA, GPIO_PIN_9); HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_10); HAL_GPIO_WritePin(TRIG_PORT, TRIG_PIN, GPIO_PIN_SET); // pull the TRIG pin HIGH __HAL_TIM_SET_COUNTER(&htim1, 0); while (__HAL_TIM_GET_COUNTER (&htim1) < 10); // wait for 10 us HAL_GPIO_WritePin(TRIG_PORT, TRIG_PIN, GPIO_PIN_RESET); // pull the TRIG pin low pMillis = HAL_GetTick(); // used this to avoid infinite while loop (for timeout) // wait for the echo pin to go high while (!(HAL_GPIO_ReadPin (ECHO_PORT, ECHO_PIN)) && pMillis + 10 > HAL_GetTick()); Value1 = __HAL_TIM_GET_COUNTER (&htim1); pMillis = HAL_GetTick(); // used this to avoid infinite while loop (for timeout) // wait for the echo pin to go low while ((HAL_GPIO_ReadPin (ECHO_PORT, ECHO_PIN)) && pMillis + 50 > HAL_GetTick()); Value2 = __HAL_TIM_GET_COUNTER (&htim1); Distance = (Value2-Value1)* 0.034/2; DISTANCE1 = HAL_GetTick(); // HAL_Delay(50); osDelay(400); }
thank you sooo muuchhhh sir, i apreciate ur hard work to educate us :)
hello sir I am getting a 0 value as a distance very frequently as I observe in the live expressions for e.g like this : 89, 0, 90, 0, 91, 90, 0, 92 and so on please help me to resolve this
thank you sir, how can I display this result in LCD display16X2?
Hi, How can I program two Ultrasonic Distance Sensors with buzzers?
Yes.
Passive buzzer video is here th-cam.com/video/zw1mQUI2sSM/w-d-xo.html
Active buzzer video is here th-cam.com/video/Bei-Of6GJAw/w-d-xo.html
@@NizarMohideen To integrate the buzzer in the code of the ultrasonic sensor and if I want it to sound in a distance less than one meter, it would only be to put an if inside the while? and put
if (distance
Inside the while loop between Distance = (Value2-Value1)* 0.034/2; and HAL_Delay(50); as below
Distance = (Value2-Value1)* 0.034/2;
if (Distance < 100)
{
// Switch on the buzzer
}
else
{
// Switch off the buzzer
}
HAL_Delay(50);
@@NizarMohideen Thank you so much
how you reduce the distance
Hi, very good video, I appreciate your work. Do you plan a project with a more exact distance sensor VL53L1X and Blue Pill board?
Hi, can you tell me how to use this code and these devices to make sure that when approaching a laptop (I'm going to make a smart stand) to the stand where the HC-SR04 is built in, a ventilator (cooler) is turned on, or at least a 5 volt signal is sent to one of the terminals of this sensor (HC-SR04)? could you give an example of the code for the STM IDE?
Hello , could you explain what this sentence means
pMillis = HAL_GetTick(); // used this to avoid infinite while loop (for timeout)
It waits for echo pins value in a while loop. In case if there is any problem with ultrasonic sensor, the program will get stuck in this loop. In order to escape from such situation, I wait for only 10 miili seconds for the echo pin value with the help of HAL_GetTick().
Hi,in the live expressions i got for distance" target is not available,"what could possibly be the problem?
hello i got the same error do you resolve it ? please if you have any idea could you tell me
Hello, how much maximum the sensor can read the distance?
400 cm
as in www.sparkfun.com/products/15569
how much distance measure?
Hi, why did you use 0.034/2 instead of " /58 "
Hi Ali Kaan,
(Value2-Value1) is the time taken for the ultrasound to go and comeback
We need to multiply by speed of sound to get the distance
Speed of sound is 343 m/s
Since the sound travels two times the distance, we need to divide by 2
Considering unit conversion between metre → centremetre
and millisecond → second
We get 0.034
Details explanation is in
th-cam.com/video/ZejQOX69K5M/w-d-xo.html
Actually you can multiply by by 0.034/2
Or you can divide by 58
You will get the same results
( Because 0.034/2 = 1/58 )
If I wanted to use two ultrasonic sensors, would the code in the while be copied twice and the pins changed?
Yes. You can have two sets of code in while loop one after another.
sir what the exact formulla to set different project STM32CubeIDE Settings as system core,analog,timmer etc
In this video I set Clock Configuration tab → HCLK (MHz) to 72
You can set the speed of STM32 to any value you want up-to maximum of 72 for STM32F103C8T6. Most of the time 72 is better.
I set prescaler to 71
The reason is
I needed to measure micro-seconds for the sensor
Time = 1 / Frequency
1 us = 1 / 1MHz
To have micro-second, timer frequency needs to be 1MHz.
Because I already set clock to 72MHz, I need to divide it by 72 for timer frequency.
You can divide it using prescalor.
Prescalor is always one less so it is 71
Another example: If you set clock to 64MHz, you need to divide it by 64 for timer frequency.
Prescalor is 63
Hello sir please can you tell me why did you multiply to 0.034 please ? And please how can i have a float value of distance
Hi Katia,
(Value2-Value1) is the time taken for the ultrasound to go and comeback
We need to multiply by speed of sound to get the distance
Speed of sound is 343 m/s
Since the sound travels two times the distance, we need to divide by 2
Considering unit conversion between metre → centremetre
and millisecond → second
We get 0.034
Details explanation is in
th-cam.com/video/ZejQOX69K5M/w-d-xo.html
instead of uint16_t Distance = 0; // cm
float Distance = 0.0; // use for float value of distance
Thank you very much , please i have a question , i have a ph sensor with analog output (0 5v ) and i want to watch the value of voltage of my ph sensor in the putty or in the live expression how can i do it can you help me please
Use a voltage divider to get 0-3.3V from 0-5V.
You can use 10k and 5k resister for this purpose
You can re-scale back to 0-5V in the coding
And then try video no 16 in this TH-cam channel to get the data to live expression
Or try video no 64 to get the data to putty or database.
Hello please can i use it to measure the water level ?
❤🧡💛♥💛🧡💛
CAN ANYONE EXPLAIN ME DEBUG CONFIGURATION WITH ST-LINK, THERE IS PROBLEM REGARDING GDB SERVER
I Need library for Ultrasonic ranger v2.0 IS IT the Same as this sensor
The module I am using here is HC-SR04. It has separate TRIG and ECHO pins.
Ultrasonic ranger v2.0 has only one pin for both transmit and receive so the setup and code I used in here can’t be used with it.
I am sorry to say that I don’t have Ultrasonic ranger v2.0 with me to do a demo
@@NizarMohideen Can i have ur mail adress
HI, probably generic question, but I have a problem, so my problem is I want to use ultrasonic sensor and run it in task using freertos, and when it run's iIhave a lot's of different measurment of very high values compare to run in endless while loop without freertos.. When it run's in normall loop without freertos everything is normall and values are about 2000 to 3000 with 0-15 centimeters, and I also seen the Pmils is increasing a little bit slower with freertos enalbed
values 1 and value2 P
I used code from your youtube video
so what i do (currently I' am suing a STM32F103C8T6, but in the future a want to use stm32f46g-disco because there is a screan and this is why i need a task to use queue):
1. i choose the MCU/CPU (STM32F103C8T6)
2. In RCC i set High Speed Clock as a Crystall/Ceramic Resonator
3. in SYS i set Timebase Source as TIM2
4. in timers i set timer TIM1 with following values:
Clock source as internal
Prescaler to 71
5. in freertos is set Interface as a CMSIS_V1
i added new task with following parameters
myTask02
osPriorityNormal
Stack Size (words) 128
Entry Function StartTask02
Code Generation Option Default
Parameter NULL
allocation Dynamic
Buffer Name NULL unpossible to set
Control Block Name unpossible to set
I also check the valu of priority in Task1 it is the same as in the seconds one, so it's osPriorityNormal
in advance settings i set USE_NEWLIB_REENTRANT as enabled.
so my point is how to run HC-SR04 measurment with freertos and task
I would be gratefull i you could help me, I think it would help others, because HC-SR04 can be use widely.
you can treat it as a idea for next video,
sorry for bad grammar i hope you will inderstand. If you have any problem or you need any extra question. text me here in coments
i try some weird things and there and i find a very very weird solution here is a part of code from task, i get a proper distance From HAL_Get_tick, how the hell it works, I'm so confused right now, small disclaimer GPIO 10 is for testing purpose there is LED
for(;;)
{
//HAL_GPIO_TooglePin(GPIOA, GPIO_PIN_9);
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_10);
HAL_GPIO_WritePin(TRIG_PORT, TRIG_PIN, GPIO_PIN_SET); // pull the TRIG pin HIGH
__HAL_TIM_SET_COUNTER(&htim1, 0);
while (__HAL_TIM_GET_COUNTER (&htim1) < 10); // wait for 10 us
HAL_GPIO_WritePin(TRIG_PORT, TRIG_PIN, GPIO_PIN_RESET); // pull the TRIG pin low
pMillis = HAL_GetTick(); // used this to avoid infinite while loop (for timeout)
// wait for the echo pin to go high
while (!(HAL_GPIO_ReadPin (ECHO_PORT, ECHO_PIN)) && pMillis + 10 > HAL_GetTick());
Value1 = __HAL_TIM_GET_COUNTER (&htim1);
pMillis = HAL_GetTick(); // used this to avoid infinite while loop (for timeout)
// wait for the echo pin to go low
while ((HAL_GPIO_ReadPin (ECHO_PORT, ECHO_PIN)) && pMillis + 50 > HAL_GetTick());
Value2 = __HAL_TIM_GET_COUNTER (&htim1);
Distance = (Value2-Value1)* 0.034/2;
DISTANCE1 = HAL_GetTick();
// HAL_Delay(50);
osDelay(400);
}
HI, probably generic question, but I have a problem, so my problem is I want to use ultrasonic sensor and run it in task using freertos, and when it run's iIhave a lot's of different measurment of very high values compare to run in endless while loop without freertos.. When it run's in normall loop without freertos everything is normall and values are about 2000 to 3000 with 0-15 centimeters, and I also seen the Pmils is increasing a little bit slower with freertos enalbed
values 1 and value2 P
I used code from your youtube video
so what i do (currently I' am suing a STM32F103C8T6, but in the future a want to use stm32f46g-disco because there is a screan and this is why i need a task to use queue):
1. i choose the MCU/CPU (STM32F103C8T6)
2. In RCC i set High Speed Clock as a Crystall/Ceramic Resonator
3. in SYS i set Timebase Source as TIM2
4. in timers i set timer TIM1 with following values:
Clock source as internal
Prescaler to 71
5. in freertos is set Interface as a CMSIS_V1
i added new task with following parameters
myTask02
osPriorityNormal
Stack Size (words) 128
Entry Function StartTask02
Code Generation Option Default
Parameter NULL
allocation Dynamic
Buffer Name NULL unpossible to set
Control Block Name unpossible to set
I also check the valu of priority in Task1 it is the same as in the seconds one, so it's osPriorityNormal
in advance settings i set USE_NEWLIB_REENTRANT as enabled.
so my point is how to run HC-SR04 measurment with freertos and task
I would be gratefull i you could help me, I think it would help others, because HC-SR04 can be use widely.
you can treat it as a idea for next video,
sorry for bad grammar i hope you will inderstand. If you have any problem or you need any extra question. text me here in coments
i try some weird things and there and i find a very very weird solution here is a part of code from task, i get a proper distance From HAL_Get_tick, how the hell it works, I'm so confused right now, small disclaimer GPIO 10 is for testing purpose there is LED
for(;;)
{
//HAL_GPIO_TooglePin(GPIOA, GPIO_PIN_9);
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_10);
HAL_GPIO_WritePin(TRIG_PORT, TRIG_PIN, GPIO_PIN_SET); // pull the TRIG pin HIGH
__HAL_TIM_SET_COUNTER(&htim1, 0);
while (__HAL_TIM_GET_COUNTER (&htim1) < 10); // wait for 10 us
HAL_GPIO_WritePin(TRIG_PORT, TRIG_PIN, GPIO_PIN_RESET); // pull the TRIG pin low
pMillis = HAL_GetTick(); // used this to avoid infinite while loop (for timeout)
// wait for the echo pin to go high
while (!(HAL_GPIO_ReadPin (ECHO_PORT, ECHO_PIN)) && pMillis + 10 > HAL_GetTick());
Value1 = __HAL_TIM_GET_COUNTER (&htim1);
pMillis = HAL_GetTick(); // used this to avoid infinite while loop (for timeout)
// wait for the echo pin to go low
while ((HAL_GPIO_ReadPin (ECHO_PORT, ECHO_PIN)) && pMillis + 50 > HAL_GetTick());
Value2 = __HAL_TIM_GET_COUNTER (&htim1);
Distance = (Value2-Value1)* 0.034/2;
DISTANCE1 = HAL_GetTick();
// HAL_Delay(50);
osDelay(400);
}