Servo Motor and LCD Project: Output Based on Photoresistor

by bluejay701 in Circuits > Arduino

343 Views, 1 Favorites, 0 Comments

Servo Motor and LCD Project: Output Based on Photoresistor

IMG_9163.jpg

This is a simple project I have made to practice using the LCD and servo motor on the Arduino and as part of my high school's career presentation. It is easy to make and customize.

Reference: Paul McWhorter's Arduino Playlist

Supplies

  • Arduino UNO
  • Breadboard
  • Servo Motor
  • LCD
  • 330Ω and 5kΩ resistors
  • Potentiometer
  • Photoresistor
  • Jumper Wires

Build Circuit

Diagram with Tinkercad below shows circuit layout

Code

2024-02-06_055119.jpg
#include <Servo.h>
#include <LiquidCrystal.h>

int servoPin=6;
int lightVal;
int lightPin=A4;
int dt=250;
int angle;
Servo myServo;

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
Serial.begin(9600);
pinMode(lightPin,INPUT);
pinMode(servoPin,OUTPUT);
myServo.attach(servoPin);

lcd.begin(16, 2);
}

void loop() {
lightVal=analogRead(lightPin);
Serial.println(lightVal);
Serial.println(angle);
delay(dt);
angle=(-2./5.)*lightVal+344.;
myServo.write(angle);
  
lcd.setCursor(0, 0);
lcd.print("The weather is");

lcd.setCursor(0, 1);

if(angle<50){
  lcd.print("bright");
}
else if(angle>50 && angle<130){
  lcd.print("cloudy");
}
else if(angle>130){
  lcd.print("foggy ");
}

}

The angle is determined using a linear equation of angle (0-180 degrees for me) and the brightness based on what voltage is detected by the photoresistor (410-860 on the 0-1023 scale for me).

Working Demonstration

Arduino: Servo Motor Control with Photoresistor &amp; LCD