Close

Hardware - LEDs & LDRs - Automation Part 1

A project log for Not Just Another Coffee Table

Custom coffee table with a DCC train track, automation, LEDs and a web interface

Jack WellsJack Wells 10/12/2018 at 12:190 Comments

Part of the fun of this build is to put small subtle details into it that add a layer of complexity to create a more "life-like" feel to the miniature layout. With this in mind we've taken our LED lighting for the housing, street lighting, etc and paired them with an LDR - light dependent resistor, or photoresistor. An LDR can be used to measure the level of light around the sensor. For our application this means we can use an LDR to measure the ambient light level in the layout and use that data to turn on LEDs as if the world is reacting to the light around the table.  This paired with our dimming LEDs creates a nice result (code block at the end of the post);

By pairing multiple LDRs with block groups of LEDs then I'm hoping we can create localised areas that will react to objects placed on the top of the table that reduce the light in a certain area and the local LEDs turn on in response.  Combining this with my house and street light setup we can see a nice little result;

/*
 Turns on LEDs based on LDR feedback
 Uses analogue write to handle dimming of LEDs
 */
 
int Redled = 6;
int Yelled = 5;
int Grnled = 3;

boolean RedState, YelState, GrnState = false;

int RedRate = 1;
int YelRate = 10;
int GrnRate = 60;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(Redled, OUTPUT);     
  pinMode(Yelled, OUTPUT);     
  pinMode(Grnled, OUTPUT);     
  pinMode(A5,INPUT);
  Serial.begin(115200);
}

// the loop routine runs over and over again forever:
void loop() {
  Serial.print("LDR Value;"); Serial.println(analogRead(A5));

  static boolean LightState = true;
  boolean newLightState = LightState;
  
  if(analogRead(A5) < 600)
   newLightState = true;
  else
   newLightState = false;
   
if(LightState != newLightState)
{ 
  LightState = newLightState; 
  
  if(newLightState == true)
  {
  //Brigthen all leds
  for (int i = 0; i < 255; i++)
  {
    analogWrite(Redled, i);
    analogWrite(Yelled, i);
    analogWrite(Grnled, i);
    delay(10);//reset timer
   
  }
  }else
  {
   
  //Dim all leds
    for (int i = 0; i < 256; i++)
  {
    analogWrite(Redled, 255-i);
    analogWrite(Yelled, 255-i);
    analogWrite(Grnled, 255-i);
    delay(10);//reset timer
  } 
  }
}
}

Discussions