Automatic Power Off Your 3D Printer With Octoprint

by dawidma in Workshop > 3D Printing

1970 Views, 18 Favorites, 0 Comments

Automatic Power Off Your 3D Printer With Octoprint

logo.png

I recently bought a Creality CR-5 Pro H to send my 12 year old Reprap Mendel in its well deserved retirement.

The CR-5 is a very sturdy built fully enclosed printer which allows you print high temperature materials. While I like the mechanics of the CR-5, the electronics is basically on the same level as the original Mendel. It uses a ATMega 2560 based board and expects you to either connect your PC directly via USB to the printer or use a SD card. Both I do not like. Due to that reason it was clear to me to use Octoprint on a Raspberry Pi to interface with the printer.

When it comes to power management the only feature you get from the CR-5 is a mechanical On/Off switch. When a print ends in the middle of the night, the electronics and fans keep running until someone is kind enough to switch the printer off manually.

You find a lot of good tutorials how to control Octoprint via a Raspberry Pi. So I will not talk to this.

What I did not find was a solution for switching on and off the printer the way I wanted it to be. This part is documented within this instructable.

Disclaimer:

This instructable involves handling with mains power! If you don't have sufficient experience and are not qualified for working with mains power, this is not the right project for you. Look instead to a solution using Smartplugs or something similar.

Also this instructable is not a detailed step-by-step guide since I used parts I had at hand and the situation with your printer will be different, so it can only act as an inspiration for your own solution.

Design Goals

The design goals for this solution are:

  • After a print, the printer shall switch off automatically after a print including the Raspberry Pi
  • The switch off shall be possible to trigger manually via
  • Octoprint web interface
  • SSH
  • via a button on the printer itself
  • The raspberry shall be shutdown correctly when the power is switched off
  • When switched off, the there shall be no power consumption
  • All electronics shall be integrated into the printer itself with minimal impact to the housing

Note: I do not see a benefit in remotely switching on the 3D printer, since I always clean the heated bed before printing. This also allows me to check if printer and filament are in good condition. Therefor pressing a button on the printer to power on the device is fine.

Concept

Concept.png

The basic idea is to have a momentary push button (T1) to switch on the printer. This push button provides mains voltage to the printers power supply when pressed. A relay (R1), with its switching contacts in parallel to the push button, is used to latch mains voltage when the push button is released and the printer shall remain on. The relay is controlled by a small electronics that basically implements a RS-Flipflop with a driver for the relay at its output. The flipflop is set by the rising edge of the power supply output which happens in consequence of pressing T1.

Once triggered the power stays on until the Raspberry Pi commands it to turn off via one of its GPIOs. To achieve this the GPIO is connected to the reset of the flipflop.

In addition to that a second push button (T2) is used to trigger the shutdown of the Raspberry Pi without the need of logging in via Octoprint or SSH.

Configuring the GPIOs of the Raspberry Pi

The Raspberry Pi provides so called Device Tree Overlays, which beside other things allow to configure behavior of the hardware. The documentation for this you can find under: https://github.com/raspberrypi/firmware/blob/master/boot/overlays/README

We are interested in two overlays:

  • gpio-poweroff which acts as an output of the Raspberry Pi to indicate the kernel has completed its shutdown procedure and the power supply can be turned off now. This output will be used to reset the flipflop and in consequence cut off the power of the printer.
  • gpio-shutdown is a input pin. When this input gets active, the Raspberry Pi starts its shutdown sequence. The input can be configured (low/high active, pull-up/pull-down resistor) and even provides a debouncing to prevent unintentional shutdowns

To configure the overlays, we have to edit /boot/config.txt on the Raspberry Pis SD card. We add the following lines at the end of the file:

#output indicating RPI is halted (HIGH==halted)
dtoverlay=gpio-poweroff,gpiopin=26

#input for switch to trigger shutdown
dtoverlay=gpio-shutdown,gpio_pin=21,gpio_pull=up,debounce=1000

A reboot of the Raspberry Pi is required to make the changes active.

With that done, if you pull GPIO21 to GND, a shutdown gets triggered.

When a shutdown is completed (either trigged by GPIO21 or via software), GPIO26 will get high.

Note:

gpio-poweroff provides a stange behavior since it shortly toggels the GPIO before leaving it active. I am not sure about the benefit of this, but it seems to be intentional:

The GPIO starts in an inactive state. At poweroff time it is driven active for 100ms, then inactive for 100ms, then active again. It is safe to remove the power at any point after the initial activation of the GPIO.

Due to the usage of the Flipflop, we do not transfer this toggling to the relay control.

Schematic

schematic.png
NE555.GIF

I built the electronics around components I had at hand (Mainly a 5V relay and a beefy DC/DC converter with 5V output to power the Raspberry Pi and my electronics). Since the power supply of the CR-5 runs on 24V and has quite some current capacity left, I decided on powering the Raspberry Pi via the DC/DC converter instead of using a second 5V power supply.

Depending on your printer, you may want to adapt the schematics to fit your needs.

The mighty NE555

In the beginning I tried to build the latching logic in discrete form with transistors, but wasn't successful that way. The fact that the electronics gets powered with the trigger signal itself and needs to avoid re-triggering when the power gets switched off, made it quite complex and I changed over to a NE555 timer.

