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

Arduino-Controlled Servo with Light Sensing

View and Edit Project In Cirkit DesignerImage of Arduino-Controlled Servo with Light Sensing

Circuit Documentation

Summary of the Circuit

This circuit is designed to utilize an Arduino UNO microcontroller to read light levels from two photocells (Light Dependent Resistors, LDRs) and control a servo motor based on the average light intensity detected. The circuit includes a power supply consisting of two 18650 Li-ion batteries, a TP4056 charging module, and a solar cell for charging. The servo motor's position is adjusted according to the average light intensity, which could be used for applications such as solar tracking or light-sensitive control mechanisms.

Component List

Resistor

Photocell (LDR)

Arduino UNO

Tower Pro SG90 Servo

18650 Li-ion Battery x 2

Solar Cell

TP4056 Charging Module

Wiring Details

Resistor

Photocell (LDR)

Arduino UNO

Tower Pro SG90 Servo

18650 Li-ion Battery x 2

Solar Cell

TP4056 Charging Module

Documented Code

#include <Servo.h>

Servo myServo;  // create servo object to control a servo

// Constants for the analog pins that the photocells are connected to
const int photocellPin1 = A0;
const int photocellPin2 = A1;

// Variables to store the read values from the photocells
int photocellValue1 = 0;
int photocellValue2 = 0;
int averageValue = 0;

void setup() {
  myServo.attach(9);  // attaches the servo on pin 9 to the servo object
  Serial.begin(9600); // start serial communication at 9600 baud rate
}

void loop() {
  // Read the value from the photocells
  photocellValue1 = analogRead(photocellPin1);
  photocellValue2 = analogRead(photocellPin2);

  // Calculate the average of the two values
  averageValue = (photocellValue1 + photocellValue2) / 2;

  // Map the average value to a range suitable for the servo (0-180 degrees)
  int servoAngle = map(averageValue, 0, 1023, 0, 180);

  // Move the servo to the mapped angle
  myServo.write(servoAngle);

  // Print the values to the serial monitor
  Serial.print("Photocell 1: ");
  Serial.print(photocellValue1);
  Serial.print("\tPhotocell 2: ");
  Serial.print(photocellValue2);
  Serial.print("\tAverage: ");
  Serial.print(averageValue);
  Serial.print("\tServo Angle: ");
  Serial.println(servoAngle);

  // Wait for a bit before reading again
  delay(500);
}

This code is designed to be uploaded to the Arduino UNO microcontroller. It initializes a servo motor and reads the analog values from two photocells. It calculates the average of these values and maps this average to a servo angle, which is then used to position the servo motor. The values are also printed to the serial monitor for debugging purposes.