# Marlin: Making any GPIO pin HIGH for a given time

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. 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?

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

• You can also use M42 which allows you to toggle any pin, even if it's not a fan or anything else. Jan 15 '19 at 13:59
• @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 Jan 16 '19 at 11:22
• @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
Jan 16 '19 at 12:59
• @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 Jan 17 '19 at 8:58
• May be possible to set M0 and connect the return pulse from Dispensing controller to LCD switch pin Jan 17 '19 at 9:05