Model Scenery Tutorials

Comprenshive Tips. Hints And Techniques For Budget Minded Diorama Maker, Model Train Enthusiast And Wargammer

How to fade LED lights using an Arduino Micro controller.

A tutorial including Arduino sketches, wiring diagrams and video demonstrations of fading lights on an off.

Materials Required

arduino nanoArduino Nano or Uno or Mega

 

 

 

Light Emitting Diodes

LEDs

The size may vary from smd LEDs to 5mm.  We use prewired SMD LEDs for street lights.

 

 

 

 

Resistors

Calculate the  resistance value using the data for your LED and one of the online LED  Ohms Law calculators.

 

 

 

circuit boardPrototype Board

Perf board, as shown, or Vero strip board can be used.  Our preference is Vero.

 

 

tlc5940TLC  5940 (optional)  This allows you to increase  PWM capability on your board

 

 

 

 

 

What These Parts Do For You

With these you make a standard multi-functional LED control board. You can get all these parts for such a control board, and instructions on how to make one,  in a discounted package at PolandsBest Electronics

You can choose a package either with or without LEDs.

 

Micro controller Limitations

In order to fade lights, the lights must be connected to PWM pins.

On an Arduino Nano and Uno, there are only 6 PWM pins.   So generally speaking, you are limited to fading only 6 lights via one of these boards.

There is, however, a workaround that you can use. Each pin can pass 40 ma on any one pin. So you can measure the current your  LEDS are passing and connect more than one LED to each pin.  You must not exceed the 40 ma and must remain below the 400 ma total board capability.  For safety sake, it is best to stay well below both those margins.

You can also use a second or third nano.  They are cheap and you can fade 18 lights.

An Arduino Mega has 15 PWM pins. They are pins 2-13 and 44-46  A mega is rather expensive, but because of the large number of pins on a mega, you can multitask and apportion the cost among the various tasks.

Cheaper than using a Mega for only fading lights is to use a TLC  5940  It is a bit more complex to use but is definitely cheaper than a Mega.  The setup is a bit out of the ordinary so we do not cover it in this page. Read about using it at  TLC5940

A Control Board For A Nano

Fading Six LEDs

This board is set up to fade 6 LEDs.

 

pre-wired smd leds Prewired SMD LEDs will be controlled by this board.  The resistors we show are 270 ohm.  Use a resistor value appropriate for you

 

 

 

 

 

 

Image of Control Board To Fade 6 LEDs.

 

The Sketch

Sketch To Fade 4 LEDs

We are showing a sketch fading only 4 LEDsbecause it is one we use in one of our dioramas.

This firs video shows the sketch in action on the Wokwi Code Simulator.  For simplicity’s sake, we did not include the resistors. When we use this sketch, we do not use a manual switch but use an input from a cycle control board. You can see how that is done at Daily Cycle.

 

 

The Arduino Code To Fade Lights

You can see the sketch controlling two street lights and two track side lights on one of our dioramas at Goods Shed Diorama
/*The street and track lights fader cycle for the goods shed. */
#define streetSwitch A5
#define streetEast 6 /* fading lights must be attached to pwm pins. Since there are only 6 pwm pins
on a nano/uno, to fade more lights use TLC  5940   https://steamtraininfo.com/arduino-projects/arduino-tlc5940
*/
#define streetWest 9
#define trackEast 10
#define trackWest 11
int brightness = 0;    //
int fadeAmount = 2;    // how many points to fade the LED by . will be slowed by a delay in the loop.
int val = 0;
// the setup routine runs once when you press reset:
void setup() {
  // declare pwm pins to be output:
  pinMode(streetEast, OUTPUT);
  pinMode(streetWest, OUTPUT);
  pinMode(trackEast, OUTPUT);
  pinMode(trackWest, OUTPUT);
  pinMode(streetSwitch, INPUT);
}
//Routine
void loop() {
lights();
}
//Cycles
//******************************************************************
  void lights(){
  val = digitalRead(streetSwitch);   // read the signal from the daily cycle
  if (val == HIGH) {
    analogWrite(streetEast, brightness);// set the brightness of pin :
analogWrite(streetWest, brightness);
analogWrite(trackEast, brightness);
analogWrite(trackWest, brightness);
    brightness = brightness + fadeAmount;  // change the brightness for next time through the loop:
    delay(200);// slows the fade time }
    if (brightness >= 255) { // keeps the LEDS bright once they reach max brigthness
      brightness = 255;
    }
  }
  if (val == LOW) {
    analogWrite(streetEast, brightness);// set the brightness of pin 9:
analogWrite(streetWest, brightness);
analogWrite(trackEast, brightness);
analogWrite(trackWest, brightness);
    brightness = brightness – fadeAmount;  // change the brightness for next time through the loop:
    delay(100);// slows the fade time }
    if (brightness <= 0) {
      brightness = 0;
    }
  }
}

Video Tutorial

This video shows the sketch in action.
Diorama Lighting
Click on the image to watch the video.

Get Materials

Once again you can get the parts to make a street light fader at  PolandsBest Electronics

Questions, Comments, Help

If you have questions, comments or need help using this sketch or animating your own diorama or train layout scene, contact us by email or use our Facebook messenger at ModelSceneryTutorials.

 

Scroll to top
 WhatsApp Us