Duet 2 – Soft Power Control

Duet 2 Soft Power Control

When running your 3D printer it is never advisable to let it run unattended. That being said, if you want the ability to set the Ender 5 to automatically power down completely using the stock power supply some modification is necessary.

Soft ATX Style Power Control

When it comes to power control modifications for the Duet 2 it is very common to see a dry contact relay modification that controls the 24VDC power supply. As part of my Duet 2 mainboard install I discussed this modification and you can read more about that here.

Dry Contact Relay

Hard Power Control – This is what the stock Ender 5 has which consists of a mechanical switch that controls the mains power (115VAC) which turns the primary supply either on or off. The power cannot be controlled via software or scripts. The power switch will have two positions Off/On similar to a conventional light switch in your home.

Soft Power Control – The ability to shutdown the power supply via software. A soft power control is what is found on all modern computers and mobile phones. A soft power switch will be a momentary button. You press the switch once and the system will boot up. If you hold the switch, the system will execute a graceful shutdown via the operating system.

Full Soft ATX Style Power Control

The solid state relay modification and the associated updates to the config.g file allows you to control the 24VDC supply via the Duet Web Control as well as from scripts via the M80 (on) or M81 (off) G-Code commands.

Duet Web Control ATX Power Software Control

The Full modification discussed in this post provides even more control over the system. This modification will allow you to also control the 5VDC power to the Duet2 mainboard for a full system shutdown. Why? Well as long as the Duet 2 is still powered on so is the PanelDue display, the BL-Touch, and the Duet 2 Web Control server. After my print is complete I want the option to have the G-Code automatically shutdown the the entire printer and not just the 24VDC supply.

The following video shows the Ender 5 after this full soft power conversion. Once implemented the system is powered on with a single momentary button press similar to your desktop PC. Once running the system can be powered off in a few different ways. The first is by pressing the button for more than a second. The second is via G-Code commands:

M81 S1; Turn off printer after cooling 
M98 P/sys/trigger2.g; Complete Shutdown

In the following video you will see the PanelDue display shows an advisory message that the system has initiated shutdown and it is verifying the heaters have cooled to 45C or below before completing the action automatically.

Getting Started

This modification is built upon the standard dry contact relay modification discussed during the install of the Duet2. With the relay modification complete we have enabled “soft” control of the 24VDC Primary PSU only. However, the Duet 2 microprocessor which is powered from the 5VDC PSU is still being controlled via the hard power rocker switch. To upgrade the system to FULL soft power control with the existing PSU we are going to use a inexpensive little board called on OnOff Shim.

Pimoroni OnOff Shim
Pimoroni OnOff Shim

The OnOff Shim has been designed to provide soft power control and thus graceful shutdown for the raspberry pi products. It is really inexpensive and can be easily integrated with the Duet 2 to provide the same kind of soft power capability for your printer.

Pimoroni OnOff Shim

Let’s first discuss how the shim works. The shim is powered directly by the 5v PSU via the microUSB input (see above). The shim monitors a momentary remote mounted switch that represents the printer power button.

  • When the shim is in the “off” mode and the momentary switch is pressed for at least one full second the shim will latch 5v power and transition to the “on” mode which will supply 5v to the Duet 2 5v power input
  • The M581 code in the config.g will monitor the (Duet exp.24 – Estop6) as an input to monitor the momentary power switch
  • When in the “on” mode, if the switch is pressed or G-code M98 is called the shim will pull a logic pin low on the Duet 2 – (Exp Port – Pin 24) that will run the trigger2.g script
  • The trigger2.g script will halt printing, start cooling the bed and hot end, and shutoff the 24VDC primary supply.
  • Once cooled below a set threshold (45C) the Duet 2 will set an output GPIO pin (exp.31 – Heater7) low which will signal the shim to unlatch the 5v power to the Duet 2 microprocessor and everything will be completely shutdown.
Pimoroni Flow Chart

The pins on the Pimoroni are as follows:

