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_channel_atten(ADC1_CHANNEL_4, ADC_ATTEN_DB_11);

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_ADC, adc+1);

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(), \

   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_ADC, R2),   
   I_SUBR(R0, R1, R2),           // VDD_LOW - VDD_ADC  
   M_BXF(_READ_ADC_DONE),        // Wake if VDD_ADC < VDD_LOW

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

  else {
    // Wake up due to ULP
    uint16_t reason = RTCVAR(WAKE_REASON); RTCVAR_SET(WAKE_REASON, 0);
    switch(reason) {
        // Stop clock and write time to EEPROM
      case WAKE_RESET_BUTTON: 

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. 



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


    1. Sure. I have uploaded the torture test code here:


Post a Comment

Popular posts from this blog

Update: Line adapter for Ozito Blade Trimmer

Update (Dec 2021): If you access to a 3D printer, I would now recommend this solution , which makes it super easy to replace the trimmer line. I have been using it for a few months now with zero issue.

Filament Joiner Part 2 (With Display and Knob)

Thanks to the current corona-virus crisis, the parts I ordered for the filament joiner project were taking forever to arrive. But now that they have finally arrived, I can put them to good use. These were the parts ordered: 0.96" OLED display SSD1306 Rotary switch encoder KY-040 Here is the final circuit diagram: The OLED display is connected to the SDA and SCL pins of the Nano (A4 and A5 respectively), and powered by 5V and GND. The rotary switch encoder is connected as follows: VCC => 5V GND = > GND CLK => D9 DT => D8 SW => D2 My prototype board now looks like this: The updated code for driving the knob and display is available in  heater-with-display.ino in the Github repository . We now have a fairly compact (about 7cm x 5cm) and independent filament joiner (no need to connect to PC) that is driven solely by a 12V power supply. Here's how to use it to join printer filaments. More usage details in my previous post .

Adding "Stereo Mixer" to Windows 7 with Conexant sound card

This procedure worked for my laptop (Thinkpad E530) with a Conexant 20671 sound card, but I suspect it will work for other sound cards in the Conexant family. I was playing with CamStudio to do a video capture of a Flash-based cartoon so that I can put it on the WDTV media player and play it on the big screen in the living room for my kids. The video capture worked brilliantly, but to do a sound capture, I needed to do some hacking. Apparently, there was this recording device called "Stereo Mixer" that was pretty standard in the Windows XP days. This allowed you to capture whatever was played to the speaker in all its digital glory. Then under pressure from various organizations on the dark side of the force, Microsoft and soundcard makers starting disabling this wonderful feature from Windows Vista onwards. So after much Googling around, I found out that for most sound cards, the hardware feature is still there, just not enabled on the software side. Unfortunately, to