ad space

Using LED as light sensor with Arduino

As explained in the How common LED can be used as Light Sensors and Light Detection with General Purpose Transistors and LEDs for Microcontrollers, a general and readily available LED can be used as light sensor(photo diode). Here it is shown how we can read the current and voltage across the output LED or the load.

Using LED as light sensor with Arduino


To display current and voltage values on an I2C LCD using the Arduino, we'll first need to set up the Arduino to read the sensor output and use a simple formula to calculate the current and voltage. Then, we'll display these values on an I2C LCD.

Required Components:

  • Arduino Uno
  • APDS-9002 Light Sensor
  • 2N3904 Transistors (Darlington Pair)
  • I2C LCD Display
  • 220 Ohm Resistor
  • LED

Wiring Diagram:

  1. APDS-9002 Light Sensor:

    • Pin 1 (Anode) to Base of Q2
    • Pins 2, 3 (Cathode) to Ground
  2. Transistors (2N3904 Darlington Pair):

    • Collector of Q2 to Base of Q3
    • Emitter of Q2 to Base of Q3
    • Emitter of Q3 to Ground
    • Collector of Q3 to LED Cathode
  3. Resistor and LED:

    • Resistor (220 Ohm) from +5V to LED Anode
    • LED Cathode to Collector of Q3
  4. Arduino:

    • A0 to Collector of Q3 (to read the amplified sensor signal)
  5. I2C LCD:

    • SDA to A4
    • SCL to A5
    • VCC to +5V
    • GND to Ground

Libraries Required:

  • LiquidCrystal_I2C: This library is used for interfacing with the I2C LCD. You can install it through the Arduino Library Manager by searching for "LiquidCrystal I2C."

Arduino Code:

Here is the complete code to read the sensor values and display the current and voltage on the I2C LCD.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Initialize the I2C LCD (address 0x27, 16 columns, 2 rows)
LiquidCrystal_I2C lcd(0x27, 16, 2);

int sensorPin = A0; // Analog pin connected to the collector of Q3
int sensorValue = 0;
float current = 0.0;
float voltage = 0.0;

const float Vcc = 5.0; // Supply voltage
const float resistorValue = 220.0; // Resistor value in ohms

void setup() {
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
Serial.begin(9600); // Start serial communication for debugging
}

void loop() {
// Read the analog value from the sensor
sensorValue = analogRead(sensorPin);

// Calculate voltage
voltage = (sensorValue * Vcc) / 1023.0; // Convert ADC value to voltage

// Calculate current using Ohm's Law (I = V / R)
current = voltage / resistorValue; // Current in Amps

// Display the voltage on the LCD
lcd.setCursor(0, 0);
lcd.print("Voltage: ");
lcd.print(voltage, 2); // Display voltage with 2 decimal places
lcd.print(" V");

// Display the current on the LCD
lcd.setCursor(0, 1);
lcd.print("Current: ");
lcd.print(current * 1000, 2); // Convert current to mA and display with 2 decimal places
lcd.print(" mA");

// Print to Serial Monitor for debugging
Serial.print("Voltage: ");
Serial.print(voltage, 2);
Serial.println(" V");

Serial.print("Current: ");
Serial.print(current * 1000, 2);
Serial.println(" mA");

delay(1000); // Update every second
}

Explanation of the Code:

  1. Libraries and LCD Initialization:

    #include <Wire.h>
    #include <LiquidCrystal_I2C.h>
     

  • Wire.h is included for I2C communication.
  • LiquidCrystal_I2C.h is included for controlling the I2C LCD.
  • The LCD is initialized with the address 0x27, which is the typical address for I2C LCDs. If your display has a different address, you may need to adjust this.
  • Constants and Variables:

    int sensorPin = A0;
    float current = 0.0;
    float voltage = 0.0;

    const float Vcc = 5.0;
    const float resistorValue = 220.0;
     

  • sensorPin is the analog pin where the output from the transistors is read.
  • Vcc is the supply voltage, assumed to be 5V in this setup.
  • resistorValue is the resistance of the resistor used in the circuit (220 ohms).
  • Setup Function:

    void setup() {
    lcd.init();
    lcd.backlight();
    Serial.begin(9600);
    }


    Initializes the LCD and the serial monitor for debugging purposes.

    Loop Function:

    void loop() {
    sensorValue = analogRead(sensorPin);
    voltage = (sensorValue * Vcc) / 1023.0;
    current = voltage / resistorValue;
    }

  • Reads the analog value from the sensor and calculates the voltage and current.
  • voltage is calculated by converting the analog reading to actual voltage using the ADC reference voltage (Vcc) and the resolution (1023 for 10-bit ADC).
  • current is calculated using Ohm’s Law (I = V / R), where R is the resistance in ohms.
  • Display on LCD:

    Serial.print("Voltage: ");
    Serial.print(voltage, 2);
    Serial.println(" V");

    Serial.print("Current: ");
    Serial.print(current * 1000, 2);
    Serial.println(" mA");

     Prints the voltage and current to the Serial Monitor for debugging.

    Explanation of Calculations:

    1. Voltage Calculation:

      • voltage = (sensorValue * Vcc) / 1023.0;
      • This line calculates the voltage across the resistor (and LED). The ADC value (sensorValue) from pin A0 is converted to the corresponding voltage based on the supply voltage Vcc (5V) and ADC resolution (10-bit, hence 1023).
    2. Current Calculation:

      • current = voltage / resistorValue;
      • This formula uses Ohm's Law to calculate the current flowing through the LED, where resistorValue is 220 ohms.

       

      •  Watch the following video:



         

         

    Post a Comment

    Previous Post Next Post