1

Using an LDR and the below circuit diagram, I am trying to read the amount of light that the LDR is receiving. My primary goal is to detect a change when an object passes over the LDR to trigger an action. I am able to implement this in Python no problem, but must implement this in C. I am working with the C "wiringPi" library. Below the image is my code. Any help would be greatly appreciated!

enter image description here

#define LDR 7
void test(void);
int main(){
    while(1){
        test();
    }
    return 0;
}

void test(){
    wiringPiSetup();
    pinMode(LDR, OUTPUT);
    digitalWrite(LDR, LOW);
    delay(500);

    pinMode(LDR, INPUT);

    while(1){
        //This is where I am trying to print whether or not an object has
        //passed over and is blocking the LDR 
        printf("%d", digitalRead(LDR));
    }
}
  • need to put a volt meter on there and see if your getting enough of a voltage rise to hit the input high treshold – Chad G Mar 27 '18 at 18:59
  • You have provided zero diagnostic information. Neither have you bothered to correct the error I told you about on raspberrypi.org. – joan Mar 27 '18 at 19:24
  • Either you have to make sure that your voltage is changing in the range that Pi can detect. Or you can use Analog Chip to read LDR signal and decide based on ADC value. MCP3008 might help you to read LDR Signal. – HallMark Mar 29 '18 at 11:09

Browse other questions tagged or ask your own question.