Light sensor

My next idea was to play around with the light sensor.

The idea of a practical use was following:
Most of the times I carry my mobile phone in my bag – so I have many missed calls, because I can’t hear it ringing (especially when listening to music). So I wanted to make something, that would notify my when the phone is ringing. Nowadays most mobile phones turn on their displays when you ring them – here the light sensor could be used. I’d create a small pocket for my phone in my bag, with the light sensor integrated and facing the display of the mobile phone. All I need then is a program, that would detect the light from the display and then turn the beeper on.

The program:
- monitor block – to see the values coming from the light sensor and thus to set the correct treshold value
- “if” block – to tell the board that if a certain value from the sensor is lower than a treshold (values rise as less ligt shines on sensor/more light means smaller number outputs – 1023 is the top value) to turn the beeper on
- “on” block next to the “if” block – simply to turn on the digital output (beeper in our case)
- “off” block to turn off the digital output when the value is less than the set treshold

int val= 0;
int compare=0;
int Pin7 = 7;
int Pin2 = 2;
void setup(){
pinMode(Pin2, INPUT);
pinMode(Pin7, OUTPUT);

void loop(){
int analogValue;
analogValue = analogRead(2);
val = analogRead(Pin2);
if (val > compare){
digitalWrite(Pin7, HIGH);
else {
digitalWrite(Pin7, LOW);


And it works!!!

I ahad several problems while working on this code:

- first of all, from reasons unknown to me the code has to be corrected after you switch from the block to the text view. the else statement in the condition looked originally like this:

else {

digitalWrite(Pin7, LOW);

The instruction to turn off the digital pin was out of curly brackets, so I had to move it in.

- I also don’t quite understand the if condition itself – it says, that if the value from the sensor is more that 900 (treshold value) it will turn the digital pin on, but it works the opposite way. I had to change the code itself after I turned it on for the first time and it did not worked as I wanted. I’m playing around with it right now and I see the values coming from the sensor – if they are above 900 the beeper is off and vice versa…strange to me…

« »

3 Responses to “Light sensor”

Milena says:

I guess you did something like this:
What it does: it checks the if Block and goes to the on Block in case the condition is true. Afterward it goes on to the off Block. No matter what. I think what you intended to do is something like this:
It checks the first if and maybe goes to the on than it checks the second if (that should ask for the opposite) and maybe goes to the off.

Tomas says:


yes, the blocks were the same as on your first picture.

I will also try out your solution. I thought that IF/then/else would work anyway, since the whole IF statement is in the loop…therefore the code checks the condition – and if the value is Ok then turns on the digital pin, if not it turns it off and then starts again from the beginning. Maybe it is not correct from a programmers view – but in my case it worked as I wanted (after the changes in the code as I wrote above).

Milena says:

Hi, well there is simply no else when you work with the blocks. It is only “if then”. If you want an “else” you need to make another “if then”. I could make an else block if you would prefer. My idea was to have as little blocks as possible.
About the other thing the wrong way round thing: did you try to connect the beeper the other way round or use an LED instead?

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>