Measuring the remaining capacity of the battery might be desirable to warn the user that it’s time to connect the device to a PC or voltage supply. The microcontroller’s ADC can measure the battery’s voltage, however, the microcontroller’s pins cannot be exposed to a higher voltage than Vcc + 0.5V (datasheet). With a Vcc of 3.3V, that becomes 3.8V, 0.5V less than the battery’s voltage of 4.2V at full charge. Also, we need to sample a voltage below the reference of 3.3V. A voltage divider will be needed. Since we don’t want to drain the battery through the divider, transistors controlled by the microcontroller will be used to turn the divider ON or OFF.
Two transistors are used. The microcontroller is powered by a 3.3V regulator, but the battery charges up to 4.2V. How then, do we switch a P-channel MOSFET connected to the battery's positive terminal completely off? That's the job of the robust BJT, which when not active releases the MOSFET's gate to be pulled up. Thanks to http://www.microbuilder.eu/ for the circuit.