OnOff Shim Pinout

The following table shows the end to end connections between the Pimoroni and the Duet 2. The wire colors happen to be what I had available in a 2×6 header ribbon cable I had laying around. Any wire colors will work so long as the end to end connections are the same.

Duet 2 and Pimoroni Wiring

The image below shows the remote button connected with a two pin connector (red and black wires), the microUSB connected 5VDC Power Supply Unit, the external Ethernet port for the Duet 2 Eth, and the output 5VDC connected via dual terminal ferrule (blue blocks).

Pimoroni Mounted on a Generic Project Board

Power Button Wiring and Mounting

The stock on/off rocker switch must be removed and replaced with a momentary button. The Pimoroni supports driving a 3.3v LED so we will use this combination momentary switch/LED.

Mounting the new power switch

Removing the rocker switch will reveal a rectangular 30 x 11mm shaped mounting hole. A simple face plate of larger dimensions will need to be 3D printed and a slightly larger 13mm circular hole drilled in the center of the existing void to accommodate the new push button switch mounting.

Dimensions of the existing mounting hole (orange) vs the new round mounting hole (black)
Switch mounted with 3D printed face plate

The switch will require a washer on the backside that is larger than 13mm in diameter.

Mounted momentary switch

Note: Disregard the raspberry pi in the above image. The image is from a previous mod where I was still using the stock Creality board with the Pimorni controlling a OctoPrint enabled raspberry pi. As far as switch mounting goes, the process is the same.

The switch leads (orange in this image) are connected directly to the Pimornoni board to the two leads marked as “BTN”. The LED leads have a polarity and require validation with a meter in diode mode to determine the anode and cathode of the LED. Another way to determine the polarity is to connect the LED up to a 3.3v supply and if it doesn’t light up, reverse the connection and verify the correct polarity. No damage will be done to the LED if it is connected backwards, it just won’t light up.

  • The LED +3.3v lead are connected to the Duet2 Expansion port pin #43 3.3v output. This ensures the LED is lit only when the Duet is powered on.
  • The LED GND pin is connected to the Pimoroni Pin #6 (GND) or one of the GND pins on the Duet 2 expansion port such as pin #2.

Note: The blue model LED can handle up to 3VDC w/o a resistor.  The Duet outputs 3.3VDC.  You can optionally include a 6ohm resistor but I determined that the switch LED handles 3.3VDC just fine. The red version of this switch is rated for 2VDC so a resistor may be required.

Momentary Switch Wiring

Duet Configuration File Changes

The Duet config.g file requires modification to make the board aware of the discrete inputs.

Config.g

This config file change sets up the Duet IO:

; Soft Power Control
M950 J0 C"^exp.e6stop" ; create GPIO input pin 0 attached to Estop6 expansion connector - Senses Power Button, Low=Button Pressed 
M950 P1 C"!exp.31" ; create GPIO output pin 1 attached to heater7 on expansion connector - Controls Duet2 On=S1 (default), Off=S0
M581 P0 S1 T2 C0         ; When E6 goes Low, execute trigger2.g to complete shutdown
  • Duet Input – Duet Expansion port pin 24 (exp.e6stop) as an input pin to monitor the state of the button press state communicated by the Pimoroni.
  • Duet Output – Duet Expansion port pin 31 (exp.31) as output pin to signal to the pimoroni pin #7 input. This connection signals the pimoroni that the Duet scripts is finished and it should unlatch the 5VDC power.
  • The M581 command tells the duet to execute the following trigger2.g file to complete shutdown. This file ensures we cooled the hotend prior to just removing power from the fans

Trigger2.g

Note: In RepRapFirmware, trigger number 0 will trigger an emergency stop as if M112 had been received. Trigger number 1 will pause the print as if M25 had been received. Any trigger number # greater then 1 will cause the macro file sys/trigger#.g to execute.

We must create a trigger2.g file to handle the button press assigned to our input button discrete as shown above in the “M581 P0 S1 T2 C0” command.

