├── RTC_RAM_DeepSleepSimpleExample.ino ├── README.md ├── RTC_RAM_DeepSleepExample.ino └── ESP32_BME280_RTCRAM_Datalogger.ino /RTC_RAM_DeepSleepSimpleExample.ino: -------------------------------------------------------------------------------- 1 | RTC_DATA_ATTR int wakeupCnt = 0; 2 | 3 | void setup(){ // At sleep wake-up programme starts from here! 4 | Serial.begin(115200); 5 | 6 | wakeupCnt = wakeupCnt + 1; 7 | 8 | Serial.println(wakeupCnt); // Note the value is preserved during sleep 9 | 10 | // Now go to sleep 11 | esp_sleep_enable_timer_wakeup(10 * 1000000); 12 | esp_deep_sleep_start(); 13 | } 14 | 15 | void loop(){ 16 | // This section will never run! 17 | } 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP32_RTC_RAM 2 | How to use the ESP32 RTC RAM 3 | 4 | The ESP32 has 8K of spare static RAM known as Real Time Clock Random Access Memory (Static RTC RAM), this can be used to save and retain variables during deep sleep. 5 | 6 | For example, you can use the 8K of RAM to save up to 1000 readings from a sensor which could then be uploaded or retrieved later, the advantage of doing this is a reduction of power consumption as the ESP32 needs to wake, read the sensor and go back to sleep and it can do this usually in a few milli-seconds; typically 45mS, whereas uploading each reading to a server could take up to 2-Secs per reading or 45 times longer and that's for each reading uploaded! 7 | -------------------------------------------------------------------------------- /RTC_RAM_DeepSleepExample.ino: -------------------------------------------------------------------------------- 1 | 2 | RTC_DATA_ATTR int integerExample = -1; 3 | RTC_DATA_ATTR float floatExample = 2.141592654; 4 | RTC_DATA_ATTR char alphabet [26] = " "; 5 | typedef struct{ 6 | float Temp = 20; 7 | float Humi = 50; 8 | float Pres = 1000; 9 | } sensorReadings; 10 | 11 | RTC_DATA_ATTR sensorReadings Readings[50]; 12 | 13 | void setup(){ // At sleep wake-up programme starts from here! 14 | Serial.begin(115200); 15 | integerExample = integerExample + 1; 16 | floatExample++; 17 | alphabet[integerExample%26] = char(integerExample%26+65); 18 | Readings[0].Temp = Readings[0].Temp + 0.1; 19 | Readings[0].Humi = Readings[0].Humi + 0.1; 20 | Readings[0].Pres = Readings[0].Pres + 0.1; 21 | Readings[1].Temp = Readings[1].Temp + 0.2; 22 | Readings[1].Humi = Readings[1].Humi + 0.2; 23 | Readings[1].Pres = Readings[1].Pres + 0.2; 24 | 25 | Serial.println(integerExample); 26 | Serial.println(floatExample); 27 | Serial.println(alphabet); 28 | Serial.print(Readings[0].Temp); Serial.print("\t"); Serial.println(Readings[1].Temp); 29 | Serial.print(Readings[0].Humi); Serial.print("\t"); Serial.println(Readings[1].Humi); 30 | Serial.print(Readings[0].Pres); Serial.print("\t"); Serial.println(Readings[1].Pres); 31 | 32 | // Now go to sleep 33 | esp_sleep_enable_timer_wakeup(10 * 1000000); 34 | esp_deep_sleep_start(); 35 | } 36 | 37 | void loop(){ 38 | 39 | } 40 | -------------------------------------------------------------------------------- /ESP32_BME280_RTCRAM_Datalogger.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | RTC_DATA_ATTR int readingCnt = 0; 6 | 7 | typedef struct { 8 | float Temp; 9 | int Humi; 10 | float Pres; 11 | } sensorReadings; 12 | 13 | #define maximumReadings 290 // The maximum number of readings that can be stored in the available space 14 | #define sleeptimeSecs 600 // Every 10-mins of sleep 10 x 60-secs 15 | 16 | RTC_DATA_ATTR sensorReadings Readings[maximumReadings]; 17 | 18 | Adafruit_BME280 bme; // Assumes I2C connection, so connect BME280 Vcc to 3.3v, Gnd to Gnd, SDA to SDA, SCL to SCL 19 | 20 | void setup() { // At sleep wake-up programme starts from here! 21 | Serial.begin(115200); 22 | Serial.println(SDA); 23 | Serial.println(SCL); 24 | // default settings 25 | bool status = true; //bme.begin(); 26 | // You can also pass in a Wire library object like &Wire2, like this status = bme.begin(0x76, &Wire2) 27 | if (!status) { 28 | Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!"); 29 | Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(), 16); 30 | Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n"); 31 | Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n"); 32 | Serial.print(" ID of 0x60 represents a BME 280.\n"); 33 | Serial.print(" ID of 0x61 represents a BME 680.\n"); 34 | } 35 | Serial.println(readingCnt); // To show ESP32 is awake! 36 | Readings[readingCnt].Temp = bme.readTemperature(); // Units °C 37 | Readings[readingCnt].Humi = bme.readHumidity(); // Units % RH 38 | Readings[readingCnt].Pres = bme.readPressure() / 100.0F; // Units hPa 39 | readingCnt = readingCnt + 1; 40 | if (readingCnt >= maximumReadings) { 41 | for (int r = 0; r < maximumReadings; r++){ 42 | // Now output readings in CSV format to the serial port 43 | Serial.println(String(r)+","+String(Readings[r].Temp)+","+String(Readings[r].Humi)+","+String(Readings[r].Pres)); 44 | } 45 | readingCnt = 0; // Reset counter 46 | } 47 | // Now go to sleep 48 | esp_sleep_enable_timer_wakeup(sleeptimeSecs * 1000000); 49 | esp_deep_sleep_start(); 50 | } 51 | 52 | void loop() { 53 | 54 | } 55 | 56 | /* Example output after 10-readings 57 | 0,20.50,50,1001.10 58 | 1,20.50,50,1001.10 59 | 2,20.70,51,1001.20 60 | 3,20.80,50,1001.20 61 | 4,20.70,51,1001.30 62 | 5,20.70,51,1001.30 63 | 6,20.70,51,1001.30 64 | 7,20.80,51,1001.40 65 | 8,20.80,51,1001.40 66 | 9,20.90,51,1001.40 67 | */ 68 | --------------------------------------------------------------------------------