# AtTiny analog read always 1023

I am trying to replicate Low power LED firefly project. So what I'm trying to do is to do an analog read from a LED, to detect light. To simplify it a bit, I am using different pins and different leds for IN and OUT. The code:

``````#define PIN1 don't use this pin! /*this is RESET*/
#define PIN2 3
#define PIN3 4
#define PIN4 don't use this pin! /*this is GND*/
#define PIN5 0
#define PIN6 1
#define PIN7 2
#define PIN8 don't use this pin! /*this is VCC*/
#define ANL1 PIN7
#define ANL2 PIN3
#define ANL3 PIN2

// the setup function runs once when you press reset or power the board
void setup() {
digitalWrite(PIN3, LOW);
pinMode(PIN3, INPUT);
}

// the loop function runs over and over again forever
void loop() {

if(value>=1023) {
pinMode(PIN2, OUTPUT);
digitalWrite(PIN2, HIGH);
delay(500);
digitalWrite(PIN2, LOW);
delay(3);
}
else {
delay(1);
}
}
``````

This is the setup. I am using much smaller resistor, but I'm still getting 1023 on analog read, as indicated by the output led always being on:

simulate this circuit – Schematic created using CircuitLab

What's wrong?

In this article the following steps are used when using an LED as a light detector:

1. Light up the LED by making the anode positive (logic one) and the cathode negative (logic zero) - this fully discharges the LED as a capacitor.
2. Charge up the capacitor by making the anode negative and the cathode positive, as this is a small capacitor it will take no time at all in other words for as short a time as you can.
3. Put the diode into the measurement mode, by making the anode negative (it already is) and the cathode connected to an analogue input.
4. Make a measurement of the analogue voltage, this is a reference level to be used later.
5. Wait while the photon current is integrated - the longer the more sensitive but too long and effects other than photo current dominate.
6. Measure the voltage again and subtract it from the reference value you took before the integration time. This figure is your light reading.

Looking at the code in the above question it is not apparent where you switch from forward bias to revers bias. Also, it does not appear there are two samples taken and the difference between the two samples is found.

• Thanks for the explanation. But I was more concerned about the fact that the analog read doesn't return zero, or close to zero, when it's shorted to GND via the resistor. Apr 13, 2019 at 2:41
• Since you are not using "value" to, say, dimension an array, then you should not use "const". This may be the root of the problem you are having. Also, ADC are not all that accurate. If you don't take several readings and average them then you usually only get a few significant bits out of the range of the ADC. Apr 13, 2019 at 21:37