Hey guys, im trying to program a servo to turn 90 degrees CCW and 90 degrees CW automatically and simultaneously program a motor (through an H-bridge) to automatically run unless it measures a variable proportional to the voltage below 100 from input pin 2 which is connected to a phototransistor (meant to measure the brightness of the environment) on a DFRobot FireBeetle2 ESP32-C6. Unfortunately, when I ran this code:
long sum = 0;
Servo s1;
const uint8_t LDR = 2;
const uint8_t Motor_1 = 4;
const uint8_t Motor_2 = 7;
void setup() {
Serial.begin(9600);
pinMode(LDR, INPUT);
pinMode(Motor_1, OUTPUT);
pinMode(Motor_2, OUTPUT);
s1.attach(5);
s1.write(90); // Start at the center position
delay(500); // Allow servo to initialize
}
void loop() {
int voltage_read= (analogRead(LDR)*0.25);
digitalWrite(Motor_1, HIGH);
digitalWrite(Motor_2, LOW);
Serial.println(voltage_read);
sum=0;
for (int i = 0; i < 8; i++) {
sum += voltage_read;
delay(250); // Read value from each input and accumulate the sum
}
long average = sum / 8;
if(average < 100 ) {
Serial.print("Too dark!");
digitalWrite(Motor_1, LOW);
digitalWrite(Motor_2, LOW);
}
static int position = 90; // Start at center
static int direction = 1; // 1 = right, -1 = left
position += direction * 2; // Move 2 degrees per step
// Change direction at limits (110° and 70°)
if (position >= 190) {
direction = -1;
// Change direction to left
} else if (position <= 60) {
delay(25);
direction = 1; // Change direction to right
}
s1.write(position); // Move servo
delay(25); // Smooth movement
}
the motor system operated as predicted but the servo didn't operate. I made a new sketch and tested only the servo program:
#include <ESP32Servo.h>
Servo s1;
void setup() {
// put your setup code here, to run once:
s1.attach(5);
s1.write(90); // Start at the center position
delay(500); // Allow servo to initialize
}
void loop() {
// put your main code here, to run repeatedly:
static int position = 90; // Start at center
static int direction = 1; // 1 = right, -1 = left
position += direction * 2; // Move 2 degrees per step
// Change direction at limits (110° and 70°)
if (position >= 190) {
direction = -1;
// Change direction to left
} else if (position <= 60) {
delay(25);
direction = 1; // Change direction to right
}
s1.write(position); // Move servo
delay(25); // Smooth movement
}
and it successfully operated, indicating that the code, I believe, is not the issue. I began looking at my circuit, specifically, when I isolated the motor aspect, which I thought drained too much current, especially since it was in parallel, I found the servo still didn't drive. For further context, Ive tried with my laptop as VCC and an external battery holder that supplied 9V for the motor and a voltage regulator to lower that to 5V for the firebeetle and the rest of my circuit to handle and I measured the voltage with a multimeter to ensure my motor and servo circuit were getting 8V and 5V respectively. In case you guys would like to view my circuit, here's a picture I made of it below:
