How to control backlight leds (and your annunciators too!) with a MOSFET and one PWM pin - MobiFlight/MobiFlight-Connector GitHub Wiki
Even though you have a lot of output pins in an Arduino Mega, it is not practical, nor sensible, to use them for backlight control with individual leds.
Of course the most straight forward approach is to use a cheap 12V led dimmer module and route warm white led strips behind your panels, but you might have several independent panels that would then need to be separately daisy chained for their backlight. While this is certainly possible to do, it can be impractical, depending on how your panels are located and used.
Also, you cannot easily sync the backlight with the dimmer in the simulator. While this is not an issue if you are building a full cockpit simulator (and thus would never see the simulated cockpit) there are different kinds of builds, and many of them are used together with the virtual panels, at least until we wade further down the rabbit holes of cockpit building.
Enter the FET (field effect transistor)
A fet is a type of a transistor - a semiconductor component that is one of the basic building blocks of pretty much anything that uses electricity. The typical FET has three pins, and it acts a bit like a water hose valve. The SOURCE
is connected to DRAIN
and the flow is controlled by the GATE
pin. While there are many kinds of transistors and FETs, we are focusing on a N-type, logic level mosfet, of which one very common one is IRLZ44.
Using a FET has several advantages: You can use a separate power supply to power your leds, thus you are not limited with the 500mA maximum available from USB. You can also use one arduino pin to control all your backlight leds at once.
So you need a N-type, logic level FET. Our circuit needs a N-type fet, and a logic level component means the control pin is compatible with 0 to 5V signal levels, that most Arduinos use. If you are making something for the RP2040 that operates at 3.3V, you can also find FETs that are designed for 0 to 3.3V operation.
The circuit
The circuit is very simple - we are basically creating a programmable switch on the ground wire of your backlight leds, that cuts off the GND wire for your led circuit. If you use 12V for your leds, you can wire your leds in strands of 3 that are in series, and add as many of them in parallel as you need. As white leds usually have approximatgely 3.5V voltage drop, you get 3 x 3.5V = 10.5V and adding a suitably sized resistor as a safety measure in case one of the leds decides to fail.
The circuit also has two resistors - one 10K Ohm pulldown resistor that ensures the circuit does not light up on its own, as it connects the gate pin to ground when the arduino is not powered. There is also another resistor, valued at 1k Ohm that is placed between the Arduino pin and the gate, to protect the pin from possible voltage spikes. While this is generally not as big of an issue with led lights compared to say running an electric motor, it still is a reasonable practice to add one just in case.