The Switch module takes care of basic input from things like buttons and switches and controls simple devices like fans and pumps.
It is an incredibly versatile tool that allows you to setup a lot of on-off type systems. It listens to inputs or custom G/M-codes, and outputs to GPIO pins or outputs custom G/M-codes.
This allows you to do one of the following :
- Make a G-code control a GPIO pin
- Make a GPIO pin cause a G-code to be executed
- Make a GPIO pin control another GPIO pin
- Make a G-code cause another G-code to be executed
You can create several different switch modules to fit your needs, within the same configuration file.
Like TemperatureControl, there can be multiple Switch modules. All you need to do is give each module it's own name in the config file.
switch.fan1.enable true switch.fan1.output_pin 2.7 etc ... switch.fan2.enable true switch.fan2.output_pin 2.6 etc ... switch.zplus10.enable true switch.zplus10.input_pin 1.7 switch.zplus10.output_on_command G91G0Z10G90 # G90 and G91 switch to relative positioning then back to absolute etc ...
The initial internal state of the switch at boot is set by the startup_state setting, which should be set to “true” or “false”.
Also remember that individual pins can be inverted with a '!' ( see Pin Configuration ). Default is false.
There is also a startup_value setting that sets the default analog value used for pwm on an output pin. This value defaults to always on.
switch.fan1.startup_state false switch.fan1.startup_value 127
This setting will enable a pin that can be used to change the state of the switch. For example, a button can be configured that toggles the state of a fan. By default input_pin is set to “nc” which stands for “not connected”.
There is also a behavior setting for the input pin. Currently the valid options are “momentary” (default) and “toggle”.
The toggle behavior allows a momentary button to behave like an on-off toggle switch. If you are connecting a physical toggle switch you would probably want the behavior set to momentary.
switch.fan1.input_pin 1.7! switch.fan1.input_pin_behavior toggle
Set this config value to drive an output pin based on the internal state of the Switch module. Remember that the pin can always be inverted with a '!' ( see Pin Configuration ).
switch.fan1.output_pin 2.7 switch.fan1.output_type pwm # pwm output settable with S parameter on the on command switch.fan1.max_pwm 255 # sets the max pwm for this pin
To set an output pin to be non-pwm so it just turns on or off set output_type digital
switch.psu.output_type digital # just on or off switch.psu.output_pin 1.30o! # set to open drain, inverted to control an ATX PSON signal
There are three different output types : digital, pwm and hwpwm, the default is none so no output pin is configured.
Note that pwm is actually SigmaDelta Modulation and will allow you to set PWM intensity via the Shift parameter to your G-codes, values between 0 and max_pwm are accepted, which is usually 255.
hwpwm is PWM controlled by the Hardware, and is PWM compatible with Hobby servos and ESCs. The Shift parameter specifies the duty cycle in percent, and for a typical servo will be between 5% and 10% (1ms to 2ms when running at 50Hz) for a 180Â° turn. the default frequency is 50Hz but can be set with the pwm_period_ms config setting.
Commands and Gcodes
There are also a set of config settings that allow the Switch module to both generate and react to Gcodes as necessary. The input_on_command is also able to read an S parameter to set an analog value for pwm over the output pin. This allows things like driving a fan at less than full speed or dimming an led.
switch.fan1.input_on_command M106 # any command that starts with this exact string turns this switch on switch.fan1.input_off_command M107 # any command starting with this exact string turns off the switch
In addition to input_on_command and input_off_command there are also corresponding config settings output_on_command and output_off_command. Offhand, it seems unlikely that a single switch module would need to use both input_ and output_ commands.
This configuration will allow you to control a fan using the standard reprap G-codes for controlling a fan
This is already present in the default configuration file
# Switch module for fan control switch.fan.enable true # Enable this module switch.fan.input_on_command M106 # This switch is turned on when M106Â is sent switch.fan.input_off_command M107 # This switch is turned off when M107 is sent switch.fan.output_pin 2.6 # This pin is turned on when this switch is turned on, and vice-versa switch.fan.output_type pwm # PWM output settable with S parameter in the input_on_comand #switch.fan.max_pwm 255 # Set max PWM for the pin default is 255
This configuration will allow you to control a servo using the standard reprap G-codes for controlling a servo.
M280 S5 would be fully to the left and M280 S10 would be fully to the right.
# Switch module for servo control switch.servo.enable true # Enable this module switch.servo.input_on_command M280 # M280 S7.5 would be midway switch.servo.input_off_command M281 # Same as M280 S0 0% duty cycle, effectively off switch.servo.output_pin 3.25 # Must be a PWM capable pin switch.servo.output_type hwpwm # H/W pwm output settable with S parameter in the input_on_comand #switch.servo.pwm_period_ms 20 # set period to 20ms (50Hz) default is 50Hz
# Switch module for a second servo control switch.servo2.enable true # Enable this module switch.servo2.input_on_command M280 # M280.1 S7.5 would be midway switch.servo2.input_off_command M281 # Same as M280.1 S0 0% duty cycle, effectively off switch.servo2.subcode 1 # M280.1 will trigger this switch switch.servo2.output_pin 3.26 # Must be a PWM capable pin switch.servo2.output_type hwpwm # H/W pwm output settable with S parameter in the input_on_comand
To find a PWM-capable pins, see Pinout
Power supply control
Here is how to control an ATX power supply's ON/OFF signal from a bare pin connected to the PS_ON signal, so that your board can tell it to turn off when needed
switch.psu.enable true # turn atx on/off switch.psu.input_on_command M80 # switch.psu.input_off_command M81 # switch.psu.output_pin 0.25o! # open drain, inverted switch.psu.output_type digital # on/off only switch.psu.fail_safe_set_to 1 # so the ATX turns off on a system crash #switch.psu.ignore_on_halt true # so the ATX does not turn off on a HALT condition (like limit trigger) # However leave commented or set to false if you want the ATX to turn off for an over heat fault condition
Note : this uses the PSON pin on the power supply, which should be open-drain, thus the o in 0.25o!
Here is how to control an ATX power supply's ON/OFF signal from a small mosfet connected to the PS_ON signal, or to an SSR which powers the non ATX PSU
switch.psu.enable true # turn atx on/off switch.psu.input_on_command M80 # switch.psu.input_off_command M81 # switch.psu.output_pin 2.4 # small mosfet (NB not inverted) switch.psu.output_type digital # on/off only #switch.psu.ignore_on_halt true # so the PSU does not turn off on a HALT condition (like limit trigger) # However leave commented or set to false if you want the PSU to turn off for an over heat fault condition
Pause when out of filament
This configuration allows you to use a pin to detect when the machine is out of filament. When the switch is hit by the filament not being present, the machine is put into pause.
Another switch is configured to allow you to resume the machine once the button is pressed.
Additional configuration allows you to specify commands that are executed when the machine suspends, and when it resumes.
switch.filamentout.enable true # Enable this module switch.filamentout.input_pin 1.30^ # Pin where filament out button is connected switch.filamentout.output_on_command suspend # Suspend command switch.resume.enable true # Enable this module switch.resume.input_pin 1.31^ # Pin where resume button is connected switch.resume.output_on_command resume # Resume command after_suspend_gcode G91_G0E-5_G0Z10_G90_G0X-50Y-50 # Gcode to run after suspend, retract then get head out of way before_resume_gcode G91_G1E1_G90 # Gcode to run after temp is reached but before resume - do a prime
Note, there is a real filament detector module which works much better than this, see filament-detector.
Suspend and resume buttons
This configuration allows you to set a suspend button, and a resume button.
switch.suspend.enable true # Enable this module switch.suspend.input_pin 1.30^ # Pin where pause button is connected switch.suspend.output_on_command suspend # Suspend command switch.resume.enable true # Enable this module switch.resume.input_pin 1.31^ # Pin where resume button is connected switch.resume.output_on_command resume # Resume command after_suspend_gcode G91_G0E-5_G0Z10_G90_G0X-50Y-50 # Gcode to run after suspend, retract then get head out of way before_resume_gcode G91_G1E1_G90 # Gcode to run after temp is reached but before resume - do a prime
Stopping SmoothieThese are the different ways of stopping Smoothie :
|abort||M26||Stops an SDCARD print immediately||Not affected||Aborts||Position maintained, but file must be restarted||Player||Stops the execution of a file being played from SDCARD, it will complete the current gcode, but stop immediately after that, the rest of the queued commands are discarded. It attempts to maintain the correct position after the abort.|
|suspend||M600||Stops once queue is empty||Turned off if option enabled (default)||Paused, can be resumed||Yes, with resume or M601, position maintained||Player||Suspends the execution of a file being played from SDCARD or being streamed from a host (upstream support required currently pronterface and octoprint support it, otherwise host needs to be manually paused), all state is saved and jogging and extruding is allowed. Mainly used for mid print filament change, or filament out detection. M601 resumes the print or the resume command|
|No command, but there is a configurable «kill» button||M112||Stops instantly if kill button pressed, if issued from host has to wait for the receive buffer to have room.||Turned off||aborted||No, position is lost, home will be required||supported-g-codes||Instantly stops all operations, printer fully halts until M999 is sent. Position is lost.|
|Sending Control-X to smoothie over the serial port or USB serial port||should work at any time even when streaming, does the same as the kill button||Turned off||aborted||No, position is lost, home will be required||Instantly stops all operations, printer fully halts until M999 is sent (or $X). Position is lost.|
If the kill button is pressed (or there is a temperature fault, M112 is issued, a limit switch is hit or other error) the system enters the Halt state, in this state the play led flashes, and the state can be cleared by issuing M999 or holding the flashing kill button for 2 seconds (it can also be cleared from the LCD panel). While in the Halt state any command issued from the host will get a !! response (with a few exceptions). The PSU may be turned off when Halt is entered if there is a psu Switch defined.
All commands can be triggered by a button or a sensor if a Switch module is configured to do so.
Suspend/resume single button
This configuration allows you to set a single button to both pause and resume the machine
switch.pause.enable true # Enable this module switch.pause.input_pin 1.30^ # Pin where pause button is connected switch.pause.output_on_command suspend # Suspend command switch.pause.output_off_command resume # Resume command switch.pause.input_pin_behavior toggle # This pin toggles between it's on and off states each time it is pressed and released after_suspend_gcode G91_G0E-5_G0Z10_G90_G0X-50Y-50 # Gcode to run after suspend, retract then get head out of way before_resume_gcode G91_G1E1_G90 # Gcode to run after temp is reached but before resume - do a prime
Spindle control button
This configuration allows you to set a single button to start and stop your spindle.
switch.spindle.enable true # Enable this module switch.spindle.input_pin 1.30^ # Pin where pause button is connected switch.spindle.output_on_command M3 S1000 # Command to turn the spindle ON switch.spindle.output_off_command M5 # Command to turn the spindle OFF switch.spindle.input_pin_behavior toggle # This pin toggles between it's on and off states each time it is pressed and released
Laser power supply
For the enable ( TTL ) pin on a CO2 laser PSU, for power control use the Laser module.
# Switch module for laser TTL control switch.laser.enable true # Enable this module switch.laser.input_on_command M106 # Turn ON when M106 is sent switch.laser.input_off_command M107 # Turn OFF when M107 is sent switch.laser.output_pin 1.31 # Pin to control, to be connected to the laser power supply's TTL input
Note this is now supported by the laser module itself, where the pin is automatically toggled, using the laser_module_ttl_pin configuration option.
However, if you are not using that functionality, this allows you to turn the laser power supply using G-codes.
Setting up a reset button
Smoothie has a reset button, and you can wire an external button to that ( see Pinout ).
However, maybe you have an existing Panel, which has a button on it, and you want to turn that into a reset button.
If that's the case, you can setup a switch module to read whatever pin you wired that button to, and make it trigger the reset command whenever it is pressed, like this :
switch.reset.enable true # Enable this module switch.reset.input_pin 1.30^ # Pin where reset button is connected switch.reset.output_on_command reset # Command to reset the board