Controlling the lamp brightness with a light sensor

Now we are going to teach you what a LDR is, how it works and we’ll use it to control the brightness of the lamp.

!Important Informations

Every simple thing, with an additional touch, can be fantastic. Adding a LDR we can make that the light of the lamp respond to the environment’s needs. When it is darker, we need more light and, when it is brighter, less light.

The LDR, Light Dependent Resistor, is a passive electronic component that is a variable resistor. More specifically, it is a resistor whose resistance varies according to the intensity of the light that shines upon it.

!Identifying the Components

LDR

Resistor (left) and LDR (right)

To assemble the circuit, follow the next steps:

  • Connect one of the LDR terminals to the 5V pin
  • Connect the other LDR terminal to pin A3
  • Connect on of the resistor terminals to pin A3
  • Connect the other resistor terminal to the GND pin

Once you have finished assembling, it’ll look like this:

Base

Final look (click to zoom)

!Code components

Let’s use a specific function which receives a read value from the sensor and returns a value that is relative to the brightness.

int intensity(int sensorValue)
{
  float intensityReturn;
  float bright = (sensorValue / 4);
  intensityReturn = 255 - bright;
  Serial.print(intensityReturn);
  return intensityReturn;
}

!Running on Arduino

int lamp = 3;
int ldr = A3;
int button = A0;
int light;

int intensity(int sensorValue)
{
  float intensityReturn;
  float bright = (sensorValue / 4);
  intensityReturn = 255 - bright;
  Serial.print(intensityReturn);
  return intensityReturn;
}

int getButtonState()
{
  int value = analogRead(button);
  if (value < 69) {
    return 1;
  } else if (value >= 69 && value <= 230) {
    return 2;
  } else if (value >= 231 && value <= 407) {
    return 3;
  } else if (value >= 408 && value <= 630) {
    return 4;
  } else if (value >= 631 && value <= 896) {
    return 5;
  } else {
    return 0;
  }
}

int readButton()
{
  int state = getButtonState();
  int count = 1;

  while (count < 5) {
    delay(5);
    int newState = getButtonState();
    if (newState == state) {
      count++;
    } else {
      count = 1;
    }
    state = newState;
  }

  return state;
}

void setup()
{
  pinMode(lamp, OUTPUT);
  analogWrite(lamp, 0);
}

void loop()
{
  // --- State: lamp on, button released
  // Lamp on while the button is not pressed
  int btn;
  while ((btn = readButton()) != 5) {
    int sensorValue = analogRead(ldr);
    light = intensity(sensorValue);
    analogWrite(lamp, light);
  }

  // --- State: lamp off, button pressed
  // Turns the lamp off
  analogWrite(lamp, 0);

  // Waits for the button to be released
  while (readButton() != 0);

  // --- State: lamp off, button released
  // Waits for the button to be pressed
  while (readButton() != 5);

  // --- State: lamp on, button pressed
  // Waits for the button to be released
  while (readButton() != 0);
}

!You Turn!

Modify the code above so that the system working is indicated by the LEDs.

Answer

int lamp = 3;
int ldr = A3;
int button = A0;
int light;
int ledAmarelo = A1;
int ledVerde = A2;

int intensity(int sensorValue)
{
  float intensityReturn;
  float bright = (sensorValue / 4);
  intensityReturn = 255 - bright;
  Serial.print(intensityReturn);
  return intensityReturn;
}

int getButtonState()
{
  int value = analogRead(button);
  if (value < 69) {
    return 1;
  } else if (value >= 69 && value <= 230) {
    return 2;
  } else if (value >= 231 && value <= 407) {
    return 3;
  } else if (value >= 408 && value <= 630) {
    return 4;
  } else if (value >= 631 && value <= 896) {
    return 5;
  } else {
    return 0;
  }
}

int readButton()
{
  int state = getButtonState();
  int count = 1;

  while (count < 5) {
    delay(5);
    int newState = getButtonState();
    if (newState == state) {
      count++;
    } else {
      count = 1;
    }
    state = newState;
  }

  return state;
}

void setup()
{
  pinMode(lamp, OUTPUT);
  pinMode(ledAmarelo, OUTPUT);
  pinMode(ledVerde, OUTPUT);
  analogWrite(lamp, 0);
}

void loop()
{
  // --- State: lamp on, button released
  // The lamp is on while the button is not being pressed
  int btn;
  while ((btn = readButton()) != 5) {
    int sensorValue = analogRead(ldr);
    light = intensity(sensorValue);
    analogWrite(lamp, light);
    digitalWrite(ledVerde, HIGH);
  }

  // --- State: lamp off, button pressed
  // Turns the lamp off
  analogWrite(lamp, 0);

  // Waits for the button to be released
  while (readButton() != 0);
  digitalWrite(ledVerde, LOW);
  digitalWrite(ledAmarelo, HIGH);

  // --- State: lamp off, button released
  // Waits for the button to be pressed
  while (readButton() != 5);

  // --- State: lamp on, button pressed
  // Waits for the button to be released
  while (readButton() != 0);
  digitalWrite(ledAmarelo, LOW);
}