Harsh Dethe
Published © CC BY-NC-SA

LED as a Light Sensor

Use a normal LED to sense light.

BeginnerFull instructions provided1 hour18,255
LED as a Light Sensor

Things used in this project

Hardware components

UTSOURCE Electronic Parts
UTSOURCE Electronic Parts
×1

Story

Read more

Code

led_sensor.ino

Arduino
Code to read the values from LED and display it on Serial Monitor.
/* To read values from led as a sensor. Upload the code in arduino. 
 * Connect the LED to pin A0 (analog pin) of arduino.
 * Connect second pin to GND.
 * Open serial monitor to see the values.
 *Code by Harsh Dethe.
*/

int sensor = A0;  // define analog pin A0. (input/sensor)

void setup()
{
  Serial.begin(9600); //sets serial communication between arduino and computer
  pinMode(sensor,INPUT); // sets analog pin A0 as input.
}

void loop()
{
  int value = analogRead(sensor); // sets variable "value" to store value of sensor.
  Serial.println(value); //prints values in serial monitor
  delay(500); //half a second delay
}

led_sensor_2.ino

Arduino
Code to use LED as a light sensor and control another LED.
/* To read values from led as a sensor. Upload the code in arduino. 
 * Connect the LED to pin A0 (analog pin) of arduino.
 * Connect second pin to GND.
 * Open serial monitor to see the values.
 * Code by Harsh Dethe.
*/

int sensor = A0;  // define analog pin A0. (input/sensor)
int led = 13; // define pin 13 for led (output)

void setup()
{
  Serial.begin(9600); //sets serial communication between arduino and computer
  pinMode(sensor,INPUT); // sets analog pin A0 as input.
  pinMode(led,OUTPUT); //sets pin 13 as output.
}

void loop()
{
  int value = analogRead(sensor); // sets variable "value" to store value of sensor.
  Serial.println(value); //prints values in serial monitor
  delay(500); //half a second delay
  
  if(value > 100) //condition
  {
    digitalWrite(led,HIGH); //turns LED on.
  }
  else
  {
    digitalWrite(led,LOW); //turns LED off
  }
}

Credits

Harsh Dethe

Harsh Dethe

30 projects • 68 followers
Electronics hobbyist, AI Enthusiast. I like to play with technology.

Comments