b. Remote ON/OFF controller with a Wi-Fi device

Raspberry Pi 4b has a global enable pin which can easily be connected to an ON/OFF switch on their casings. With an addition of a small ESP32 board and a 3.3 V voltage regulator the global enable pin may also be operated remotely via a Wi-Fi device.

I assembled a remote on/off Raspberry Pi 4b controller from an ESP32-ROOM module, a LM317T voltage, a BC109C transistor and a small number of capacitors and resistors. BC109C or a similar low power transistor is used as a voltage level adapter form 3,3 V to 5 V. It is important to note that the global enable pin operates on 5 V, but ESP32 module operates on 3.3 V (max. 3,6 V).

The remote on/off Raspberry Pi 4b controller is pretty useful for preventing a young teenager to hang on the Internet all the afternoon after returning from the school...

Finding a suitable Raspberry Pi casing

There is a variety of different Raspberry Pi 4b casings at Internet microelectronics stores, however it is important to buy a one with a fan and not a fan-less metal casing with no room to in-build additional electronics. Of course, there must also be enough room for the internal fan to operate normally after adding the on/off controller circuits.

Concept

ESP32 module runs on 3.3 V, it cannot draw power from 3.3 V power supply pins on 40-pin Raspberry Pi 4b expansion connector. The only pins that remain powered during global enable pin is connected to the ground are 5 V power supply pins, so a 3.3 V voltage regulator is needed to power ESP32 module regardless the global enable pin state.

By analyzing the reduced Raspberry Pi 4b schematic one can quickly observe that global enable pin is connected to the 5 V power supply via a 100 k Ohm pull-up resistor. A low power NPN transistor (ex. BC109C) may be used to pull it to the ground with a ESP32 digital output.

ESP32 module is powered on as soon as 230 AC to 5 V power adapter is connected, but Raspberry Pi is only enabled, if the ESP32 module digital output is a logical 0 and the transistor is closed. Raspberry Pi is internally powered on. When ESP32 digital output has a logical 1 (3.3 V) the transistor is opened and Raspberry Pi global enable is connected to the ground and Raspberry Pi is internally powered off.

ESP32 module also has to remember the Raspberry Pi’s last power state when itself is powered off. This may easily be achieved by storing the Raspberry Pi’s last power state in ESP32 module’s internal EEPROM as it changes. ESP32 module then reloads the last Raspberry Pi’s power state as it boots. If the Raspberry Pi was powered off via a Wi-Fi device, it must also be powered by it after ESP32 module is powered on.

Assembly

Rasberry Pi casing is quite crammed, but there is still enough room to glue a ESP32-ROOM module and a small LM317T based voltage regulator to the bottom side of cover plate. A small circuit is then connected to the upper part of the 40-pin expansion connector. Keep in mind that the fan must remain connected to 5 V power supply in other to provide sufficient cooling to Raspberry Pi 4b internals. The power supply to the LM317T voltage regulator must therefore be connected in parallel to the fan. There is also a small patch of electrical insulation tape needed to prevent inadvertent contact of the circuits glued to the cover plate to the cooling ribs of Raspberry Pi 4b chips.

There is one more thing! A precise hand soldering is needed to correctly connect all the wires to small ESP32 module contacts. The use of a magnification lens is highly recommended.

ESP32 firmware programming with the Raspberry Pi

Why not? Raspberry Pi 4b is more than capable of programming an ESP32 module via its native 3.3 V RS232 port which is available through its 40-pin connector. ESP32 module RxD0 pin must be connected to Raspberry Pi 4b TxD0 pin (GPIO14) and ESP32 module TxD0 pin must be connected to Raspberry Pi RxD0 pin (GPIO16). ESP32 module EN and GPIO0 (boot/programming mode selection) pins must also be controlled by the Raspberry Pi 4b. I connected Raspberry Pi GPIO17 pin to ESP32 module EN pin and Raspberry Pi 4b GPIO18 pin to ESP32 module GPIO0 pin. I also used ESP32 module GPIO23 pin to control Raspberry Pi global enable pin via BC109C transistor.

Raspberry Pi 4b also needs a software library package to directly control GPIO17 and GPIO18 states. Though there are a number of software packages available from the Internet, it is hard to find (and build from source code if executables are not provided) a working package for a 64-bit operating system. Therefore, it may not be a bad idea to stick to a 32-bit Raspberry Pi OS or other 32-bit Linux version for now. I installed wiringPi package, which internally interprets GPIO17 as GPIO0 and GPIO18 as GPIO1. The package also installs gpio command, which can be used from a console to control GPIOs without any programming. To set ESP32 module in programming mode type:

gpio 0 out

gpio 1 out

gpio 0 write 0

gpio 1 write 0

gpio 0 write 1


To normally boot ESP32 module type:

gpio 0 out

gpio 1 out

gpio 0 write 0

gpio 1 write 1

gpio 0 write 1


The remote power on/off controller circuit is designed to normally start Raspberry Pi even with a new ESP32 module with no firmware. It also automatically boots when its power supply is restored, which is determined by resistor pull-ups on EN and GPIO0 lines (see the circuit).

Next, Arduino IDE and Espressif Systems ESP32 board plugin must be installed (see instructions from various Arduino related internet sites). Select ESP32 Dev module and select /dev/ttyS0 port and 115200 kbps speed to verify connection through Arduino IDE serial interface window. You may also test GPIOs operation.

Start ESP32 in programming mode (see the procedure above), compile and upload the firmware with Arduino IDE. My firmware source code is provided from PC USP Projects Download site (ESP32-RasberryPi-controller.zip). It also worth to note that Raspberry Pi uses a native RS-232 interface and it is therefore faster and more reliable.

Telnet terminal emulator application and testing

My ESP32 module firmware is designed to accept telnet commands “-“ (Raspberry Pi 4b OFF), “+” (Raspberry Pi 4b ON) and “?” (displays Raspberry Pi 4b current power state and ESP32 module eeprom status). Any terminal emulation software that supports a telenet connection (ex. PuTTY) should do just fine. Android devices may use Terminus or a similar application.

Can the circuit be simplified?

Yes. If you decide to preprogram ESP32 module on a PC, you can make a simple interface to USB to RS-232 bridge (FTDI interface) just for programming and you can remove it before in-building the ESP32 module to Raspberry Pi casing. Please, note that pull-up resistor on EN (enable input) must remain to properly boot the ESP32 module. The only lines that remain connected to Raspberry Pi 4b 40-pin expansion connector are GND and 5 V power supply and GPIO23 is connected to BC109C that controls global enable.

What about other Raspberry Pis?

Raspberry Pi 3b has implementation of the global enable signal slightly differs from Rasbperry Pi 4b’s due to an inbuilt voltage divider. 1.8 k Ohm : 3.3 k Ohm voltage divider gives 3,23 V on global enable pin when not connected to the ground, but BC109C transistor should still work the same. You may also connect global enable directly to ESP32 module.

What about Raspberry Pi 2b? It only has a 3.3 V run pin that must be connected to the ground to reset the microprocessor, but it has no global enable pin. The run pin may be used instead of the global enable pin, but power consumption in a disabled state would be higher that Raspberry Pi 3b’s or Raspberry Pi 4b’s.

However, the original Raspberry Pi have neither run nor global enable pins, so the circuit below may not be implemented.