Starting a SOA Processes by Opening the Light on Your Living Room! Part-1

Following the previous post, this part is telling you how to start your little project, and what you should do. In this part I’m focusing on the Hardware part, most importantly the Arduino part, How to make your sketch and create your circuit successfully.


In order for you to start you the following:

  • Hardware1 (Required)

    • ArduinoUno (You can have any Arduino board, but this tutorial shows the example on Arduino Uno)
    • Breadboard (for wiring everything together)
    • Miniature photocell (The Light detector, the component that will tell us when the lights is on or off)
    • 1 10k Ohm resistor (This will be hooked with the miniature photocell -My photocell is basically a resistor in disguise, in order for Arduino to interact with it, I have to make a divider in which I have to provide the same resistance in the other part of the circuit (will explain soon), so if you have a larger miniature photocell resistance this resistor should match it!)
    • 5 Jumper wires
    • USB Cable for programming the Arduino and giving a power source
  • Hardware (Optional) -just to be sure from hardware point of view that everything is right!-

    • 1 330 Ohm resistor
    • 1 Yellow Led (or your choice of color)
    • 1 Jumper wire
  • Software

    • Arduino IDE (Very simple to install, follow your OS steps in here


The Sketch

Follow the following sketch from the screenshot

Light Sensor Sketch


PS: You don’t have to follow the Jumper’s colors, but it’s a good practice to stick to the colors not to do things wrong!


The Code

The code is self explanatory and it’s very simple, what it said is when you see change from the photo resistor, if there is no lights in the living room make the led shine, else make the Led dimmed!



  Use a photoresistor (light sensor) to control the brightness
  of a LED.

Hardware connections:

  Photo resistor:

    Connect one side of the photoresistor to 5 Volts (5V).
    Connect the other side of the photoresistor to ANALOG pin 0.
    Connect a 10K resistor between ANALOG pin 0 and GND.

    This creates a voltage divider, with the photoresistor one
    of the two resistors. The output of the voltage divider
    (connected to A0) will vary with the light level.


    Connect the positive side (long leg) of the LED to
    digital pin 9. (To vary the brightness, this pin must
    support PWM, which is indicated by "~" or "PWM" on the
    Arduino itself.)

    Connect the negative side of the LED (short leg) to a
    330 Ohm resistor.

    Connect the other side of the resistor to GND.

// We'll create constants to name the pins we're using.
// This will make it easier to follow the code below.

const int sensorPin = 0;
const int ledPin = 9;

// We'll also set up some global variables for the light level:

int lightLevel, high = 0, low = 1023;

void setup()
  // We'll set up the LED pin to be an output.
  // (We don't need to do anything special to use the analog input.)
  pinMode(ledPin, OUTPUT);

  //We'll also start sending signal to the host Computer on port 9600

void loop()
  // We'll use the analogRead() function to measure the voltage 
  // coming from the photoresistor-resistor pair. This number can 
  // range between 0 (0 Volts) and 1023 (5 Volts), but this circuit 
  // will have a smaller range between dark and light.

  lightLevel = analogRead(sensorPin);

  // We now want to use this number to control the brightness of
  // the LED. But we have a problem: the analogRead() function
  // returns values between 0 and 1023, and the analogWrite()
  // function wants values from 0 to 255.

  // The circuit we made won't have a range all the way from
  // 0 to 5 Volts. It will be a smaller range, such as 300 
  // (dark) to 800 (light).
  // If we just pass this number directly to map(), the LED will
  // change brightness, but it will never be completely off or
  // completely on.

  autoTune();  // have the Arduino do the work for us!

  // The above functions will alter lightLevel to be cover the
  // range from full-on to full-off. Now we can adjust the
  // brightness of the LED:


  // And then we can send this lightLevel to The Host Computer
  // Notice that it'll be from 0 to 255

void autoTune()
  // As we mentioned above, the light-sensing circuit we built
  // won't have a range all the way from 0 to 1023. It will likely
  // be more like 300 (dark) to 800 (light).

  // In this function, the Arduino will keep track of the highest
  // and lowest values that we're reading from analogRead().

  // If you look at the top of the sketch, you'll see that we've
  // initialized "low" to be 1023. We'll save anything we read
  // that's lower than that:

  if (lightLevel < low)   
           low = lightLevel;   
  // We also initialized "high" to be 0. We'll save anything   
  // we read that's higher than that:  
  if (lightLevel > high)
    high = lightLevel;

  // Once we have the highest and lowest values, we can stick them
  // directly into the map() function. No manual tweaking needed!

  // One trick we'll do is to add a small offset to low and high,
  // to ensure that the LED is fully-off and fully-on at the limits
  // (otherwise it might flicker a little bit).

  lightLevel = map(lightLevel, low+30, high-30, 0, 255);
  lightLevel = constrain(lightLevel, 0, 255);

  // Now we'll return to the main loop(), and send lightLevel
  // to the LED.


Running the code on Arduino

Now you are able to run this on Arduino, select your serial port and click on upload from you Arduino IDE.

Now try to cover the Photo resistor to see the LED flashing and dimming, pretty fun huh!

Next part is to get the data from Arduino and start the SOA Process, stay tuned.




1: You can download all these hardware as a training bundle from SparkFun