ESP32 How to keep switching pin without manually writing

Hey guys,

Trying to have PIN 5 on my ESP32 turn on and off ranging from 1Hz to 25Hz depending on what I select. Is there an easier method of doing this besides calling a function to do like

digitalWrite(Pin 5,  HIGH);
delay(20);
digitalWrite(Pin 5, LOW);
delay(20);

Thank you

one way would be to use a task set to a blink rate.

#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/event_groups.h"

#define ESP32_WROVER_LED1 27 // LED D4
#define ESP32_WROVER_LED2 0 // LED D3
#define ESP32_LED 2
#define TaskStack10K 10000
#define TaskCore1 1
#define TaskCore0 0
#define Priority2 2
#define Priority3 3
#define Priority4 4
#define TestPin 4
#define OneK 1000




void setup() 
{
  Serial.begin(115200);
pinMode (ESP32_LED, OUTPUT);
pinMode ( TestPin, OUTPUT);
 xTaskCreatePinnedToCore( fBlinkBuiltIn, "fBlinkBuiltIn", TaskStack10K, NULL, Priority2, NULL, TaskCore1 ); //assigned to core 1
 
}
void loop() {}

void fBlinkBuiltIn( void* pvParameters )
{
  // toggle built in LED off/on
  for (;;)
  {
    vTaskDelay( pdMS_TO_TICKS( 10 ) );
    REG_WRITE( GPIO_OUT_W1TC_REG, BIT2 ); // GPIO2 LOW (clear)
    vTaskDelay( pdMS_TO_TICKS( OneK ) );
    REG_WRITE( GPIO_OUT_W1TS_REG, BIT2 ); //GPIO2 HIGH (set)
  }
  vTaskDelete( NULL );

The blink led task could read a global variable that is changed in another task as a blink rate.

Try the example PWM Generation.

This could solve your problem

ESP32 has a feature called "LedC" which would take care of it for you. Unfortunately, it has a minimum frequency of 10Hz. That still covers more than half the frequency range you wanted.

Ah was a bit busy, thank you for your responses, so first

  1. I have the second core already in use for other tasks and so far my solution was to alternate between both cores to run the digitialWrite code but obviously if both cores are busy I cannot continue.

  2. I have LedC written for another pin and I would prefer to use less than 1Hz but I may include it as a backup to run from 10Hz to 25Hz, its more less just switching that PWM frequency because I know it has to be initialized at that frequency first.

  3. The square wave one actually doesn't look too impossible my only concern is if I can run other things in the meantime while its outputting like a 5Hz 3.3V wave?

It sounds as if your problem is more complicated than just flashing an LED. Have you considered using something like a nano to do the simple hardware handling?

You seem to forget that delay on an ESP32 is not implemented as busy waiting, like on an AVR,
so a delay reschedules the thread and yields to other tasks.

OHHHH UUU RIGHHHTTT, then yeah I would see if I could write another task with a higher priority and when the first task has a delay it would do the second task right?

Considered it but the ESP32 is much more capable

Best would be to have both at the same priority,
but it does not matter much, if the tasks only do blink with delay.

Perhaps you have misunderstood my suggestion.
1: Use the ESP32 to do all your complicated stuff
2: Farm out some (or all) of the hardware handling to a second arduino - which need only be a simple device - a nano, micro, whatever.

Its like if the managing director is too busy to do their job properly, get them a secretary

Ahhh I see what you mean, I was considering this but the space in the enclosure I designed is too little. For now allocating tasks seems better working for the ESP32 as I have no more issues generating a constant square wave.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.