Let’s introduce a new sensor component: the photo diode. In simple terms, when the sensor detects light, its resistance changes. The stronger light in the surrounding environment, the lower the resistance value the photo diode will read. By reading the photo diode's resistance value, we can work out the ambient lighting in an environment. The photo diode provided in the starter kit is a typical light sensor.
In this arduino tutorial, we will make an automatic light that can adjust itself according to the ambient lighting around it. When it is dark, the photo diode detects the change and triggers the light, and vice versa.
Be aware that photo diodes are polarized, just like LEDs, so they will only work if connected the correct way around.
The photo diode has to be connected with a 10k resistor rather than a 220Ω resistor. Diagram of the photo diode circuit
Sample code 9-1:
// Project 9– Light the lamp int LED = 13; //define LED digital pin 13 int val = 0; //define the voltage value of photo diode in digital pin 0
void setup(){ pinMode(LED,OUTPUT); // Configure LED as output mode Serial.begin(9600); //Configure baud rate 9600
}
void loop(){ val = analogRead(0); // Read voltage value ranging from 0 -1023 Serial.println(val); // read voltage value from serial monitor if(val<1000){ // If lower than 1000, turn off LED digitalWrite(LED,LOW); }else{ // Otherwise turn on LED digitalWrite(LED,HIGH); } delay(10); // delay for 10ms }
After uploading the code, you can shine a flashlight on the photodiode to alter the light levels in the environment. When it is dark, the should light up. When it is bright, the LED should turn off.
Code
A very brief explanation of the program: Similar to the LM35 temperature sensor, the photo diode reads an analog signal so we don’t need to define “pinMode” in “serial.begin”.
We take the analog current data from the photodiode and compare it to a value of 1024 to make it digital. You can change this value if you like. Try playing with the serial monitor and seeing what outputs the photodiode gives. Then use this number you get here as the comparison number to alter the sensitivity of the circuit.
Circuit
Photo Diode
A photo diode is a semiconductor device that converts light into current.
The current is generated when photons are absorbed in the photo diode. The stronger the environment’s light, the lower the resistance value the photodiode will output. The analog value ranging from 0 to 1023 corresponds to voltage value ranging from 0 to 5V.
The input voltage Vin(5V) is connected to 2 resistors. By measuring the voltage of R2 as below, you can get the resistance value of photo diode.
In our arduino project, R1 is the 10k resistor and R2 is the photo diode. The resistance value of R2 in dark is so high that it almost reaches 5V. Once photons are absorbed, the value of R2 will decrease, and so will its voltage value. For this project it is preferable to use a fixed resistor ranging from 1k to 10k, otherwise the voltage dividing ratio is not obvious. This is why in this project we have used a 10k resistor for R1.
This article introduces the Mind+(Mindplus) editor, which seamlessly combines block-based and text coding in its interface, facilitating an easier transition.