LED control w. GPIO hackerboard+MOSFET
Had some fun last night with the GPIO hackerboard and the LED light mod I have printed for the MK4S.
https://www.printables.com/model/493459-led-light-bar-prusa-mk4
To make the GPIO boards 3.3 volt output control the on/off of the LED, you need a logic level MOSFET transistor and two resistors. An IRLZ44N MOSFET is fully on when a voltage of 3.3 volt is applied to the gate, meaning you can draw sufficient current to drive an LED strip.
I also tried an IRF520 MOSFET drivermodule from Aliexpress, but that one need about 7 volts to be fully on, and I only got 18 volts on the LED strip. DOES NOT WORK!
A small piece of veroboard and some soldering later I had this -
Fairly primitive, but it works. I printed a small box for the veroboard and attached it on top of the processor board enclosure. I have used Pin 4 from the GPIO board. Pin 4-7 can be used like this, just remember to select the correct pin when you write the gcode. I have testet it with Pronterface by connecting the USB-C port on the back to my PC, and choosing CONNECT in the interface. Use the terminal window to issue commands.
M262 P4 B0 sets Pin4 as Output
M264 P4 B1 sets Pin4 high, meaning you now have 3.3 volts on the pin, the MOSFET opens and the LED strip lights up.
M264 P4 B0 sets Pin4 low, meaning 0 volts, the MOSFET can no longer draw any current and the LED turns off.
Insert the first two lines in your printer start gcode to turn on the LED strip when you start a print.
Insert the last line in the stop gcode to turn it off. You can also insert a delay here i.e. if you have a camera and want to confirm that everything went OK via the Prusa Connect camera option. In the image below you can see that it is only necessary to connect the GPIO pin, since ground is already present in the MOSFET circuit when you use the 24 V power supply from the printer.
The GPIO board is covered by this https://www.printables.com/model/1016997-prusa-gpio-hackerboard-cover
RE:
After installing the GPIO board and hooking up a MOSFET to control the LED lights I realized this will only work for new gcode files that i upload, and I have a ton of stuff on the USB that would need to be re-sliced with the LED control profile with the start/stop codes for the LED to work. Let's do some more GPIO magic to fix that.
The GPIO firmware has a built in macro keyboard that works as follows. If you define one of the pins 4-7 as an input pin (ie. M262 P7 B1 for pin 7), the GPIO board will start checking the input pin(s) for a change in voltage every 0,5 seconds. What you do is solder shut the pull-up resistor connection on the pin you want to use (4-7). Then you connect a switch with one connector to the pin and the other to ground. When the switch is closed you basically make a short circuit between the input pin and ground. This triggers the macro keyboard to go look on the USB stick for instructions.
In this case I have two files on the USB stick. A setup file that I run right after power on, to define the pins on the GPIO board.
It is a simple text file that contains the following -
M262 P4 B0
M262 P7 B1
I made the file with Notepad, and simply renamed it to aa_setup.gcode. Pin4 is now output to control the MOSFET, and Pin7 is my new input for the button. On the USB drive you now make a folder called macros. In that folder you make another text file that contains the line M265 P4. Save the file as btn_7.gcode.
Connect the button to Pin7 and ground on the GPIO board.
I used a 6x6 mm toggle switch inserted into a 14x8,5 mm housing replacing the original on/off switch.
Push the button to execute the btn_7.gcode file. The M265 command will toggle the state of pin4. Remember to hold the button for a minimum of 0,5 seconds to make sure the GPIO board registers the change. You can now turn the light on and off through the GPIO board. This even works mid print in case you want to turn the LED off before a print is finished. This is GPIO control in its simplest form. You can add whatever gcode is valid for the Prusa printer to a btn_x.gcode file and have it executed at the push of a button. Instead of a button you could have an external microcontroller pull the input pin low i.e. to set the printer ready.
Enjoy, and happy printing 😊
RE: LED control w. GPIO hackerboard+MOSFET
Very nice info, thanks 🙂
Instead of the pishing the button for 0.5s the next step would be to add a voltage divider to detect the printer printing when the bed heats up, and this would be useful to trigger the gpio pin to trigger the lights automatically 😉
See my GitHub and printables.com for some 3d stuff that you may like.
RE:
Very nice info, thanks 🙂
Instead of the pishing the button for 0.5s the next step would be to add a voltage divider to detect the printer printing when the bed heats up, and this would be useful to trigger the gpio pin to trigger the lights automatically 😉
Having a voltage divider with an NTC/PTC on the heatbed to raise or lower the voltage on a GPIO pin might give you some trouble due to a slow rise/fall. You would probably need to insert a Schmitt trigger in between to do a proper level detection and subsequent high/low shift. But sure it is doable.
Think of the GPIO board as a row of software controlled switches that you can either feed a signal in to, to make gcode happen, or use as a trigger on/off for something else. Pins 0-3 are either floating or low (active). If you attach an ESP32 or ESP8266 Arduino compatible microcontroller to one of those pins you can really make magic happen through Home Assistant or a simple MQTT/Node Red setup - which is basically also a row of software controlled switches. The ESP controller will act as an external pull-up resistor, and you can detect the pin active low with an interrupt loop like a switch library. Make your Philips HUE lights turn green in the kitchen when a print finishes, play a notification sound on a SONOS speaker or ZigBee smart siren. Instead of an LED strip, turn a fan on to cool the print after finishing, send an E-mail or a Telegram message. Anything Smart Home related is an option really.
RE: LED control w. GPIO hackerboard+MOSFET
Oh right the initial bed heating is a constant voltage which later changes into pulses due to PID control.
Also regarding to gpio I have found that certain things are better executed outside of the printer or without electronic interaction with it, by just utilizing PrusaLink and HA integration or Node Red and doing printer api calls for some details.
Gpio helps in that matter in execution for custom scripts - surely this is powerful feature. Yet some printer models seem not to support it (AFAIR), or don't support gpio at all, so the PrusaLink is the only option available. It is limited but allows to get certain integrations without major hacks.
See my GitHub and printables.com for some 3d stuff that you may like.
RE: LED control w. GPIO hackerboard+MOSFET
"Does that mean I have to run the aa_setup.gcode every time I start the printer? I want to control a few things with it, but I couldn’t find any information about macros on the Prusa help page.
RE: LED control w. GPIO hackerboard+MOSFET
Yes, you need to activate the pins on each startup. There is no way of storing it in a ROM for auto start. You would need to access the firmware itself to do that. Theoretically you could add a function/option in the firmware to look for a setup.gcode file on startup and run it if there is one.