How can I write a code for Light Detection 4-Pin 2-Channel Photo Diode Sensor Module

Hi,I have bought Light Detection 4-Pin 2-Channel Photo Diode Sensor Module.


But I don’t know how to detect light…

int led0 = D0;  
int led1 = D1;
int ledBlink = D7; 
int led0_val;
int led1_val;

void setup() {

  pinMode(led0, INPUT);
  pinMode(led1, INPUT);
  Serial.begin(9600);
 }

void loop() {
 led0_val = digitalRead(led0);
 led1_val = digitalRead(led1);
 if(led0_val = HIGH){
    Serial.println("0 is on"); 
    }else{
    Serial.println("0 is off");   
    }
if(led1_val = HIGH){
    Serial.println("1 is on"); 
    }else{
    Serial.println("1 is off"); 
    }
 }

Could you give me advices?

Thanks

Hi @Daisuke

Change ‘=’ to ‘==’:

if(led0_val == HIGH){
    Serial.println("0 is on"); 
    }else{
    Serial.println("0 is off");   
    }
if(led1_val == HIGH){
    Serial.println("1 is on"); 
    }else{
    Serial.println("1 is off"); 
    }

I think you have a typo that assigns instead of tests the LED values.

1 Like

Daisuke, your IF statements are not quite right. The “=” sign is for assigning a value while the “==” is for testing a value. So your IF statements should read:

if(led0_val == HIGH) {

:smile:

1 Like

Thanks @bko @peekay123
Thanks, I have not written C. I have forgot the basic of it.
Btw, I have turned off the light in the room, but still serial says “0 is on” and “0 is off”,
What is wrong? Can the module detects a light is bright or not, right?

I have written wrong.
Btw, I have turned off the light in the room, but still serial says “0 is on” and “0 is off”,

it should be written ->

Btw, I have turned off the light in the room, but still serial says only “0 is on” and “1 is on”

Hi @Daisuke

There are two trim potentiometers on that board that can be adjusted to your exact lighting situation. Have you tried adjusting them to see if there is a setting that works better for you?

@bko
Yes, there is 2 trim potentiometers. And 3 LEDs near it.
How should I adjust it? When room light turns on, should left(开关1) and right(开关2) LED turns on or off?

Hi @Daisuke

I think the trim pots adjust the on/off threshold, one for each channel in the 2-channel sensor.

The web page says:

When environment light brightness can’t reach the set range, the output is high; when it over the set range, the output is low

I think you have to experiment to figure out how it works.

FYI = vs == vs === are of the worst typos to try and track down. Even after programming for 15+ years, I still typo those three very frequently.

@bko
Thanks replied to me. Yeah, I found the set range is quite narrow. I make it to close the light, it starts work.