MARLIN: M42
November 26, 2012 07:53AM
One of the M codes in MARLIN as noted in the README is M42 which is described as enabling "Change pin status via gcode". However, there is nowhere that I can find which describes how to use M42. For example, if I wanted to toggle the status of digital line D30 (aka A1), would I use "M42 30", "M42 D30" or something similar? I am looking to control a fan connected to one of the spare lines on a MELZI (or Sanguinololu) board via an external MOSFET so that it comes on at the end of a print to cool down the heated bed. (Not a fan used to cool a PLA print down as it is being produced - ie, not an M106 / M107).
Thank you,
Alan

EDIT
I have just done some searching on the MARLIN.PDE / MARLIN.INO file and PINS.H.
I don't have anything like a full understanding of how the firmware works but it would be good to get opinions on the following from those who know more than me ....

The syntax for the M42 command is: M42 S(value to be written to pin) P (pin number) e.g. To set digital pin 30 high, you would use M42 S1 P30

The MARLIN firmware will not enable you to change the status / write values to any of the pins in use for things such as the heaters, thermistors, end stops etc. The command will let you send values other than 0 and 1 to any pins which can output analogue values. (0-255)

Is that about right?

Edited 2 time(s). Last edit at 11/26/2012 09:01AM by Alzibiff.
Re: MARLIN: M42
November 26, 2012 10:46AM
I haven't tried it yet, but I was thinking about doing the same thing. That way, I could control additional fans and lights directly from my host software.

With marlin as I read it, everything you say is true. What I have never seen before is the following: If you look at the M42 code within Marlin, it does BOTH a digitalWrite and an analogWrite to the same pin (in that order) with the specified value. For pins that are NOT PWM pins, I guess it ignores the analog write, but what I don't understand is the effect on PWM pins. Obviously the analogWrite is done second, so that's the one that will win out, but what if I wanted to used that pin in a digital fashion? Is that even possible? I guess there are enough extra pins that it is possible to just avoid this issue.
Re: MARLIN: M42
November 26, 2012 11:16AM
The same thing occurred to me which made me think that writing '1' to a PWM pin, (is that the same as an analogue pin?) isn't going to quite do it. I suspect that in my example - using D30, I would have to write 255 to get full on output.

Alan
Re: MARLIN: M42
November 26, 2012 12:56PM
yes - the "analog" pins are the same as PWM, although PWM is probably more accurate. PWM is a digital way to simulate analog.

and yes - if it is an analog (PWM) pin then you can output a range of values 0-255, with 255 being full on. If you only sent a 1, I doubt you saw any result on whatever device you are trying to turn on.

Bear in mind, you can't power things like fans, heaters, or even a bank of LEDs directly from the Arduino. I don't know the value, but there is a limit on the amount of amperage it can drive and, of course, you are already running your printer. You need to add a simple transistor circuit like the one shown here.
Re: MARLIN: M42
November 27, 2012 07:32AM
Quote

the "analog" pins are the same as PWM

Well, I think analog is an input while PWM is an output. All PWM pins can be configured as simple digital output pins, too, so the behaviour depends on what the pin is configured to do.

Quote

Bear in mind, you can't power things like fans, heaters, or even a bank of LEDs directly from the Arduino.

An ATmega pin can sink 20 mA, which is sufficient for standard signal LEDs.


Generation 7 Electronics Teacup Firmware RepRap DIY
     
Re: MARLIN: M42
December 15, 2012 01:47AM
digitalWrite immediately followed by analogWrite is dumb...who writes this stuff? I guess I could use git blame but I'm too lazy. This is the problem when you use wrapper libraries like arduino without understanding what it actually does. Deleting the 'digitalWrite' line would have no effect and slightly speed up the code. Basically the analogWrite always overrides it.

For a non-PWM digital pin you need to put S0 (or any value <128) to turn off and S255 (or any value >=128) to turn on.
For a PWM pin, S0 will be full off and S255 full on, and intermediate values will PWM at that duty cycle.
Re: MARLIN: M42
December 15, 2012 07:00AM
Aren't analogue pins numbered independently of digital pins? In which case how does the code work?


[www.hydraraptor.blogspot.com]
Re: MARLIN: M42
December 19, 2012 05:10PM
Quote
Traumflug
Quote
jbernardis
Bear in mind, you can't power things like fans, heaters, or even a bank of LEDs directly from the Arduino.
An ATmega pin can sink 20 mA, which is sufficient for standard signal LEDs.
One LED, not a bank of them, and don't forget the series resistor.

Quote
nophead
Aren't analogue pins numbered independently of digital pins? In which case how does the code work?
The separately numbered pins are the analog input pins, not the PWM ("analog" output) pins.

I don't know what happens if one tries to use analogWrite() on a non-PWM pin: might the digitalWrite() be there to support that case?
Re: MARLIN: M42
December 25, 2012 09:46AM
rebecca.palmer Wrote:
-------------------------------------------------------
> I don't know what happens if one tries to use
> analogWrite() on a non-PWM pin: might the
> digitalWrite() be there to support that case?

analogWrite() on a non-PWM pin falls back to a digitalWrite(). If the value is < 128 then it sets the output LOW, else HIGH.

It also sets the pin to output, unlike digitalWrite(). digitalWrite() always turns off PWM on the pin, but does not set the pin to an output.

digitalWrite() treats any non-zero as HIGH, whereas analogWrite() only treats 255 as HIGH.

It seems the pinMode() and digitalWrite() in M42 are superfluous, and in fact are overridden by analogWrite().

The bottom line is, for M42, always use S255 to set a pin HIGH, regardless of PWM/non-PWM. S0 will always set the output LOW. Values for S in the range 1-254 will set the duty cycle accordingly for a PWM capable pin, if a non-PWM pin then set to LOW or HIGH by comparing with 128.

Using S1 will not set the output to HIGH in either scenario.
Sorry, only registered users may post in this forum.

Click here to login