I am running an ESP32 microcontroller with a Co2 sensor, a real time clock (RTC), and an SD card reader. I will be putting the microcontroller into deep sleep and waking it every hour. I want to switch off the power to the aforementioned components before the microcontroller goes into deep sleep and turn the components back on after the microcontroller wakes up. Can I use 1 BJT npn transistor to switch off power to all three components? I was also thinking of using three transistors with a common base signal... i.e. using just one GPIO pin to control three transistors. Thanks
As long as you don't ever need to turn one of them on while another remains off, this will work. However, be aware that the 2N2222 has a saturation voltage that may be high enough to cause problems if you want to interface these with un-switched devices. Because of that saturation voltage, "ground" for your switched devices will be about 100~300mV higher than your circuit ground, and this will be reflected in their output voltages and may cause problems if they get an input voltage of circuit ground (which to them is negative).
Also note that your 2N2222 will dissipate a significant amount of power if your switched peripherals draw too much current. Given what those peripherals are, it's unlikely that this will be a problem, but it's good to be aware of that anyway.
To overcome both of these problems, the following circuit can be used instead:
The use of a FET instead of a BJT eliminates the problem of saturation voltage, as FETs act like (low-value) resistors when turned on. Switching the high side instead of the low side ensures that the switched components still share a ground with all the other things in the circuit, too.
Note: make sure that M1 is a logic-level FET. Some FETs require substantial drive voltages that you don't have available, but logic-level FETs work on just 5V logic.
Note 2: Since it's a PFET, the logic will be inverted. Apply 5V to its gate to turn off the devices, and apply 0V to turn them on.
Note 3: 3.3V is not enough to turn it fully off, so if you use 3.3V signals you should use a simple gate drive circuit. You may want a gate drive anyway, to help it turn on and off quickly and reliably without demanding too much current from the MCU's output.