Skip to main content

ESPCLOCK4 - Monitoring supply voltage via ADC

To monitor the battery pack voltage (4 x AA; max ~6V), I hooked up GPIO32 (maps to ADC1_CHANNEL_4) to a voltage divider consisting of 2 x 10K resistors. This divides the max voltage in half, giving us 3V, which is clear of the max 3.3V input voltage accepted by the ESP32 ADC pins.

To initialize the ADC channel so that it can be used by ULP code, this function is used:

void init_adc() {
  adc1_config_width(ADC_WIDTH_BIT_12);
  adc1_config_channel_atten(ADC1_CHANNEL_4, ADC_ATTEN_DB_11);
  adc1_ulp_enable();
}

I created 2 new RTC_SLOW_MEM variables:

enum {
  ...
  VDD_ADC,        // Supply voltage measured by ADC
  VDD_LOW,        // Low voltage threshold
}

The ADC reading ranges from 0 to 4095 in a slightly non-linear mapping, so I use this code to figure out the right ADC value for the low voltage threshold for the battery pack (target: 1.05V x 4 = 4.2V).

#define SUPPLY_VLOW (1050*4)    // 4 x 1.05V

// Map given ADC value to corresponding voltage
uint32_t adc_to_voltage(uint16_t adc) {
  esp_adc_cal_characteristics_t adc_chars;
  esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, 1100, &adc_chars);
  return esp_adc_cal_raw_to_voltage(adc, &adc_chars);
}

for (uint16_t adc=2000; adc<4096; adc++) {
  uint32_t v = adc_to_voltage(adc);
  if (v >= SUPPLY_VLOW/2) {
    RTCVAR_SET(VDD_LOW, adc);
    RTCVAR_SET(VDD_ADC, adc+1);
    break;
  }
}

The ULP code below makes a series of 8 ADC readings, computes the average and writes it to VDD_ADC just in case the main CPU needs to use the value. Then it compares the average reading with VDD_LOW. If lower, it wakes the main CPU with a new wake reason WAKE_VDD_THRESHOLD_LOW. The main CPU will then stop the clock and write the clock time to the EEPROM.

// R0 = R0 + ADC reading from GPIO33
#define X_ADC() \
   I_ADC(R1, 0, VDD_CHANNEL), \
   I_ADDR(R0, R0, R1)

// Sum 8 x ADC readings from GPIO33
#define X_ADC_SUM() \
   X_ADC(), \
   X_ADC(), \
   X_ADC(), \
   X_ADC(), \
   X_ADC(), \
   X_ADC(), \
   X_ADC(), \
   X_ADC()

   ...
   I_MOVI(R0, 0),
   X_ADC_SUM(),                  // R0 = sum(8 x ADC readings)
   I_RSHI(R0, R0, 3),            // Divide by 8 to get average ADC reading
   X_RTC_SAVE(VDD_ADC, R0),      // Save to RTCMEM[VDD_ADC] for main CPU
   X_RTC_LOAD(VDD_LOW, R1),
   X_RTC_LOAD(VDD_ADC, R2),   
   I_SUBR(R0, R1, R2),           // VDD_LOW - VDD_ADC  
   M_BXF(_READ_ADC_DONE),        // Wake if VDD_ADC < VDD_LOW
   X_RTC_SAVEI(WAKE_REASON, WAKE_VDD_THRESHOLD_LOW),
   I_WAKE(),
   I_HALT(),
M(_READ_ADC_DONE),
   ...

In the main setup() code, all we have to do is to deal with another wake reason:

  #define WAKE_VDD_THRESHOLD_LOW 2
  
...
  else {
    // Wake up due to ULP
    uint16_t reason = RTCVAR(WAKE_REASON); RTCVAR_SET(WAKE_REASON, 0);
    switch(reason) {
      case WAKE_VDD_THRESHOLD_LOW:
        // Stop clock and write time to EEPROM
        break;
      case WAKE_RESET_BUTTON: 
        esp_hard_restart();
    }
  }
  ...

The test went quite well when I hooked up GPIO32 to a variable voltage source. When the voltage drops below 4.2V, the main CPU is woken up with the correct wake reason. 

ESPCLOCK1 / ESPCLOCK2 / ESPCLOCK3 / ESPCLOCK4

Comments

  1. Can you share the code for espclock4 in GitHub, especially the one with the PWM modification?

    TIA

    ReplyDelete
    Replies
    1. Sure. I have uploaded the torture test code here:
      https://github.com/victor-chew/espclock4

      Delete

Post a Comment

Popular posts from this blog

Cooling mod for the X96 Air #2

Previously, I added a USB cooling fan to the X96 Air TV box . The problem with this mod is that the fan is always running, and it runs at full speed. Ideally, the fan should kick in only when the CPU temperature is above a certain threshold. It would be even better if there is a way to control the fan speed. Dan McDonald left me a comment pointing to his project on Github . He basically connected the fan to a USB relay that can be controlled by Python script. His project inspired me to make a similar mod that would make use of the spare D1 Mini boards I have lying around. The plan is to hook up the fan to a MOSFET (2N7000) and control it via PWM. Here's the very simple circuit: The code simply reads a single character from the serial port (0 - 9). 0 will turn the fan off, while 1 - 9 will generate a proportional PWM to drive the fan, with 1 being the lowest and 9 being the highest. Here's the Arduino code: #include <Arduino.h> void setup () { Serial . begin ( 9600 ...

Installing and customizing CoreELEC in X96 Air

I previously installed CoreELEC on another TV Box ( Ugoos X3 Pro ), which unfortunately died after only 9 months during the summer (due to the unit overheating, which I learned is a common problem for cheap Android TV boxes). So this time I purchased a X96 Air  (4GB/32Gb) and had to do the whole thing again. So this is a note-to-self in case I ever have to install CoreELEC again on some other device. Installation of CoreELEC is simple enough by following this guide . Basically, it involves downloading and writing the firmware to a microSD card using usbimager . Then insert the microSD card, reset the unit and hold the reset until the logo appears. The unit will then proceed to boot into CoreELEC. First thing is to connect to WiFi, then enable SSH. This allows me to login via ssh and execute: ceemmc -x from the terminal. This writes CoreELEC to the built-in eMMC storage, after which I am able to remove the microSD card and reboot the unit into CoreELEC via the built-in sto...

DC-DC Buck Stepdown Converter for ESP8266

I am working on a project that requires a step-down converter from 12V to 5V, that will then power a WeMOS D1 Mini. I saw this new mini buck converter based on the usual LM2596 MP2307 , so I thought I'd give it a try. Unfortunately, it didn't work. Although it is supposed to be able to supply up to 1.8A, the D1 Mini was not able to boot up. The 5V pin was being properly supplied, but the 3.3V pin measures at only ~1.3V. So I had to go back to my usual LM2596 module, which is much larger, but works to power the D1 Mini with a 12V source. Here's a great review of the mini buck converter I found while trying to figure out how to make it work. The fact that it has high quiescent current (~60mA) is also mentioned in a few other sources.