Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

Using a Photoresistor with Arduino for Light Sensing

A photoresistor, also known as a light-dependent resistor (LDR), is a sensor that changes its resistance based on the amount of light it is exposed to. This makes it a valuable component for projects that require light sensing, such as automatic lighting systems, light meters, and even solar trackers. Integrating a photoresistor with an Arduino allows for precise control and monitoring of light levels, enabling the creation of responsive and intelligent systems.

In this article, we will explore how to use a photoresistor with an Arduino to measure light intensity. We will provide a detailed project example, including the necessary components, schematic, and code to get you started.

Project: In this project, we will create a simple light-sensing system using a photoresistor and an Arduino. The objective is to measure the ambient light intensity and display the readings on the serial monitor. Additionally, we will use an LED to indicate whether the light level is above or below a certain threshold.

Components List:

  1. Arduino Uno - 1
  2. Photoresistor (LDR) - 1
  3. 10kΩ Resistor - 1
  4. LED (any color) - 1
  5. 220Ω Resistor - 1
  6. Breadboard - 1
  7. Jumper wires - Several

Examples:

// Define the pin connections
const int LDR_PIN = A0; // Analog pin connected to the photoresistor
const int LED_PIN = 13; // Digital pin connected to the LED
const int THRESHOLD = 500; // Light level threshold for LED indication

void setup() {
  // Initialize the serial communication
  Serial.begin(9600);

  // Set the LED pin as an output
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  // Read the value from the photoresistor
  int lightLevel = analogRead(LDR_PIN);

  // Print the light level to the serial monitor
  Serial.print("Light Level: ");
  Serial.println(lightLevel);

  // Check if the light level is above the threshold
  if (lightLevel > THRESHOLD) {
    // Turn off the LED if the light level is high
    digitalWrite(LED_PIN, LOW);
  } else {
    // Turn on the LED if the light level is low
    digitalWrite(LED_PIN, HIGH);
  }

  // Wait for a short period before the next reading
  delay(500);
}

Explanation of the Code:

  1. Pin Definitions:

    • const int LDR_PIN = A0; defines the analog pin connected to the photoresistor.
    • const int LED_PIN = 13; defines the digital pin connected to the LED.
    • const int THRESHOLD = 500; sets the light level threshold for LED indication.
  2. Setup Function:

    • Serial.begin(9600); initializes serial communication at 9600 baud rate.
    • pinMode(LED_PIN, OUTPUT); sets the LED pin as an output.
  3. Loop Function:

    • int lightLevel = analogRead(LDR_PIN); reads the analog value from the photoresistor.
    • Serial.print("Light Level: "); and Serial.println(lightLevel); print the light level to the serial monitor.
    • An if statement checks if the light level is above the threshold. If it is, the LED is turned off (digitalWrite(LED_PIN, LOW);); otherwise, the LED is turned on (digitalWrite(LED_PIN, HIGH);).
    • delay(500); introduces a 500ms delay before the next reading.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.