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

Hacking an analog clock to sync with NTP - Part 5

This is how it looks after I have put everything together. The Arduino sketch is available here . The 2 jumper wires soldered to the clock mechanism are connected to pins D0 and D1 on the ESP-12 (in any order). When the device first boots up, it presents an access point which can be connected to via the PC or smartphone. Once connected, the captive portal redirects the web browser to the configuration page:     A custom field has been added to the WiFi configuration page to enter the current clock time in HHMMSS format. Try to set the clock time to as close to the current time as possible using the radial dial at the back of the clock so the clock will have less work to do catching up. In the config page, the HTML5 Geolocation API is also used to obtain your current location (so if your web browser asks if you would like to share your location, answer "yes"). This is then passed to the Google Time Zone API to obtain the time and DST offset of your time z...

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...