Calculating bitmask value for ESP32 external wake up: Google is your friend

I’m currently learning how to use the deep sleep functionality for ESP32. I’ve been following the excellent article by Random Nerd Tutorials ESP32 Deep Sleep with Arduino IDE and Wake Up Sources. I recommend this article if like me you are starting out with deep sleep with the ESP32.

I want my ESP32 to wake up from any one of three external inputs and External Wake Up (ext1) looks like the best option for this. Rather than directly specifying pins, there is a more elaborate method that requires calculating a bitmask that is a combination of all pins. The bitmask value is then used in this command:

esp_sleep_enable_ext1_wakeup(bitmask, mode)

The bitmask value is calculated using this:

  1. Calculate 2^(GPIO_NUMBER) for each of the pins.
  2. Add the totals together.
  3. Convert to hex. Can use to do this.
  4. Use the hex number you’ve obtained in the bitmask variable.

I am using three pins; 27, 32 and 33. For these I need to start by calculating this: 2^27 + 2^32 + 2^33. I started by getting out my ancient trusty Casio calculator and entered 2^27. The result 1.34217 08. Hmmm looks like scientific notation if I remember back 40 years correctly. Doesn’t look promising as it doesn’t have enough digits.

I thought I would try a few things to see what is the easiest way I could calculate it.

Just to see what we have to end up with this is this is the full calculation:

= 2^27 + 2^32 + 2^33
= 134217728 + 4294967296 + 8589934592
= 13019119616
Hex = 0x308000000

I started by entering 2^27 directly into Google search. Nice, it gave the result 134217728

Next I tried entering 2^27 + 2^32 + 2^33 into Google search. Result: 13019119616. Impressive.

Finally I entered convert 2^27 + 2^32 + 2^33 to hex into the Google search. Result: 0x308000000

Very cool and simpler than I was expecting. I’m happy that I know how to manually do the calculation and also that I have an easy way to do it.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Website Powered by

Up ↑

%d bloggers like this: