Sensing Light Variations using LDR


Today, I’m going to show how easy it is to read data from an accessory to your android device. In this example, we are going to use a special kind of resistor whose resistance is dependent on the amount of light falling on its surface. It is called Light-Dependent-Resistor or LDR or also known as Photocell.

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:

I= V/R

or

V=IR

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

Vo = Vcc - Vphotocell

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.

Sketch

Our sketch looks like below:



int value = 0;

void setup(){
    Serial.begin(9600);
    acc.powerOn();
 }

void loop(){
   byte msg[1];

   if (acc.isConnected()) {
      value = analogRead(7);
      msg[0] = value;
      acc.write(msg, 1);
      delay(100);
    }
}

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.

APK

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:


public void run() {
    byte[] buffer = new byte[16384];
    int i;
    int retvalue = 0;

    while (retvalue >= 0) {
        try {
        retvalue = input.read(buffer);
        } catch (IOException e) {
        break;
        }

        i = 0;
        while(i < retvalue){
            Message m = Message.obtain(handler, 0);
            m.obj = String.valueOf(buffer[0]);
            handler.sendMessage(m);
            i++;
        }
    }

    }

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.


public void handleMessage(Message msg) {
        if(msg.what == 0){
                String value = (String) msg.obj;
                audioManager.setStreamVolume(AudioManager.STREAM_DTMF, Integer.parseInt(value), 0);
                progress.setProgress( Integer.parseInt(value));
                reading.setText(value);
    }
}

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.

comments powered by Disqus