;tigger2.g
M18            ; Disable Steppers
M104 S0; Set Hot-end to 0C (off)
M140 S0; Set bed to 0C (off)
M291 P"Shutdown request active. Waiting for extruder to cool to 45 deg." T5
M109 R45; wait until extruder cools to 45 deg 
M81 S1 ; Turn off printer once cooled down
M42 P1 S1 ; Turn Off Duet Power (inverted logic)

Slicer Settings

The following slicer script G-Code will power the printer on via the M80 and also automatically invoke the M98 shutdown after the print is complete.

Start Script

; Ender 5 Custom Start G-code

M80; Turn on Printer
M117 "Pre-heating Bed"
M140 S[bed0_temperature] ; Set Heat Bed temperature
M190 S[bed0_temperature] ; Wait for Heat Bed temperature
M117 "Pre-heating extruder"
M117 "Heating extruder"
M104 S[extruder0_temperature] ; Set Extruder temperature
G28 ; Home all axes
M117 "Auto bed-leveling"
G29 ; Auto bed-level (BL-Touch)
G92 E0 ; Reset Extruder
M109 S[extruder0_temperature] ; Wait for Extruder temperature

G1 Z1.0 F3000 ; move z up little to prevent scratching of surface
G1 X0.1 Y20 Z0.3 F5000.0 ; move to start-line position

M117 "Purging Material"
G1 X0.1 Y200.0 Z0.3 F1500.0 E15 ; draw 1st line
G1 X0.4 Y200.0 Z0.3 F5000.0 ; move to side a little
G1 X0.4 Y20 Z0.3 F1500.0 E30 ; draw 2nd line
G92 E0 ; reset extruder
G1 Z1.0 F1500 ; move z up little to prevent scratching of surface
M117 "Printing"

; End of custom start GCode

End Script

G91 ;Relative positioning
G1 E-2 F2700 ;Retract a bit
G1 E-6 Z0.2 F2400 ;Retract and raise Z
G1 X5 Y5 F3000 ;Wipe out
G1 Z100 ;Raise Z more
G90 ;Absolute positionning

G1 X0 Y0 ;Present print
;M106 S0 ;Turn-off fan
M104 S0 ;Turn-off hotend
M140 S0 ;Turn-off bed
M81 S1; Turn off printer after cooling

M98 P/sys/trigger2.g ; Complete Shutdown

The following image shows the full modification to include a DC-DC converter to power a PI based NVG camera that will be discussed in a future post.

Ender 5, Duet 2, Relay Mod, DC-DC Camera Supply, Soft Shutdown Mod.
Complete Solution

5 thoughts on “Duet 2 – Soft Power Control

  1. Hello

    I find your soft power off very good. currently I already have a classic power off for 24v, I would like to modify it, but I am still in RRFW 2.0

    can you also include a version of config.g for the RRFW 2.0
    that would be really good,
    I can’t convert from 3.0 to 2.0.
    i have a duex5 card. and I ordered the Pimoroni OnOff Shim card

    ; Soft Power Control
    M950 J0 C “^ exp.e6stop”; create GPIO input pin 0 attached to Estop6 expansion connector – Senses Power Button, Low = Button Pressed
    M950 P1 C “! Exp.31”; create GPIO output pin 1 attached to heater7 on expansion connector – Controls Duet2 On = S1 (default), Off = S0
    M581 P0 S1 T2 C0; When E6 goes Low, execute trigger2.g to complete shutdown
    ;

    thank you very much
    Best regards

    Johann

    1. Johann,

      Unfortunately I do not have a version for the older firmware. I started with the newest stuff so I don’t have a reference for the older FW.

  2. In concept I would like to put in writing similar to this moreover – taking time and actual effort to generate a very good article… but what can I say… I procrastinate alot and also by no indicates find a way to get something completed.

Leave a Reply

Your email address will not be published. Required fields are marked *