Sensing Light Variations using LDR

Basically, the resistance of the photocell increases as the amount of light falling through its surface decreases and resistance goes down while the amount of light increases. These variation in resistance can be determined by measuring the voltage drop on the photocell. Remember Ohm’s law:


which means as our resistance goes up, the voltage drop across our photocell also increases thus resulting to a lower output voltage.

There are only two components needed here:

  • 1 Photocell
  • 330 Ohms resistor

The circuit is actually a voltage divider. This is done so that our analog input pin will not be floating in the air when connected to the photocell. The analog pin 7 needs to be pulled down that’s why the 330 ohms resistor is also known as the pull-down resistor because it connects the analog pin 7 to the ground.


Our sketch looks like below:

After checking if our device is connected, we read the current value of analog pin 7 then write it into our device’s input stream using acc.write(). Then we wait for 100ms before reading the pin again so that our android app will not be choked by too much data coming in.


So in our app, we need to constantly monitor any incoming data from our accessory’s input stream and to do this, we need to spawn a thread. Our run method looks like:

Here, we read any incoming data into our buffer and for each byte, we construct a corresponding message which will be sent to our handler.

The app plays a continuous DTMF tone as an example and the volume of the tone will be adjusted based on the readings from our photocell.¬†Our handler will just set the volume for DTMF tones based on the value retrieved from the accessory’s stream and at the same time, sets the progress and text of our visual components.