42. STM32CubeIDE Ultrasonic Distance Sensor. HC-SR04 with STM32F103C8T6

Поділитися
Вставка
  • Опубліковано 25 сер 2024

КОМЕНТАРІ • 39

  • @azhanisyahputra9867
    @azhanisyahputra9867 Рік тому

    thank you sooo muuchhhh sir, i apreciate ur hard work to educate us :)

  • @aniketsubudhi2260
    @aniketsubudhi2260 Рік тому +2

    thank you sir, how can I display this result in LCD display16X2?

  • @kaalesaaye4261
    @kaalesaaye4261 Рік тому +1

    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

  • @tomasmyslivec3084
    @tomasmyslivec3084 Рік тому +1

    Hi, very good video, I appreciate your work. Do you plan a project with a more exact distance sensor VL53L1X and Blue Pill board?

  • @woldecosgrove
    @woldecosgrove 3 місяці тому

    Hello , could you explain what this sentence means
    pMillis = HAL_GetTick(); // used this to avoid infinite while loop (for timeout)

    • @NizarMohideen
      @NizarMohideen  3 місяці тому

      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().

  • @user-sv4si7hi5j
    @user-sv4si7hi5j Рік тому

    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?

  • @yessicavannesa1032
    @yessicavannesa1032 2 роки тому +2

    Hi, How can I program two Ultrasonic Distance Sensors with buzzers?

    • @NizarMohideen
      @NizarMohideen  2 роки тому +1

      Yes.
      Passive buzzer video is here ua-cam.com/video/zw1mQUI2sSM/v-deo.html
      Active buzzer video is here ua-cam.com/video/Bei-Of6GJAw/v-deo.html

    • @yessicavannesa1032
      @yessicavannesa1032 2 роки тому +1

      @@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

    • @NizarMohideen
      @NizarMohideen  2 роки тому +1

      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);

    • @yessicavannesa1032
      @yessicavannesa1032 2 роки тому +1

      @@NizarMohideen Thank you so much

  • @052rafihanifl3
    @052rafihanifl3 Місяць тому

    Hello, how much maximum the sensor can read the distance?

    • @NizarMohideen
      @NizarMohideen  Місяць тому +1

      400 cm
      as in www.sparkfun.com/products/15569

  • @wp_me_hedfd5783
    @wp_me_hedfd5783 4 місяці тому

    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

    • @wp_me_hedfd5783
      @wp_me_hedfd5783 4 місяці тому

      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);
      }

  • @prakashgaikwad1125
    @prakashgaikwad1125 Рік тому

    how much distance measure?

  • @rzighaya3250
    @rzighaya3250 Рік тому

    Hi,in the live expressions i got for distance" target is not available,"what could possibly be the problem?

    • @ouarhaous
      @ouarhaous 3 місяці тому

      hello i got the same error do you resolve it ? please if you have any idea could you tell me

  • @auroraramirez730
    @auroraramirez730 2 роки тому

    If I wanted to use two ultrasonic sensors, would the code in the while be copied twice and the pins changed?

    • @NizarMohideen
      @NizarMohideen  2 роки тому +1

      Yes. You can have two sets of code in while loop one after another.

  • @tanujaparmar1076
    @tanujaparmar1076 11 місяців тому

    CAN ANYONE EXPLAIN ME DEBUG CONFIGURATION WITH ST-LINK, THERE IS PROBLEM REGARDING GDB SERVER

  • @alikaan.ozturk
    @alikaan.ozturk 2 роки тому

    Hi, why did you use 0.034/2 instead of " /58 "

    • @NizarMohideen
      @NizarMohideen  2 роки тому +1

      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
      ua-cam.com/video/ZejQOX69K5M/v-deo.html

    • @NizarMohideen
      @NizarMohideen  2 роки тому +2

      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 )

  • @vinodkinoni4863
    @vinodkinoni4863 2 роки тому

    sir what the exact formulla to set different project STM32CubeIDE Settings as system core,analog,timmer etc

    • @NizarMohideen
      @NizarMohideen  2 роки тому +2

      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

  • @catfish7982
    @catfish7982 3 місяці тому

    ❤🧡💛♥💛🧡💛

  • @hajartouil8938
    @hajartouil8938 2 роки тому

    I Need library for Ultrasonic ranger v2.0 IS IT the Same as this sensor

    • @NizarMohideen
      @NizarMohideen  2 роки тому

      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

    • @hajartouil8938
      @hajartouil8938 2 роки тому

      @@NizarMohideen Can i have ur mail adress

  • @katiaihaddadene7457
    @katiaihaddadene7457 2 роки тому

    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

    • @NizarMohideen
      @NizarMohideen  2 роки тому +2

      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
      ua-cam.com/video/ZejQOX69K5M/v-deo.html

    • @NizarMohideen
      @NizarMohideen  2 роки тому +2

      instead of uint16_t Distance = 0; // cm
      float Distance = 0.0; // use for float value of distance

    • @katiaihaddadene7457
      @katiaihaddadene7457 2 роки тому +2

      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

    • @NizarMohideen
      @NizarMohideen  2 роки тому +2

      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 UA-cam channel to get the data to live expression
      Or try video no 64 to get the data to putty or database.

    • @katiaihaddadene7457
      @katiaihaddadene7457 2 роки тому

      Hello please can i use it to measure the water level ?

  • @wp_me_hedfd5783
    @wp_me_hedfd5783 4 місяці тому

    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

    • @wp_me_hedfd5783
      @wp_me_hedfd5783 4 місяці тому

      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);
      }