<p>As explained in the comments by Tom, you can set any port directly from G-code using the <a href="https://reprap.org/wiki/G-code#M42:_Switch_I.2FO_pin" rel="nofollow noreferrer"><code>M42</code></a> command. To set pin <code>22</code> "high", you need to call <code>M42 P22 S255</code>. There is no parameter to add time to the command, so you need to add that yourself using <a href="https://reprap.org/wiki/G-code#G4:_Dwell" rel="nofollow noreferrer"><code>G4</code></a> (dwell or pause) to specify how long the printer needs to wait for the next instruction, e.g.:</p>

<pre><code>M42 P22 S255 ; Activate solenoid/relay
G4 P2000     ; Dwell/pause for 2000 milliseconds (2 seconds)
M42 P22 S0   ; Deactivate solenoid/relay
</code></pre>

<p>As an alternative, you could use the existing fan if that fan is unused in your machine (or add an "extra fan" in the firmware and send the value 255 to that fan). To use the existing fan:</p>

<pre><code>M106 S255 ; Activate solenoid/relay
G4 P2000  ; Dwell/pause for 2000 milliseconds (2 seconds)
M107      ; Deactivate solenoid/relay
</code></pre>

<p>How you add an extra fan is already described (for a specific RAMPS board) in <a href="https://3dprinting.stackexchange.com/a/6536/5740">this answer</a>. However, the answer is valid for other boards as well, as long as you have exposed pins you have access to (or if you can solder directly to open pins), you can use the described technique. </p>

<p>The G-code to activate the "extra fan" (solenoid) is <code>M106 P1 S255</code> disabling would be <code>M107 P1</code>. Note that there is an option/parameter to add time (<code>Bnnn</code> Blip time - fan will be run at full PWM for this number of seconds when started from standstill) to the <a href="https://reprap.org/wiki/G-code#M106:_Fan_On" rel="nofollow noreferrer"><code>M106</code></a> command, but that is only implemented in RepRapFirmware. An alternative is to use <a href="https://reprap.org/wiki/G-code#G4:_Dwell" rel="nofollow noreferrer"><code>G4</code></a> (dwell or pause) to specify how long the printer needs to wait for the next instruction (<code>M107</code>) is being parsed, e.g.:</p>

<pre><code>M106 P1 S255 ; Activate solenoid/relay
G4 P2000     ; Dwell/pause for 2000 milliseconds (2 seconds)
M107 P1      ; Deactivate solenoid/relay
</code></pre>

CC BY-SA 4.0
2019-01-15T13:59:46.717Z
You can also use `M42` which allows you to toggle any pin, even if it's not a fan or anything else.
Tom van der Zanden
CC BY-SA 4.0
2019-01-16T11:22:12.020Z
@0scar I have tried to understand `pins.h` of marlin. In your answe, you xhose pin number `22`. By referrring to `pins_arduino.h` for mega, that pin curresponds to Digital Pin 22 of Arduino Mega. Am I right?? If not, is there any documentation available that maps pin number used in Marlin to Arduino or MCU pin. Also setting delay using `M4` command. is it based on internal Timer/Counter module of MCU. I need it to be accurate
Athul
CC BY-SA 4.0
2019-01-16T12:59:39.600Z
@Athul Pin `22` is used as an example, please see which you can use (D4, D6, D11 possibly). Some of the digital output pins can be used as PWM output, but you only need on/off. As an alternative, if you do not want to rely on the clock cycles of the MEGA, for accurate timing you could send the pulse to an external timer controller and let that operate the solenoid.
0scar
CC BY-SA 4.0
2019-01-17T08:58:05.343Z
@0scar Extreranl timer controller. You have any link for suitable device. I think making external timer sync with machine movement would be difficult. Machine shouldn't move when dispensing. I was looking in to some liquid dispensing controllers. There are some controllers which dispense liquid when receive a pulse. User can set the duration in that controller, and when it finishes it would return a signal. I know marlin is not designed for this, but is it possible to make Marlin set a pin high to indicate START dispensing and wait till return signal received and move to next position
Athul
CC BY-SA 4.0
2019-01-17T09:05:33.910Z
May be possible to set `M0` and connect the return pulse from Dispensing controller to LCD switch pin
Athul
CC BY-SA 4.0
2019-01-15T13:39:05.003Z
2019-01-15T15:03:18.263Z
2019-01-15T15:03:18.263Z
0scar
<p>Is there any way Marlin Firmware supports G-code that can make a pin "high" and keep it at that state for a period of time specified? </p>

<p>I have made a machine, which runs on Marlin Firmware, that is a mould forming tool, that would make a mould into a powder, then another tool would come over this mould and dispense some liquid in it. I have a custom GUI to move the tools to given coordinates, press a mould, position the second tool over the mould. </p>

