Raspberry Pi Used to Control Layout Lighting?

Doorgunnerjgs Aug 29, 2017

  1. Doorgunnerjgs

    Doorgunnerjgs TrainBoard Member

    Is anyone using Raspberry Pi to control the lighting of buildings, street lights, yard lights, etc.? Was finally playing around with mine last night to turn on and off a LED. Got to thinking that it would be pretty cool to use it to turn on (and off) lights in sequence both in overall structures and within them. For example porch light goes on, hall light goes on, porch light turns off, kitchen on/off, bedroom on, hall off, etc. At the same time street lights go on in block sequence.

    Would love to see some examples of code, hardware, and lighting in action. Would a capacitor allow a ramp up from off to dim to full, or would something else be needed?
  2. drken

    drken TrainBoard Member

    If you're just going to be controlling LEDs, you'd probably be better off just using an Arduino or similar microprocessor. They're cheaper and much simpler than a Rasp Pi. You could program in any variations in brightness or flashing you want, no capacitors necessary.
  3. Doorgunnerjgs

    Doorgunnerjgs TrainBoard Member

    Thanks, drken. Was thinking about using the Pi to control multiple Arduino processors. Central control but offloading the details. Would possibly ease wiring with central com bus and mini hubs outlying.
  4. lnxlnx

    lnxlnx TrainBoard Member

  5. Doorgunnerjgs

    Doorgunnerjgs TrainBoard Member

    Cool, should have thought to look at mrh. Some great ideas there.
  6. Doorgunnerjgs

    Doorgunnerjgs TrainBoard Member

    So, a year and a half later and I've gotten the Arduino bug. With a lot of help from Geoff Bunza and others, I've learned a lot about using the Arduino Uno and Nano. The sequencing processing I talked about in my first post in this thread has become a reality, at least on the prototype board used for testing. I'm planning on installing it in some kind of building in the future by putting a Nano board in a basement of a building. Will use a simple power on sequencing start. Really amazing stuff!
    BNSF FAN likes this.
  7. Keith Ledbetter

    Keith Ledbetter TrainBoard Member

    So I know this an old thread but any updates on what you actually did and implemented. Arduinos truly are amazing once you get to know them a bit. They can do almost anything we need control and animation wise.
  8. Doorgunnerjgs

    Doorgunnerjgs TrainBoard Member

    Sorry, I ended up doing and showing my efforts over at Model Railroading Hobbyist site. Here is some pertinent entries from there.

    Don't know if anyone cares, but...
    Sat, 2019-03-23 17:16 — Photo Bud
    I've been working on a project that I wanted to share. The intent is to have an Arduino board control the lights in a house and cycle thru a scenario. The hard part (for me) was the time issue. The following sketch has an arbitrary start time of just before 5 pm. When started up, it flashes the LEDs 3 times so one knows it is loaded and started. Porch light comes on at 5:00, off at 5:05, hall light on at 5:01 and off at 5:06, bathroom on at 5:05 and off at 5:10, and kitchen on at 5:10 and off at 6:30.

    All are times defined at beginning. The board I'm using is still attached to the breadboard as in my previous post. A Serial print is done for testing that lights come on and go off when expected, but will probably be removed before "production". Also still need Parlor, TV, Front room, and bedroom added both to code and breadboard.

    Note earlier code has been eliminated as the next section shows the latest iteration.

    Optimization is Ongoing!
    Sat, 2019-03-23 22:18 — Photo Bud
    Using arrays and loops, the size of the sketch has been heavily reduced. Might be a bit easier to follow, also.

    Want to set up time spans for lights on by hour/minute format (24hr clock)
    Want fast clock capability both as testing function and normal running. Usually 4:1 or 5:1
    Include start of sequence flash of all lights 5 times
    Watch push button to restart sequence
    Initial lights to include
    TV in Parlor
    Front room

    // Time Variables
    unsigned long currtime;
    unsigned long starttime;
    unsigned long elapsetime;
    float floatelapse;
    float hourconv;
    float hourmsecs = 3600000;
    float minmsecs = 60000;
    float daymsecs = hourmsecs * 24;
    float elapseoffset = 16.95 * hourmsecs; // On startup, start the clock at approx 16:47
    float flmin;
    float fastratio = 12;
    int ihour;
    int imin;
    int iprevmin = 99; // dummy integer previous minute for proper start up

    // LED Info
    int porch = 12; //red
    int hall = 11; //yellow
    int bath = 10; //green
    int kitchen = 9; //blue

    // Room Arrays
    int onhrArray[] = {17, 17, 17, 17}; // Time on hour
    int onmiArray[] = {00, 01, 05, 10}; // Time on minute
    int offhArray[] = {17, 17, 17, 18}; // Time off hour
    int offmArray[] = {05, 06, 10, 30}; // Time off minute
    int statArray[] = {false, false, false, false}; //Current on/off status of LED
    int ledArray[] = {porch, hall, bath, kitchen}; //Pin numbers of corresponding rooms
    int allarraysize = sizeof(ledArray)/sizeof(ledArray[0]);

    // Misc Variables
    int i = 0;
    int j = 0;
    int longdelay = 300;
    int shortdelay = 200;

    void setup() {
    starttime = millis();
    pinMode(porch, OUTPUT);
    pinMode(hall, OUTPUT);
    pinMode(bath, OUTPUT);
    pinMode(kitchen, OUTPUT);

    for (i=0; i<5; i = i + 1) { // Flash all LEDs 5 times
    for (j=0; j<allarraysize; j = j + 1) {
    digitalWrite(ledArray[j], HIGH); // turn the LED on (HIGH is the voltage level)
    delay(longdelay); // wait for long delay
    for (j=0; j<allarraysize; j = j + 1) {
    digitalWrite(ledArray[j], LOW); // turn the LED off by making the voltage LOW
    delay(shortdelay); // wait for short delay


    void loop() {
    currtime = millis();
    elapsetime = currtime - starttime;
    elapsetime = (elapsetime * fastratio) + elapseoffset;
    floatelapse = float (elapsetime);
    hourconv = floatelapse / hourmsecs;
    ihour = int(hourconv);
    flmin = hourconv - ihour;
    imin = 60 * flmin;
    if (imin != iprevmin) {
    Serial.print(" Time: ");
    if (ihour > 23) {
    ihour = 0;
    starttime = currtime;
    elapseoffset = 0;
    if (ihour < 10) {
    Serial.print ("0");
    Serial.print (ihour);
    Serial.print (":");
    if (imin < 10) {
    Serial.print ("0");
    Serial.print (imin);
    iprevmin = imin;

    for (j=0; j<allarraysize; j = j + 1) {
    if (statArray[j] == false && ihour == onhrArray[j] && imin == onmiArray[j]) {
    statArray[j] = true;
    digitalWrite (ledArray[j], HIGH);
    else if (statArray[j] == true && ihour == offhArray[j] && imin == offmArray[j]) {
    statArray[j] = false;
    digitalWrite (ledArray[j], LOW);
    delay (1000);

    Flickering Effect
    Mon, 2019-03-25 15:56 — Photo Bud
    I've modified the House Light Seq Test to include a flickering TV. If anyone wants me to post the latest complete sketch, let me know, otherwise here is the pertinent code. Others have probably done it better, but this is mine. It basically uses the bits in a character string to turn on and turn off the LED. In addition to the delay at the end as shown, this is part of a much larger execution loop.


    // Flickering effect Variables
    int byIndex = 0; //byte index
    int biIndex = 0; //bit index
    char flickerstring[] = "AZghDpShKJdEOGL*zS12y3"; //random letters and symbols to mine for their bit strings
    byte flickChar; //extracted character to get bit values
    int biVal; //actual bit value, will be either 0 or 1


    // Flickering effect for TV or other bright object
    biIndex = biIndex + 1;
    if (biIndex > 7) {
    biIndex = 0;
    byIndex = byIndex + 1;
    if (byIndex > sizeof (flickerstring) - 2) {
    byIndex = 0;
    flickChar = byte(flickerstring[byIndex]);
    biVal = bitRead (flickChar, biIndex);
    if (statArray[TVIdx] == true) { //Note this is needed because of rest of code. Will only
    digitalWrite (ledArray[TVIdx], biVal); //flicker if the LED is already supposed to be on.
    // End of flickering effect code

    delay (75);

    A new version will be coming out soon based on Adafruit's NeoPixels and their Trinket board. Tiny LED's with full RGB color and addressability using a single data line.
  9. Pastor John

    Pastor John TrainBoard Member

    I have not yet plunged into learning Raspberry Pi, but I have thought about using it to control the lighting in an O gauge high rise hotel that my club owns. I thought, someday, it might be nice to cycle through an evening, dusk, dark, sunrise, morning cycle. Thanks for sharing.

    Sent from my moto g(7) play using Tapatalk
  10. Keith Ledbetter

    Keith Ledbetter TrainBoard Member

    While it certainly could do that but honestly an arduino family product could do much cheaper and easier.
  11. Pastor John

    Pastor John TrainBoard Member

    Ah, you're probably right. I'm sure that my inexperience with these allows me to confuse them far too easily.

    Sent from my moto g(7) play using Tapatalk

Share This Page