I want to control 2 independent processes with 2 PID controllers. Is it possible to run each controller on a separate core? I'm using this PID library. According to its documentation, the PID::Compute() function should be called once every loop(). How can this be achieved when using RTOS tasks (for multi core execution)?

  • why not put both PID::Compute() and PID2::Compute() in loop()? it still will be called once per instance
    – Juraj
    Commented Jul 2, 2020 at 13:35
  • No that would work but the PID computation algorithm will only run on a single core and that's not what I want to achieve.
    – glamis
    Commented Jul 2, 2020 at 14:09
  • still one core runs WiFi
    – Juraj
    Commented Jul 2, 2020 at 16:14

2 Answers 2


Sure. The existing Arduino setup and loop are just executed in their own task:

void loopTask(void *pvParameters)
    for(;;) {

extern "C" void app_main()
    loopTaskWDTEnabled = false;
    xTaskCreateUniversal(loopTask, "loopTask", 8192, NULL, 1, &loopTaskHandle, CONFIG_ARDUINO_RUNNING_CORE);

So there's no reason not to replicate that to give yourself an entire new setup and loop, or just a single function that does it all if you prefer.

TaskHandle_t secondPidTaskHandle;

void secondPidLoop() {
    // setup code here
    for (;;) {
        // PID loop code here

void setup() {
    xTaskCreateUniversal(secondPidLoop, "secondPidLoop", 8192, NULL, 1, &secondPidTaskHandle, 0);

void loop() {
    // First PID code

The last parameter to xTaskCreateUniversal() is the core to execute on. The default setup/loop runs on core 1:


So just use 0 and it'll run it on core 0.



Page 620

As mentioned before, only an elevated process with PID of 0/1 can initiate a process switch. The new process may have any PID from 0 ~ 7 after the process switch. The key for successful proactive process switching is that Espressif Systems 620 Submit Documentation Feedback ESP32 Technical Reference Manual V4.3 29. PID Controller when the last command of the current process switches to the first command of the new process, PID should switch from 0/1 to that of the new process. The software procedure for proactive process switching is as follows:

  1. Mask all the interrupts except NMI by using software.
  2. Set register PIDCTRL_NMI_MASK_ENABLE_REG to 1 to generate a CPU NMI Interrupt Mask signal.
  4. Configure register PIDCTRL_PID_NEW_REG.
  5. Configure register PIDCTRL_LEVEL_REG and PIDCTRL_FROM_n_REG.
  7. Revoke the masking of all interrupts but NMI.
  8. Switch to the new process and fetch instruction. Though we can deal with interrupt nesting, an elevated process should not be interrupted during the process switching, and therefore the interrupts have been masked in step 1 and step 2. In step 3, the configured values of registers PIDCTRL_PID_DELAY_REG and PIDCTRL_NMI_DELAY_REG will affect step 6. In step 4, the configured value of register PIDCTRL_PID_NEW_REG will be the new PID after step 6. If the system is currently in a nested interrupt and needs to revert to the previous interrupt, register PIDCTRL_LEVEL_REG must be restored based on the information recorded in register PIDCTRL_FROM_n_REG in step 5. In step 6, after the values of register PIDCTRL_PID_CONFIRM_REG and register PIDCTRL_NMI_MASK_DISABLE_REG are set to 1, PID Controller will not immediately switch PID to the value of register PIDCTRL_PID_NEW_REG, nor disable CPU NMI Interrupt Mask signal at once. Instead, PID Controller performs each task after a different number of clock cycles. The numbers of clock cycles are the values specified in register PIDCTRL_PID_DELAY_REG and PIDCTRL_NMI_DELAY_REG respectively. In step 7, other tasks can be implemented as well. To do this, the cost of those tasks should be included when configuring registers PIDCTRL_PID_DELAY_REG and PIDCTRL_NMI_DELAY_REG in step 3. Espressif Systems 621 Submit Documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.