Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO-Based Smart Light Controller with PID and LCD Display

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Smart Light Controller with PID and LCD Display

Circuit Documentation

Summary

This circuit is designed to control the brightness of an LED based on ambient light intensity using a PID controller. The system includes an Arduino UNO microcontroller, multiple potentiometers for tuning PID parameters, an ambient light sensor, an LCD display for real-time feedback, and additional passive components like resistors, capacitors, and inductors.

Component List

  1. Arduino UNO

    • Description: Microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. Potentiometer

    • Description: Variable resistor used for adjusting PID parameters.
    • Pins: GND, Output, VCC
  3. Ambient Light Sensor

    • Description: Sensor to measure ambient light intensity.
    • Pins: GND, OUT, VCC
  4. LCD 20x4 I2C

    • Description: 20x4 character LCD display with I2C interface.
    • Pins: GND, 5V, SCA, SCL
  5. Electrolytic Capacitor

    • Description: Capacitor used for filtering.
    • Pins: -, +
    • Properties: Capacitance: 0.000001 Farads
  6. Inductor

    • Description: Inductor used in the circuit.
    • Pins: pin 0, pin 1
    • Properties: Inductance: 0.1 Henrys
  7. Resistor

    • Description: Resistor used in the circuit.
    • Pins: pin1, pin2
    • Properties: Resistance: 200 Ohms
  8. Bulb

    • Description: Bulb used as an indicator.
    • Pins: GND, power

Wiring Details

Arduino UNO

Potentiometer

Ambient Light Sensor

LCD 20x4 I2C

Electrolytic Capacitor

Inductor

Resistor

Bulb

Code Documentation

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

// Initialize the LCD with I2C address 0x27 and size 20x4
LiquidCrystal_I2C lcd(0x27, 20, 4);

const int MAX_DUTY_CYCLE = 255; // Max duty cycle for 8-bit resolution

// PID parameters and variables
double Setpoint, Input, Output;
double kp, ki, kd;
PID myPID(&Input, &Output, &Setpoint, kp, ki, kd, DIRECT);

// Pin definitions
const int temt6000Pin = A0; // Analog pin for TEMT6000 sensor
const int ledPin = 3;       // PWM pin for LED control via MOSFET
const int kpPin = A1;       // Analog pin for potentiometer to adjust Kp
const int kiPin = A2;       // Analog pin for potentiometer to adjust Ki
const int kdPin = A3;       // Analog pin for potentiometer to adjust Kd

// Fading parameters
int targetBrightness = 0;
int currentBrightness = 0;
const int fadeAmount = 1; // Fade amount for each step
const int fadeDelay = 30; // Delay in milliseconds for each fade step

//lux tuning parameter
const int LUX_TUNING = 200;

void setup() {
  Serial.begin(115200);

  // Initialize the LCD
  lcd.init();
  lcd.backlight();

  // Set initial PID parameters
  Setpoint = 0; // Desired light intensity in lux

  myPID.SetMode(AUTOMATIC);
  myPID.SetSampleTime(100); // Sample time in milliseconds

  pinMode(ledPin, OUTPUT); // Set LED pin as output
}

void loop() {
  // Read the light intensity from the TEMT6000 sensor
  int light_value = analogRead(temt6000Pin);
  float volts = light_value * 3.3 / 1023.0; // Arduino has 10-bit ADC resolution and 3.3V reference voltage
  float amps = volts / 10000.0; // across 10,000 Ohms
  float microamps = amps * 1000000;
  float lux = microamps * 2.0;
  Input = lux;

  // Read and scale potentiometer values to 0-10 range for PID parameters
  kp = analogRead(kpPin) * (10.0 / 1023.0);
  ki = analogRead(kiPin) * (10.0 / 1023.0);
  kd = analogRead(kdPin) * (10.0 / 1023.0);
  myPID.SetTunings(kp, ki, kd);

  // Compute the PID output
  myPID.Compute();
  
  // Set the LED brightness based on the lux value directly (scale 0-255)
  targetBrightness = map(lux, 0, LUX_TUNING, MAX_DUTY_CYCLE, 0);
  targetBrightness = constrain(targetBrightness, 0, MAX_DUTY_CYCLE);

  // Smoothly adjust currentBrightness towards targetBrightness
  if (currentBrightness < targetBrightness) {
    currentBrightness = min(currentBrightness + fadeAmount, targetBrightness);
  } else if (currentBrightness > targetBrightness) {
    currentBrightness = max(currentBrightness - fadeAmount, targetBrightness);
  }

  // Set the LED brightness
  analogWrite(ledPin, currentBrightness);

  // Display PID parameters and light intensity on the LCD
  lcd.setCursor(0, 0);
  lcd.print("P=");
  lcd.print(kp, 2);
  lcd.print(" I=");
  lcd.print(ki, 2);

  lcd.setCursor(0, 1);
  lcd.print("D=");
  lcd.print(kd, 2);

  lcd.setCursor(0, 2);
  lcd.print("Light Intensity:");
  lcd.setCursor(0