The block diagram shows that it contains already most of the components needed for the build.

How it works

The electronics and the Raspberry Pi are both powered with 5V by the DC/DC converter. C2 buffers the supply for the NE555 and filters away unwanted peaks.

The NE555's output is directly connected to the relay coil. Diode D1 protects the NE555 from inductive voltage peaks when the coil gets switched off. The LED D2 and R4 are used as a optical status indicator.

When the power gets switched on the capacitor C1 is at 0V and gets charged via R1 to 5V. This implements the pulse detection to set the flipflop. The time constant (R1*C1) for the used application is around 100ms. Once the voltage at the TRG pin is above 1/3 of the supply voltage, the set condition for the flipflop is no longer given and it is waiting to be reseted.

Resistor R2 acts as a pull up, to make sure the NE555's flipflop is not in reset during normal operation.

Resetting the flipflop is achieved with a high level on GPIO26 of the Rasperry Pi. With the configuration in /boot/config.txt we did before, the pin goes high once the shutdown is complete. In this case Q1 gets active through the base resistor R3. The collector of Q1 pulls down the voltage on the RES pin of the NE555, which switches off the flipflop and the coil of R1 is no longer powered. Since in this situation the capacitor C1 is already charged, the flipflop remains off.

Downloads

Mechanics

mech1.png
bracket1.png
bracket2.png
bracket3.png

As stated in the beginning, I wanted to integrate my power switch solution into the printer itself. The CR-5 provides a lot of space for that.

Since I did not want to drill additional holes to the nicely powder coated steel frame, I designed a 3D printed bracket to hold all components of the build. The bracket is clamped to the printers side wall using a plastic frame on the outside. Both parts are bolted together through the already existing ventilation holes. Also the two buttons (power on switch and shutdown key) as well as the LED get accessible through those holes.

For the power button I used a switch salvaged from an old micowave. It is rated for mains voltage with sufficient current capability and has a long travel with a nice clicking sound and good haptic feedback.

The bracket is designed in OpenSCAD in a way that it can be printed in one piece without supports. Even though the stringing looks quite bad on the photo (too low retraction), it was quickly cleaned up.

Prototyping and Building the Electronics

breadboard.png
perfboard.png
perfboard2.png

I usually try to prototype the circuit I design in parallel to the generation of the schematics. This helps me to find errors and do some measurements to confirm my assumptions.

Once the circuit is complete and working, it is transferred to a perfboard and mounted the Raspberry Pi and the DC/DC converter to the bracket. Final testing also looked good.

Integration

mech2.png
usbconnection.png
frontbracket.png

The bracket fits well to the hole pattern of the printers housing. Mounting was a bit fiddly since the space to access the nuts is very limited. If I had to to it again, I would flip around the bracket to improve accessibility.

Dirty little secret

While I used connectors and screw terminals to connect the wires to the perfboard and the Raspberry Pi, I also had to connect USB from the Raspberry Pi to the printers mainboad. The USB plug of the printer is directly facing out of the housing. Since moving the printers mainboard to a different location was no option (due to the mounting studs and cable length), I decided to solder the USB wires directly to the board.

I did cut off the 5V wire in the USB cable to avoid powering the printer electronics through the Raspberry Pi.

From the outside, the panel with the two buttons and the status LED integrates well into the design of the printer.

Octoprint Configuration

installPlugin.png
shutdownprinter1.png
shutdownprinter2.png

Already now the shutdown function in Octoprint will trigger the power off via the relay because of the overlays configured in Step 3.

Also the shutdown button works since this functionality is part of the Raspberry Pi's kernel.

What does not yet work is the automatic shutdown after the print. For this we need to install the plugin 'Shutdown Printer' in Octoprint. The documentation for the plugin can be found here:

https://plugins.octoprint.org/plugins/shutdownprinter/

You can install it using Octoprint's Plugin Manager.

In the settings for the plugin, insert the following command to the field 'Extra command/script':

sudo shutdown -h now

Lessons learned

It took me a long time to get the plugin running the way it should. Two things took me a long time to figure out

  • The 'Extra command/script' gets only executed if you have at least one Mode selected. Even though we do not need a GCode execution, you want to select 'Mode GCODE', otherwise the shutdown will not be triggered.
  • Sometimes it takes up to a few minutes for the shutdown to complete. Octoprint is then already offline, but the printer still powered. I do not know the reason for this, but at the end the printer shuts down correctly.

Conclusion

As always the project took much longer than I had anticipated in the beginning, but I am glad I invested the time. After a few days of usage and testing, it seems to work reliably and provides additonal comfort in the daily usage.

Switching on the printer via the push button works very well. The button gives a good haptic feedback and a short press is sufficient to start the printer.

The 'Shutdown Printer' plugin allows a good configuration of the automatic shutdown.

Also I am happy with the mechanical integration to the printer. The decent panel with the buttons and the LED fit nicely to the printers design. Beside the soldering of the USB cable to the mainboard, the modification did not require any hole to be drilled or cable to be cut.

Your thoughts?

This is my first instructable. So I would like to hear your feedback on the solution as well as on the instructable itself. What did you like, what not? What did you miss?