Notifications
Clear all

Hardware interrupt on ESP32

12 Posts
4 Users
3 Likes
2,474 Views
(@khylan)
Member
Joined: 4 years ago
Posts: 4
Topic starter  

I am looking at automating some curtains. Have found a DC motor to use and it contains a hall effect sensor with 6 pulses per revolution. Since I do not want to miss a pulse, I want to use a hardware interrupt like on the Arduino. I also want a WIFI connection to the 4 curtains to control them with a cell phone. The ESP32 looks perfect except I see no hardwire interrupt in the data sheets. Am I right or is there a work around in software that work?


   
Quote
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 

For my understanding, could you clarify what you mean by a "Hardware Interrupt"? Thanks.


   
ReplyQuote
(@khylan)
Member
Joined: 4 years ago
Posts: 4
Topic starter  

@yurkshirelad By hardware interrupt I mean a hardware input that can be used to immediately stop the current task the processor is doing and transfer control to an interrupt function. When the function ends the former task would then continue in the processor. I used this in the Arduino with a flow switch that had a hall effect sensor. The interrupt function simply added 1 to a variable and on a timer I looked at the total in the variable to see how many pulses I had to calculate the flow and then I zeroed the variable. This allowed me to essentially multitask the Arduino.


   
ReplyQuote
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 

Thanks, I wanted to make sure I understood your question.

The ESP32 can definitely do this:

gpio_set_intr_type(INPUT_PIN, GPIO_INTR_ANYEDGE);
gpio_isr_handler_add(INPUT_PIN, ISR_HANDLER, NULL);

void IRAM_ATTR ISR_HANDLER(void* arg) {
    if (gpio_get_level(INPUT_PIN) == HIGH) {
        is_triggered = true;
    } else {
        is_triggered = false;
    }
}


   
ReplyQuote
(@khylan)
Member
Joined: 4 years ago
Posts: 4
Topic starter  

@yurkshirelad Excellent! Thanks for the code. Now I am going to order the ESP32s and the motors and start constructing. 


   
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1596
 

Hi @yurkshirelad and @khylan,

  Answering the queries in reverse order:

  • Simplistically:
    • an interrupt is a means of temporarily halting the main flow of a program, and running a 'usually short', self-contained chunk of program code to deal with something that has just 'appeared'.
    • When the processor finishes the chunk of code, it returns to its 'main flow program and continues.
    • Interrupts are used when events may 'appear' at any time and they must be dealt with quickly.
    • A 'hardware interrupt' refers to an interrupt which 'appears' as a change of logic level to the processor. This logic level change may be applied to an external pin on the processor or it may be internal to the processor, such as clock counting down to zero.

 

    I am sure there will be many others to explain the finer details.

Best wishes,

Dave


   
ReplyQuote
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 

I'm pretty sure the Arduino interrupt code works as well on an ESP32:

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);

 

It's worth doing some reading and searches to find example code.


   
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1596
 

Hi @yurkshirelad,

    Sorry, if my reply to you was too 'detailed'  ... it is tricky knowing what level to answer a question when you are not in a face-to-face chat.

Dave


   
ReplyQuote
(@yurkshirelad)
Member
Joined: 3 years ago
Posts: 493
 
Posted by: @davee

Hi @yurkshirelad,

    Sorry, if my reply to you was too 'detailed'  ... it is tricky knowing what level to answer a question when you are not in a face-to-face chat.

Dave

No worries Dave, understood. 👍 I understand the concepts of interrupts, I was making sure I was on the same page as Khylan so I could reply.


   
DaveE reacted
ReplyQuote
(@tedbear)
Member
Joined: 5 years ago
Posts: 61
 

@khylan I built a device that I call a "Batcher" I use this to display the running total volume when I mix a batch of spray for my farm crop sprayer.

I used a flow meter connected to the Interrupt Pin on an Arduino Nano.  Each pulse from the Hall Effect sensor in the flow meter causes an Interrupt which increments a counter. The value of this counter is divided by a calibration number to convert the pulses to gallons (could be liters) and sends this value to a large four digit seven segment display. I use a push button to reset the counter back to zero. I also used some other push buttons and a second Interrupt to allow me to change the calibration number.  This calibration number is written to a nonvolatile address so that it is available after a loss of power.

I have considered trying to do this with an ESP32 where the results could be shown on my phone via Bluetooth or Access Point. 


   
Inst-Tech reacted
ReplyQuote
(@khylan)
Member
Joined: 4 years ago
Posts: 4
Topic starter  

@yurkshirelad What my question entailed is whether the interrupt was a hardware or software interrupt. A hardware interrupt is a more reliable service since all processes are halted. A software interrupt may be delayed slightly by a higher priority thread. The Raspberry Pi uses software interrupt as opposed to the Arduino hardware interrupt. 

Since I am going to be positioning curtains by counting pulses from a hall effect sensor I thought the hardware option would be a better choice but I am going to read the documentation more closely and do some tests when my ESP32 arrives. 


   
YurkshireLad reacted
ReplyQuote
(@davee)
Member
Joined: 3 years ago
Posts: 1596
 

Hi @khylan,

  In comment to ..." What my question entailed is whether the interrupt was a hardware or software interrupt. A hardware interrupt is a more reliable service since all processes are halted. A software interrupt may be delayed slightly by a higher priority thread. The Raspberry Pi uses software interrupt as opposed to the Arduino hardware interrupt. "

Your conclusion is broadly correct ... however, both R-Pi and Arduino boards can, in principle, support hardware interrupts ... the difference is R-Pi boards (excluding Pico) are provided with a general purpose computer operating system, most commonly "Raspberry Pi Os" (formerly Raspian), which is a Linux variant, and it is this which prevents hardware access by 'user' programs.

Operating systems such as Linux and Windows are designed to allow many programs to run at the same time, with the OS acting as a 'policing superglue' to  access to keyboard, screen, discs, memory etc., without each program needing to be specifically designed to communicate and share with all of the others. To enable access to hardware based things (including hardware interrupts) to be 'policed', only the operating system has the required access privilege. Furthermore, 'vanilla' Linux and Windows are not generally designed for 'hard real-time' applications in which the response time to an event is ALWAYS required to be below a few milliseconds, so that missing events on this time scale will be a concern.

Adding an interface with direct hardware interrupt access to a R-Pi running R-Pi Os is likely to mean building it into the operating system. In principle this is feasible but complex.

In principle, the R-Pi could run an alternative operating system, but I have no experience of that happening.

By contrast, the default Arduino 'operating system' is so minimal, it barely exists ... It is essentially a 'bare metal' system in which the only software is that specifically required for the task, in which the main software program follows a single thread, broken ony by the 'occasional' hardware interrupt. The Arduino IDE provides a convenient packaging system to automatically select and arrange the (small-ish) pieces of code for housekeeping tasks, such as initialising the microcontroller at power up. This 'bare metal' approach means the hardware interrupt structure is still available for user programs, greatly simplifying the task compared to say Linux.

-------

The R-Pi Pico is quite different from the rest of the R-Pi family ... it is much simpler chip, which does not run R-Pi Os. Bill has produced one of his excellent demonstrations, including a section on interrupts. See https://dronebotworkshop.com/pi-pico/ for the blog, including link to the video.

The ESP86 and ESP32 boards obviously provide a low cost processors with WiFi, which have become 'adopted' into the Arduino fold, and benefit accordingly.

--------

Good luck with your project. Dave


   
ReplyQuote