So you bought a raspberry pi and have it connected to your printer. Nice! You may have even gone one step further and decided to power it using the printer PSU (which is by default if you are using a BIQU BX). Also nice. However, the downside to that is that your pi will now turn off without a safe shutdown process when you power off your printer.
While this is never going to brick your pi it most definitely can corrupt the SD card and require you to reinstall the OS as well as any other tools that you have installed on top of the OS. Since nobody wants to spend half a day doing that I developed this mod which will safely shut down the pi when the power to the machine is cut.
Take note that this mod is currently in beta but is in it's third iteration so it should be relatively stable. Installing this mod while this message is still a part of this gist means that you are signing on as a beta tester. Please share your experience, positive or negative, in the comments below so that it can be refined.
Of course the pi takes some time to shut down. We therefore cannot simply shut off the power to the machine and expect that the pi will magically be able to shut down safely before the 24V rail collapses. The BIGTREETECH mini UPS 24V solves this issue for us. Connecting it to your printer will provide enough time for the pi to safely shut down before the 24V rail collapses completely. This is only true if there are no heaters on at the point that the power is cut. If heaters are on then the drain from the 24V UPS will be too much and the rail will collapse before the pi can complete the shutdown process.
Also take note that if you are running a pi then you are most likely printing from octoprint. Since octoprint does not support power loss recovery this means that the mini UPS will not be able to offer that functionality at all. However, if you are using octoprint merely as a remote monitoring solution and are initiating the prints from the printer SD card then power loss recovery will be supported since Marlin is taking the lead and octoprint is simply reporting on the Marlin status.
You'll also need a single, dupont jumper lead that is female to female and about 15cm long.
Install the mini UPS in an appropriate location. With the BIQU BX there are mounting holes right within the motherboard enclosure that sit just to the left of the motherboard.
Wire the UPS to the motherboard using the Z max input which is shown in the image below. It's the last input along the line of endstop inputs. The connectors that come with the mini UPS are keyed so there should be no issue with incorrect insertion orientation.
Once you are done installing the mini UPS you'll need to run a signal to the pi which will let it know that it is time to shut down. This is where the dupont jumper cable is used. On the motherboard side connect the jumper cable to the second pin from the right on the SWD header. This pin is marked by the red arrow labeled 'A' in the image below. The arrow labeled 'B' points to an LED which will indicate when the pi has been instructed to shut down by Marlin.
Run the jumper lead along the same path to the raspberry pi that is mentioned for the serial cable jumper leads in this gist: https://gist.github.com/looxonline/89e79b2554771eee8aa8b6492f30400d
On the pi side, connect the female end of the jumper lead to GPIO 4 which is shown in the image below.
That's it for the hardware.
To implement this mod you need to be able to ssh into your pi. Learning how to do that is left as an exercise for the user since the internet is full of info on how to do it.
Once you have ssh'd into the pi you will need to run the command below. Note that if you are updating from a previous version of this mod then you will need to re-run this command and it will update the software on the pi side so that it works with the latest version of the mod.
bash <(wget -qO- https://raw.githubusercontent.com/looxonline/Marlin/BIQUBX_ALMOST_STOCK/pishutinstall.sh)
The pi will prompt you for your admin password which is "raspberry" unless you have changed it. The installation may take a few seconds to a few minutes depending on the state of your system. The command prompt will show the activity as it progresses.
Once the installation is complete you will need to reboot your pi using "sudo reboot".
If you are using this with the BIQU BX then you will need to use the firmware branch over here which has been customized for this functionality. Read all of the associated gists before you use that firmware.
https://github.com/looxonline/Marlin/tree/BIQUBX_ALMOST_STOCK
Once the above steps have been completed you should be able to turn off the printer and watch as the raspberry pi gracefully shuts down before the 24V rail collapses.
If you press the reset button on the BX TFT the pi should remain powered up which allows you to control Marlin resets separately to the pi. If you hold the reset button on the TFT then the pi will shut down. This allows you to force a pi shutdown without actually turning the power off. This can be used to safely shut down the pi if you have an older pi which takes longer to shut down than the mini UPS can keep it powered for.
Good to hear. It's one of my favorite mods. Makes things so much more convenient.