i am new to micro controller programming and in need of help. i have managed to interface a light sensor to a micro controller (atmega 32a) and can read the value i.e. (1023) as per the set prescale. however i intended to achieve a single decimal value as the result. in this case (1) every time the sensor is exposed to light and successively perform an incremented tally as lights go on and off around the sensor ( i am using a LDR for this). i am currently having problem getting my micro controller to do this. i have searched all over the internet but no seems to assist me on this... please any insight on how to achieve this will be much appreciated. i know its a simple thing,HELP :(

basically just a means of making my micro controller count the number of times light falls on the LDR

  • 1
    What reading do you have when the sensor is lit? And when it's not? 1023 sounds like you wired the LDR wrong. Forgot a pulldown maybe? Jun 5, 2016 at 17:51
  • 1
    Can you show what code you have so far? This isn't really a code-writing site.
    – Nick Gammon
    Jun 7, 2016 at 23:08

1 Answer 1


There are 2 common light sensor types. The photo diode and the photo resistor. The first produces small amounts of current and operates fast. The second changes resistance and operates (very) slowly.

There is a good stack exchange response here for how to use a photo diode. Often the photo diode output is small and additional hardware is used to amplify the signal. As discussed in this stack exchange response.

The photo resistor or cadmium sulphide cell or Light Dependent Resistor is much easier to interface with an Arduino. However, the response time is very slow. On the order of hundreds of milliseconds to seconds. This is acceptable when controlling street lights. But is not acceptable in applications requiring fast light detectors such as those used for communication.

Before going on, verify which device you are using in your design.

Generally, Arduinos contain a 10 bit ADC. This results in values from 0 to 210−1 or 0 to 1023. Should you consistently get a reading at or near the limits of the ADC check your hardware. Do this by measuring the voltage at the input of the ADC pin used. Do this under varying conditions to verify the hardware is operating as expected.

A good software design uses hysteresis to convert an analog value to a logical 1 or 0. Here is a stack exchange response about using hysteresis with Arduinos.

  • i have perfect wiring of the LDR and yes i used a pull up resistor. the results range from 0-1023. this occurs when i vary the variable resistor. to add i am not using a arduino. i am using a programmer to push a C++ code into my micro controller then fabricate it on a pcb. what i need is how to manipulate the returned ADC value read from the pins, from a 10 bit value to just a single decimal 1 (at code level ) . i require the micro controller to return the value 1 on exposure, store the value when not exposed and increment the value on every subsequent exposures.
    – Bob Otieno
    Jun 7, 2016 at 7:19
  • in other words ..is it possible to dictate the value returned. . i intend to use the sensor at its maximum reading. (will use a trimer instead of a pontentiometer for final fabrication ) . all i want to achieve is a tally of number of max exposure times
    – Bob Otieno
    Jun 7, 2016 at 8:13
  • No, the ADC hardware inside the processor returns a value proportional to the voltage seen at it's input. The Arduino ADC library configures the ADC hardware and returns this value to your sketch. It is up to your sketch to interpret this value. If all you want to do is interpret the ADC value as a 1 or 0 ... I suggest you use the knowledge in the last paragraph of the answer.
    – st2000
    Jun 7, 2016 at 12:24
  • There are hardware hysteresis solutions to this problem. But they tend to operate at a fixed point and, normally, people tend to address this problem in software. If you use a compatible hardware solution you should be able to use a GPIO pin and simply test if it is connected to a high or low voltage source.
    – st2000
    Jun 7, 2016 at 12:29
  • thank you sir, @st2000 .let me try approach it your way.. much appreciated.. some guys though..smh
    – Bob Otieno
    Jun 10, 2016 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.