ESP32 #56: ESP32 Timer & Multiple Timer & Changing Timer

Поделиться
HTML-код
  • Опубликовано: 3 фев 2025

Комментарии • 78

  • @gorjandzundev413
    @gorjandzundev413 4 года назад +3

    Excellent presentation! I really like how you laid it out. Thank you for making this video.

  • @alanchard47
    @alanchard47 Год назад

    still the most useful explanation I have seen. Thank you.

  • @claudiomarques8514
    @claudiomarques8514 Год назад

    Congrats, really great and helpfull to support own applications!

  • @Daniel-kx8ci
    @Daniel-kx8ci 7 лет назад

    Thank you, nice video! Still getting into the ESP32 mindset.

  • @buffplums
    @buffplums 10 месяцев назад

    This is a really helpful tutorial, many thanks fella

  • @youtubecopyrights
    @youtubecopyrights 6 лет назад +1

    Thank you, I was very interested into whether multiple timerMux can be used. There is little easy information on this feature. Your video was very helpful.

    • @pcbreflux
      @pcbreflux  6 лет назад +1

      Hope you see the ESP32_multitimer.ino example there are mutltiple timerMux (timerMux0 and timerMux1) used. If you change a variable (i.e. memory or registers) inside a interrupt this prevent the crash (sometimes on random circumstances) of your code, because different tasks could possible change the same variable at the same time.

    • @pcbreflux
      @pcbreflux  5 лет назад +1

      If you interested in ramping up/down stepper motors with a sine wave (sinusoidal driving) maybe this video is helpful ruclips.net/video/LONGI_JcwEQ/видео.html (just the timer example).

  • @szucspower
    @szucspower 2 года назад

    Great video! It helped me a lot! Thank you for your work!

  • @ingenia-tec5194
    @ingenia-tec5194 3 года назад

    Thanks !! all works :D
    Thank you.

  • @ingenierocristian
    @ingenierocristian 4 года назад +2

    Awesome, I looking for a book who explores in detail these topics, can you recommend me one, please?

  • @KaiGoEnergy22
    @KaiGoEnergy22 9 месяцев назад

    Helpful video, thanks. Just exploring ESP32 for science. I am thinking whether it is possible to use it as a delay generator to trigger our expeirments. We are currently using SRS DG535 or BNC 577. Of course the ESP32 is far off their specs (would be strange if not), but many of our applications are fine with microsecond accuracy. I will give it a go and check what is possible with the ESP32 timers.

  • @szabi2434
    @szabi2434 6 лет назад +2

    Nice tutorial!
    I need some extra functions of the timer which I couldn't find in any of your examples.
    As for example I wish to start timer0 on an external interrupt, then when timer0 elapses it should never reload but should start timer, timer1 should never reload. The loop should start again on the next external interrupt.
    I could find some extra info esp32-hal-timer.c source, so I was playing with timerStart(), timerStop(), timerRestart() but I'm not sure if if it the best way doing it.
    A tutorial for "cascading" timers is more than welcome!

  • @lucianoraffagnini8347
    @lucianoraffagnini8347 4 года назад

    Wonderful explanation, it helped me a lot!!! Thank you

  • @pierreschippers2684
    @pierreschippers2684 3 дня назад

    Hello, this is a fine explanation off esp32 timers but espressif migrate from a 2.x version to new 3.x version and your timer code is not valid anymore. is it possible you make an update

  • @ericksebastianchavezcaipe8013
    @ericksebastianchavezcaipe8013 2 года назад

    csm el mejor vídeo de este día

  • @alviustinambunan5432
    @alviustinambunan5432 5 лет назад

    I have tried to replace led with relay , but it didn't work is there any difference between using relay and LED? , I used esp32 Dev module

  • @manecourquhart3242
    @manecourquhart3242 7 лет назад

    thanks for your knowledge and generosity, this helped me solve a problem, you rule!!!!!

    • @pcbreflux
      @pcbreflux  7 лет назад

      Thank you, pleased to help.

    • @manecolooper
      @manecolooper 7 лет назад

      I have one question regarding this Project; i can´t allocate more tan 96000 bytes of ram, no matter there's free ram ( around 150 k more ccording to compiler, i'm in arduino.it's a big mistery...

  • @RaadYacu
    @RaadYacu 7 лет назад

    Knowledge dump - you are awesome

  • @fabiovsroque
    @fabiovsroque Год назад

    Hi, nice presentations. It worked very well at first, but after a board update it haven't worked anymore. Any help?

  • @ivsuk
    @ivsuk 4 года назад

    Brilliant. Thank you!

  • @Really2950
    @Really2950 7 лет назад

    Very nicely explained, thanks

  • @devyanshgarg6156
    @devyanshgarg6156 7 месяцев назад

    Hey i tried the simple timer code and uploaded it but the serial monitor gives me the guru meditation error after the timer starts. Please help me with that

  • @channedossa
    @channedossa 3 года назад

    Hi,
    thanks for this tutorial.
    I have a esprit ESP32 board with an onboard LED (PIN 4).
    The LED lights up every second. BUt when I wanna reduce the time to 50 ms. The LED doesnt lights up anymore.
    With delay(50) altough te LED lights up.
    What am I doing wrong?

  • @nilyash81
    @nilyash81 2 года назад

    You have changed timerTicks in interrupt routine. Can timerTicks in timerAlarmWrite function be changed in main loop?

  • @vishnukannan947
    @vishnukannan947 3 года назад

    Thank you very much

  • @RENANTENORIOSANTOS
    @RENANTENORIOSANTOS Год назад

    Hi, I would like to know if you can change the esp32 baudrate to 110? Could you help me with this I really need it.

  • @arthuranyakwo6849
    @arthuranyakwo6849 2 года назад

    How do you apply this to generate PWM

  • @adrianomellobass9557
    @adrianomellobass9557 4 года назад

    Boa noite Professor, como que eu faço a seguinte função....se o sensor de nível nao mudar de status em 5 minutos desliga o rele...no Esp32?

  • @rozakanugrah4649
    @rozakanugrah4649 Год назад

    Hallo pak,bisakah bikin tutorial belajar rgbmatrix tanpa lubrary,intinya bikin tampilan text ke rgbmatrix dari dasar banget

  • @fflynnful
    @fflynnful 4 месяца назад

    what's a precaler?

  • @naufaldwilyadi3195
    @naufaldwilyadi3195 4 года назад

    So the timing resolution is 12.5 ns? What about the accuracy? +/- 10 ppm?

  • @cyberdientu
    @cyberdientu 3 месяца назад

    Compilation error: too many arguments to function 'hw_timer_t* timerBegin(uint32_t)'

  • @vishnunaikdhanavath
    @vishnunaikdhanavath 7 лет назад

    Thanks for your video. Sir I am having doubt on ESP32 if you have time please clarify.
    Is it possible to use the ESP32 module for communicating with an external sensor(having inbuilt Bluetooth module) over the Bluetooth and parallelly send the sensor data to the server by using Wi-Fi?
    If it's possible please tell me which code I should use to program ESP32?

    • @pcbreflux
      @pcbreflux  7 лет назад +2

      Sure the ESP32 can do this - see ruclips.net/video/F4tMvfXDSbU/видео.html for a example using Bluetooth and WiFi (now I wouldn't use blufi this was a misleading task) and to read maybe a nRF51822 BLE Sensor see ruclips.net/video/brD3dYTK6Fg/видео.html and for thoughts about a BLE/WiFi Gateway here ruclips.net/video/PBYCG-ypvRY/видео.html. And more on Bluetooth ruclips.net/video/_ELUt2EDUR0/видео.html and here ruclips.net/video/FvIywveUVZw/видео.html.
      The magic happens in FreeRTOS you just start one task for WiFi and an other for Bluetooth and use inter task communication (ruclips.net/video/DLqj01asDM0/видео.html or ruclips.net/video/iunw7qd5Wr4/видео.html) for sync the tasks.
      If you are a beginner have a look at Neil Kolbans (github.com/nkolban/esp32-snippets/tree/master/cpp_utils) library for Bluetooth and WiFi and use the ESP-IDF (github.com/espressif/esp-idf). The Arduino implementation is not really ready yet.
      And a Tip: all tasks (especial WiFi and BLE) need memory - start the tasks witch enough StackDepth to avoid crashing applications [xTaskCreate(...,usStackDepth,..); ].

    • @vishnunaikdhanavath
      @vishnunaikdhanavath 7 лет назад

      Thank you very much sir.

    • @vishnunaikdhanavath
      @vishnunaikdhanavath 7 лет назад

      Dear sir, If I added a new Bluetooth device (sensor Bluetooth module address) in the place of beacon Bluetooth address exist in the code ESP32_blufi_beacon_tracker. ESP32 can read the sensor data? or I should do any extra modification in ESP32_blufi_beacon_tracker code.

  • @AbiChess.tricks
    @AbiChess.tricks 6 лет назад

    How to increase the ticking time from 12.5 ns to higher duration . For me its enough to tick for every 100 milli second

  • @klassichd10
    @klassichd10 7 лет назад

    Thanks for the example code.
    Now I used your example "simpletimer" in a timebase for an interrupt based counter.
    I am quite sure, that all the Code is running on the same core. Showing a website affects the counter value (e.g. @ 10kHz). While couting using Interrupts the "arduinoOTA" does not work. It works as Long as you have no Input signal. But not with an Input signal.
    So, I am still looking for a method to run the wifi and OTA on core 0 and the couter stuff on core 1.
    btw: in your multitimer, what does this mean?
    portMUX_TYPE timerMux0 = portMUX_INITIALIZER_UNLOCKED;
    portMUX_TYPE timerMux1 = portMUX_INITIALIZER_UNLOCKED;
    portMUX_TYPE timerMux2 = portMUX_INITIALIZER_UNLOCKED;
    And why is that NULL?
    hw_timer_t * timer = NULL;

  • @klassichd10
    @klassichd10 7 лет назад

    Thanks a lot for this example.
    Currently I use blocking code (delay) for analogRead capture to feed an 2048 FFT. This is not very smart but is strictly serial and has the advantage of Iess interference with other things like wifi or I2C. But I observe the frequencies are measured a few percent too high.
    Maybe that using a timer would give a more stable time base. But I fear that this would interfere with I2C or wifi. What do you think about that?

    • @pcbreflux
      @pcbreflux  7 лет назад

      Hi, Espressif don't tell us much about how the (binary) WiFi stack is implemented, but as far as i assume WiFi and Timer uses different clock sources (APB_CLK for TIMG and PLL_CLK/APB_CLK for WiFi) and also different priority levels for interrupts, so you only have to fear to slow down your data transfer rates.

    • @klassichd10
      @klassichd10 7 лет назад

      Thanks for the Reply. Whats about I2C? In ESP8266 it was done by SW bitbanging. I am not sure how it is done in ESP32.

    • @pcbreflux
      @pcbreflux  7 лет назад

      From espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf (now over 500 pages ...)
      "The ESP32 has dedicated hardware to communicate with peripherals on the I2C bus" -> no bitbanging -> no interference with the timer.

    • @klassichd10
      @klassichd10 7 лет назад

      Yes, the chip might have the capabilities, but are they used by the Wire.h lib of the ESP32 package? I have no Clou.

    • @pcbreflux
      @pcbreflux  7 лет назад

      Just have look at the code github.com/espressif/arduino-esp32/blob/master/libraries/Wire/src/Wire.cpp uses the github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-i2c.c luckily no bitbanging.

  • @kiromaisus
    @kiromaisus 4 года назад

    Suggestion: make a video dealing with the problem of varying cpu frequency

  • @joellecam9993
    @joellecam9993 3 года назад

    Merci Beaucoup. je cherchais comment ajouter un keypad scruté toutes les 50mms dans une application blynk qui verrouille le loop à 1s.

  • @fredrwema3482
    @fredrwema3482 7 лет назад

    Thanks for this tutorial.
    Is ESP32 for hobbyist only not for production? as Espressif claims on ESP8266 to be for hobbyist only.

    • @pcbreflux
      @pcbreflux  7 лет назад

      Hi, don't see any evidence that the ESP32 is not a product for professionals and also hobbyists. So bring your knowledge to your work, if possible, or build and sell your own products based on the ESP32.
      Maybe have a closer look at the different ESP32 hardware revisions. espressif.com/sites/default/files/documentation/eco_and_workarounds_for_bugs_in_esp32_en.pdf

  • @CalinColdea
    @CalinColdea 4 года назад

    THANK YOU !!!

  • @xingyumli8104
    @xingyumli8104 5 лет назад

    hello sir, can you make a video about the esp32 arduino board timer. i am confused about the timerstart timerrestate timerend,can you show us how to use them?please. thanks.

  • @joaquinpine964
    @joaquinpine964 5 лет назад

    Thank you for the tutorial. I could test the code perfectly, but it seems that any code will crash if the prescaler is at 8 and the alarm counts to 1. That is a time of 1us. I'm needing to interrupt every 100ns and i 'm not finding the solution for this trouble. Do you know how to solve this problem? Thank you in advance

    • @tubelins
      @tubelins 4 года назад

      If the prescaler is 8 you will have only 8 instructions for the microcontroller between interrupts. Probably your code will last more than that and you will always have junk. If you need to process something so fast, you will need an even faster microcontroller/microprocessor.

  • @paulixus
    @paulixus 6 лет назад

    Hi, are you absolutely sure it has a 80MHz timer ? Because all i'm getting is 40MHz. I have a dev board with Wroom-32. thanks.

    • @pcbreflux
      @pcbreflux  6 лет назад

      Popescu Paul Not sure. All I find is the timer depends on the (external) oscillator: External 2 MHz to 60 MHz crystal oscillator (40 MHz only for Wi-Fi/BT functionality) ... maybe max is one tick?

    • @paulixus
      @paulixus 6 лет назад

      pcbreflux thank you for the answer. If the premise is correct, setting the prescaler to 1 will make the blink 80 times faster (80Hz) Right? I'm trying to figure this out because I spent an entire day troubleshooting based on the premise that the timer is working at 80mHz.

    • @pcbreflux
      @pcbreflux  6 лет назад

      Popescu Paul Sorry maybe miss your question. And yes acording to the formular 12.5 ns * 80 -> 1000 ns = 1 us setting the prescaler from 80 to 1 should do this, BUT the minimum setting is 2 see espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf ... from chapter 18. 64-bit Timers ... "A 16-bit clock prescaler, from 2 to 65536".

    • @pcbreflux
      @pcbreflux  6 лет назад

      quote: "The prescaler can divide the APB clock by a factor from 2 to
      65536. Specifically, when TIMGn_Tx_DIVIDER is either 1 or 2, the clock divisor is 2; when TIMGn_Tx_DIVIDER is
      0, the clock divisor is 65536. Any other value will cause the clock to be divided by exactly that value."

  • @amirabozaid5457
    @amirabozaid5457 3 года назад

    thanks a lot

  • @jej5957
    @jej5957 7 лет назад

    Im pretty new to coding how would i convert it to hours?

    • @pcbreflux
      @pcbreflux  7 лет назад

      JonEric Eubanks What is your use case? If you need to run some code every hour maybe use deepsleep to save energy. But for the simple timer there are 60s in a minute and 60 minutes in a hour so multiply the timer for a second with 3600.

  • @laurentl.1134
    @laurentl.1134 4 года назад

    Hello thanks a lot for your video. how can I set an interruption of 58 and 116 microseconds and how can I use the loop during the same time. I need to create interruption with no discontinuity and to continue to code de loop pin order to perform other tasks. Is it possible. Please find my code below. I want to blink a led on GPIO 27 in the loop and to send a signal to led2 and 23 every 58 (BitUn) and 116 (BitZero) microseconds.
    Thanks for your help. I am a beginner so pardon me if my question is not clear enough.
    /* create a hardware timer */
    hw_timer_t * timer = NULL;
    /* LED pin */
    int led = 27;
    int led_2 =2;
    int led_23=23;
    /* LED state */
    volatile byte state = LOW;
    void IRAM_ATTR onTimer(){
    //state = !state;
    //digitalWrite(led_2, state);
    //digitalWrite(led_23, !state);
    //delayMicroseconds(8500);
    BitUn();
    BitZero();
    }
    void setup() {
    Serial.begin(115200);
    pinMode(led, OUTPUT);
    pinMode(led_2, OUTPUT);
    pinMode(led_23, OUTPUT);
    /* Use 1st timer of 4 */
    /* 1 tick take 1/(80MHZ/80) = 1us so we set divider 80 and count up */
    timer = timerBegin(0, 4640, true);
    /* Attach onTimer function to our timer */
    timerAttachInterrupt(timer, &onTimer, true);
    /* Set alarm to call onTimer function every second 1 tick is 1us
    => 1 second is 1000000us */
    /* Repeat the alarm (third parameter) */
    timerAlarmWrite(timer, 1, true);
    /* Start an alarm */
    timerAlarmEnable(timer);
    //Serial.println("start timer");
    }
    void loop() {
    state=!state;
    digitalWrite(led,state);
    delay(2000);
    }
    void BitZero() {
    digitalWrite(led_2,LOW);
    digitalWrite(led_23,HIGH);
    delayMicroseconds(116);
    digitalWrite(led_2,HIGH);
    digitalWrite(led_23,LOW);
    delayMicroseconds(116);
    }
    void BitUn() {
    digitalWrite(led_2,LOW);
    digitalWrite(led_23,HIGH);
    delayMicroseconds(58);
    digitalWrite(led_2,HIGH);
    digitalWrite(led_23,LOW);
    delayMicroseconds(58);
    }

  • @mallewax
    @mallewax 7 лет назад

    Thanks for this video. Am I right that I can use all 4 timers at once without losing time resolution?

    • @pcbreflux
      @pcbreflux  7 лет назад +1

      mallewax Hi, even if the organised in two groups you can use all four timers independently. From the technical manual ... "Each timer uses the APB clock (APB_CLK, normally 80 MHz) as the basic clock".

    • @mallewax
      @mallewax 7 лет назад

      Hi, thanks for the reply. I may have overlooked that information in the manual... But now I understand

    • @pcbreflux
      @pcbreflux  7 лет назад +1

      Don't expect that I read all the informations in the manuals (and I do not expect this from others either) ... i read just the parts that I am currently interested in ... and unfortunately only a little was stored in my long-term memories.

  • @apersaz4178
    @apersaz4178 5 лет назад

    Thanks for the info.
    I am using your example code, to turn on and off a led with "digitalWrtie", but I do not get pulses under 2 microseconds.
    I have adjusted the code in the following way.:
    //===================================================================================
    const int ledPin = 13;
    volatile uint8_t ledOn = 1;
    hw_timer_t * timer = NULL;
    portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
    extern void IRAM_ATTR __digitalWrite(uint8_t pin, uint8_t val){
    if(val) {
    if(pin < 32) {
    GPIO.out_w1ts = ((uint32_t)1

  • @omarhalabi889
    @omarhalabi889 2 года назад +1

    the voice got my head screaming

  • @parmanjosan6687
    @parmanjosan6687 7 лет назад

    thanx for this knowledgeable video, I want to capture time in deep sleep do you have any code for it?

    • @pcbreflux
      @pcbreflux  7 лет назад

      +Parman Josan Hi, have you found a new law of physics to store time? Just a deep sleep joke ... How do wan't keep the time while sleeping? You can sleep for a given time and the ULP cocpu keep the internal clock running. Or after wake up you can get the time from a source like SNTP or external GPS or RTC. What do you prefer? Just using the internal esp32 clock is not precise in most cases.

    • @parmanjosan6687
      @parmanjosan6687 7 лет назад

      pcbreflux , there is a RTC timer and and RTC resistor which also runs in deep sleep mode.I have code of it but it is in different firmware so don't give an advice if you don't know about it,brother.

    • @pcbreflux
      @pcbreflux  7 лет назад

      Parman Josan Sure there is a RTC register, but not seen any affordable modules that came populated with a RTC crystal. And deriving the RTC clock from the 40Mhz crystal is not so accurate.

    • @parmanjosan6687
      @parmanjosan6687 7 лет назад

      okk, tnx @pcbreflux

    • @pcbreflux
      @pcbreflux  7 лет назад +1

      Here are some examples that may fit your use case:
      From Neil Kolban - DS1307 Real Time Clock
      ruclips.net/video/irtxFEZPRrg/видео.html
      From Neil Kolban GPS Code
      github.com/nkolban/esp32-snippets/tree/master/hardware/gps
      From Espressif Arduino Time HAL:
      github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-time.c
      From Espressif SNTP esp-idf example:
      github.com/espressif/esp-idf/tree/master/examples/protocols/sntp
      Andreas Spiess (S)NTP for esp8266:
      ruclips.net/video/qzkNXhubWLg/видео.html
      Andreas Spiess Timed Deep Sleep for esp8266:
      ruclips.net/video/BzFM3PWx1rg/видео.html
      All example need a external time source and (S)NTP only works if your device can connect to a time-server.