深度睡眠模式外部唤醒

在使用ESP32时,有时我们不必需要它一直处于运行状态,使ESP32处于深度睡眠模式 意味着可以减少那些在运行时消耗更多功率的活动,只在一些指定的操作时唤醒处理器开始正常工作,比如: 在深度睡眠模式时使用开关唤醒ESP32

零知ESP32有5中不同的电源模式:

1、Active mode
2、Modem Sleep mode
3、Light Sleep mode
4、Deep Sleep mode
5、Hibernation mode

具体的区别见下图:

一、工具原料

1、零知ESP32:http://www.lingzhilab.com/home/introduction.html?gid=184
2、220Ω电阻

二、硬件连接

三、代码


												
	#define BUTTON_PIN_BITMASK 0x200000000 // 2^33 in hex
	 
	RTC_DATA_ATTR int bootCount = 0;
	 
	/*
	Method to print the reason by which ESP32
	has been awaken from sleep
	*/
	void print_wakeup_reason(){
	  esp_sleep_wakeup_cause_t wakeup_reason;
	 
	  wakeup_reason = esp_sleep_get_wakeup_cause();
	 
	  switch(wakeup_reason)
	  {
	    case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
	    case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
	    case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
	    case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
	    case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
	    default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
	  }
	}
	 
	void setup(){
	  Serial.begin(115200);
	  delay(1000); //Take some time to open up the Serial Monitor
	 
	  //Increment boot number and print it every reboot
	  ++bootCount;
	  Serial.println("Boot number: " + String(bootCount));
	 
	  //Print the wakeup reason for ESP32
	  print_wakeup_reason();
	 
	  esp_sleep_enable_ext0_wakeup(GPIO_NUM_33,1); //1 = High, 0 = Low
	 
	  //Go to sleep now
	  Serial.println("Going to sleep now");
	  delay(1000);
	  esp_deep_sleep_start();
	  Serial.println("This will never be printed");
	}
	 
	void loop(){
	  //This is not going to be called
	}
					

导入程序代码,在右侧选择ESP32开发板,然后验证程序,如图:

然后上传程序到开发板,打开串口调试窗口,按下一次开关按钮,可以看到ESP32被唤醒,并且打印唤醒原因。