What is it?
Today Sprinter, one of the firmwares for the arduino microcontroller that can control a raprap printer, got a new M-code.
The M-code is M42, and as you might know, 42 is the answer to everything, it is almost the same with M42 too.
42 here is the answer to control any non critical pin, that is not used to move the printer around. Pins like direction, step, driver enable, and so will not be available with this command. But all the other pins, can be controlled directly from the G-code.
Why could this be useful?
If you are making some ad-dons to your printer, it could be nozzle light, extra fans, LED’s to show you when the printer is done, or maybe you want to have a little buzzer connected to the printer, so it can make sounds. You can now easily do all this, without having to mess around inside the firmware to add extra functions to control exactly that pin you want to do something.
If we assume you got your nozzle light connected to pin 7, you can do like this
M42 P7 S255
This will then pull pin 7 HIGH, and the light will turn on, but we can do more! Pin 7 on an Arduino MEGA is also capable of PWM, so we can dim the light too
M42 P7 S125
would almost do the same as before, but instead have a PWM duty cycle on around 50%, so the light will appear less bright. S can vary from 0 to 255
How to power something from a pin on the Arduino
To drive a fan, you need a transistor or a mosfet. On the following image I am using a TIP102 to do the heavy lifting the pin can’t do.
If we look at the pins from the left. The first pin is the one connected to the pin on the controller, where you put a 10k resistor on to limit the current. The middle pin goes down to minus/gnd on the fan, and the right pin goes to the minus/gnd on your power supply.
Warning the “flap” on the TIP102 is connected to the middle pin, so never connect it to anything that can conduct electricity, or you might get a short or other unexpected things happening.