<p>For dispensing liquid I'm thinking about buying a <a href="https://www.dosieren.de/en/products/dispensing-valves/diaphragm/dv-5625-t/diaphragm-valve-dv-5625-t" rel="nofollow noreferrer">diaphragm valve</a>. I've downloaded the user manual for it. As per the documentation, when compressed air is given to valve it will lift diaphragm and will start dispensing. The amount of liquid it dispenses depends on the how long the valve is opened. So I'm thinking about using a relay to turn a solenoid valve <code>ON</code> and <code>OFF</code> to supply air to the valve. To turn the relay <code>ON</code> I need to make a certain GPIO pin "high". Is there any way I can do this in Marlin Firmware? </p>

<marlin><g-code>
Marlin: Making any GPIO pin HIGH for a given time
Athul
# Marlin: Making any GPIO pin HIGH for a given time

(Asked by: username_0 on 2019-01-15)

Is there any way Marlin Firmware supports G-code that can make a pin "high" and keep it at that state for a period of time specified?

I have made a machine, which runs on Marlin Firmware, that is a mould forming tool, that would make a mould into a powder, then another tool would come over this mould and dispense some liquid in it. I have a custom GUI to move the tools to given coordinates, press a mould, position the second tool over the mould.

For dispensing liquid I'm thinking about buying a [diaphragm valve](https://www.dosieren.de/en/products/dispensing-valves/diaphragm/dv-5625-t/diaphragm-valve-dv-5625-t). I've downloaded the user manual for it. As per the documentation, when compressed air is given to valve it will lift diaphragm and will start dispensing. The amount of liquid it dispenses depends on the how long the valve is opened. So I'm thinking about using a relay to turn a solenoid valve `ON` and `OFF` to supply air to the valve. To turn the relay `ON` I need to make a certain GPIO pin "high". Is there any way I can do this in Marlin Firmware?

**Tags:** marlin, g-code

---

## 1 answers

### Answer by username_1 on 2019-01-15. Score: 3

As explained in the comments by Tom, you can set any port directly from G-code using the M42 command. To set pin 22 "high", you need to call M42 P22 S255. There is no parameter to add time to the command, so you need to add that yourself using G4 (dwell or pause) to specify how long the printer needs to wait for the next instruction, e.g.:
M42 P22 S255 ; Activate solenoid/relay
G4 P2000     ; Dwell/pause for 2000 milliseconds (2 seconds)
M42 P22 S0   ; Deactivate solenoid/relay

As an alternative, you could use the existing fan if that fan is unused in your machine (or add an "extra fan" in the firmware and send the value 255 to that fan). To use the existing fan:
M106 S255 ; Activate solenoid/relay
G4 P2000  ; Dwell/pause for 2000 milliseconds (2 seconds)
M107      ; Deactivate solenoid/relay

How you add an extra fan is already described (for a specific RAMPS board) in this answer. However, the answer is valid for other boards as well, as long as you have exposed pins you have access to (or if you can solder directly to open pins), you can use the described technique. 
The G-code to activate the "extra fan" (solenoid) is M106 P1 S255 disabling would be M107 P1. Note that there is an option/parameter to add time (Bnnn Blip time - fan will be run at full PWM for this number of seconds when started from standstill) to the M106 command, but that is only implemented in RepRapFirmware. An alternative is to use G4 (dwell or pause) to specify how long the printer needs to wait for the next instruction (M107) is being parsed, e.g.:
M106 P1 S255 ; Activate solenoid/relay
G4 P2000     ; Dwell/pause for 2000 milliseconds (2 seconds)
M107 P1      ; Deactivate solenoid/relay

> **Comments:**
> **username_2**: You can also use `M42` which allows you to toggle any pin, even if it's not a fan or anything else.  
> **username_0**: @0scar I have tried to understand `pins.h` of marlin. In your answe, you xhose pin number `22`. By referrring to `pins_arduino.h` for mega, that pin curresponds to Digital Pin 22 of Arduino Mega. Am I right?? If not, is there any documentation available that maps pin number used in Marlin to Arduino or MCU pin. Also setting delay using `M4` command. is it based on internal Timer/Counter module of MCU. I need it to be accurate  
> **username_1**: @Athul Pin `22` is used as an example, please see which you can use (D4, D6, D11 possibly). Some of the digital output pins can be used as PWM output, but you only need on/off. As an alternative, if you do not want to rely on the clock cycles of the MEGA, for accurate timing you could send the pulse to an external timer controller and let that operate the solenoid.  
> **username_0**: @0scar Extreranl timer controller. You have any link for suitable device. I think making external timer sync with machine movement would be difficult. Machine shouldn't move when dispensing. I was looking in to some liquid dispensing controllers. There are some controllers which dispense liquid when receive a pulse. User can set the duration in that controller, and when it finishes it would return a signal. I know marlin is not designed for this, but is it possible to make Marlin set a pin high to indicate START dispensing and wait till return signal received and move to next position  
> **username_0**: May be possible to set `M0` and connect the return pulse from Dispensing controller to LCD switch pin  

---

