Arduino-Controlled Light-Tracking Servo System with L298N Motor Driver and ESP32-CAM
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to control a set of DC motors using an L298N DC motor driver, which is interfaced with an ESP32-CAM MB FLIP microcontroller. The circuit also includes an Arduino UNO connected to an Arduino Expansion Board, which manages a set of servos and LDR (Light Dependent Resistor) photoresistors. The servos are controlled based on the light intensity detected by the LDRs, allowing for light-tracking capabilities. Additionally, the circuit includes inductive proximity sensors for object detection and batteries for power supply.
Component List
- L298N DC Motor Driver: A module used for controlling high current DC motors. It can drive two DC motors simultaneously.
- Arduino UNO: A microcontroller board based on the ATmega328P, used for controlling various components like servos and sensors.
- Arduino Expansion Board: An expansion shield for Arduino UNO, providing easy access to different pins for connecting additional components.
- Servo: A rotary actuator or linear actuator that allows for precise control of angular or linear position, velocity, and acceleration.
- KY-018 LDR Photo Resistor: A light-sensitive device used to detect the intensity of light.
- Motor Amarillo Motorreductor Hobby: A yellow hobby gear motor used for various DIY projects.
- ESP32-CAM MB FLIP: A small camera module with Wi-Fi capabilities, which can be used for surveillance or other camera-related projects.
- Battery 12V: A power source for the motor driver and other components requiring 12V.
- 3.7V Battery: A power source for components requiring lower voltage.
- LJ18A3-H-ZBX Inductive Proximity Sensor: A sensor used to detect the presence of nearby objects without any physical contact.
- Tower Pro SG90 Servo: A small and lightweight servo used for applications where small size and weight are important.
Wiring Details
L298N DC Motor Driver
- OUT1, OUT2: Connected to one set of DC motor terminals.
- OUT3, OUT4: Connected to another set of DC motor terminals.
- 12V: Connected to the positive terminal of the 12V battery.
- GND: Connected to the ground terminal of the 12V battery and ESP32-CAM MB FLIP GND.
- 5V: Connected to ESP32-CAM MB FLIP 5V.
- IN1, IN2, IN3, IN4: Connected to ESP32-CAM MB FLIP IO pins (IO14, IO15, IO13, IO12) for motor control signals.
Arduino UNO
- Reset: Connected to Arduino Expansion Board RST.
- 5V, GND, Vin, A0-A5, AREF, D13-D2: Connected to corresponding pins on the Arduino Expansion Board.
- Unused pins: Not connected.
Arduino Expansion Board
- G, V: Connected to GND and VCC of servos, LDRs, and inductive proximity sensors.
- Digital pins 2-13: Connected to various sensors and actuators as per their control requirements.
Servos
- GND: Connected to the ground (G) on the Arduino Expansion Board.
- VCC: Connected to the voltage supply (V) on the Arduino Expansion Board.
- PWM/Signal: Connected to control pins on the Arduino Expansion Board (e.g., D5, D6, D9).
KY-018 LDR Photo Resistor
- Ground: Connected to the ground (G) on the Arduino Expansion Board.
- VCC: Connected to the voltage supply (V) on the Arduino Expansion Board.
- Signal: Connected to analog input pins on the Arduino Expansion Board (e.g., A0-A3).
Motor Amarillo Motorreductor Hobby
- vcc: Connected to the output pins of the L298N DC motor driver (OUT1, OUT4) and to the positive terminal of the 3.7V battery.
- GND: Connected to the output pins of the L298N DC motor driver (OUT2, OUT3) and to the negative terminal of the 3.7V battery.
ESP32-CAM MB FLIP
- GND: Connected to the ground terminal of the 12V battery.
- 5V: Connected to the 5V output of the L298N DC motor driver.
- IO Pins: Connected to the input pins of the L298N DC motor driver for motor control.
Batteries
- 12V Battery: Provides power to the L298N DC motor driver.
- 3.7V Battery: Provides power to the DC motors.
LJ18A3-H-ZBX Inductive Proximity Sensor
- VO (BLU): Connected to the ground (G) on the Arduino Expansion Board.
- VI (BRN): Connected to the voltage supply (V) on the Arduino Expansion Board.
- SIG (BLK): Connected to digital input pins on the Arduino Expansion Board (e.g., D2, D3).
Documented Code
Arduino UNO Code (sketch.ino)
#include <Servo.h>
#define SERVOPINH 5 //horizontal servo
#define SERVOPINV 6 //vertical servo
int tol=100; //The response range of illuminance, the smaller the value, the more sensitive the response, otherwise it is slow
//(the value is 10~100, the sensitivity is different depending on the ambient light intensity, the indoor light source changes greatly, and the change is smaller under the sun)
int dtime=100; //delay parameter. The smaller the value, the faster the response speed.
//On the contrary, the larger the value, the slower the response speed. Unit: milliseconds General value (10~100)
// Horizontal servo settings
Servo horizontal; //horizontal servo
int servoh = 90; //Initialize angle
int servohLimitHigh = 180; //The maximum angle of rotation in the horizontal direction
int servohLimitLow = 0; //The minimum angle of rotation in the horizontal direction
// Vertical Servo Settings
Servo vertical; //vertical servo
int servov = 120; //Initialize angle
int servovLimitHigh = 180; //The maximum angle of rotation in the vertical direction
int servovLimitLow = 90; //The minimum angle of rotation in the vertical direction
// 4 connection ports for photoresistor modules
const int ldrlt = A0; //top left
const int ldrrt = A1; //top right
const int ldrld = A2; //down left
const int ldrrd = A3; //down right
void setup()
{
horizontal.attach(SERVOPINH);
vertical.attach(SERVOPINV);
horizontal.write(servoh);
vertical.write(servov);
delay(100);
Serial.begin(9600);
}
void loop()
{
//Read the illuminance values of 4 photoresistor modules respectively
int lt = analogRead(ldrlt); //upper left
int rt = analogRead(ldrrt); //top right
int ld = analogRead(ldrld); //down left
int rd = analogRead(ldrrd); //down right
Serial.print("lt:");Serial.print(lt);Serial.print(" ");
Serial.print("rt:");Serial.print(rt);Serial.print(" ");
Serial.print("ld:");Serial.print(ld);Serial.print(" ");
Serial.print("rd:");Serial.println(rd);
//Average readings from adjacent photoresistor modules
int avt = (lt + rt) / 2;
int avd = (ld + rd) / 2;
int avl = (lt + ld) / 2;
int avr = (rt + rd) / 2;
int veg=(avt + avd + avl + avr) / 4;
//Adjust response parameters according to different light intensities
if(0<veg&&veg<300)
{ tol=map(veg,10,300,5,100);
dtime=map(veg,10,300,100,50);
}
else
{ tol=50;
dtime=50;
}
Serial.print("veg= ");Serial.println(veg);
Serial.print("tol= ");Serial.println(tol);
Serial.print("dtime= ");Serial.println(dtime);
//Then calculate the difference between the upper and lower rows and the average value of the left and right rows
int dvert = avt - avd; //upper and lower rows
int dhoriz = avl - avr; //left and right rows
//Check if the difference is within tolerance, otherwise change the vertical angle
if (-1*tol > dvert || dvert > tol)
{
if (avt > avd)
{
servov = ++servov;
if (servov > servovLimitHigh)
{
servov = servovLimitHigh;
}
}
else if (avt < avd)
{
servov= --servov;