Hello, we were making a project for TÜBİTAK, the project's about assistive tech for people with disabilities.
It's a project where there is 2 servos (MG996R), 2 arduino unos (1 for power only, the other is for controlling) and an ESP32-CAM for vision in control of the servos but here in the prototype stage, we're doing Arduinos. Unfortunately, we couldn't use one arduino for everything because the power wouldn't be enough, after previously damaging boards using barrel jack + USB simultaneously in the past, i told the team something like "Let's use 2 arduinos, one for power and the other for comms", and they agreed.
I connected the servos and the arduinos with a common ground. At first it was great, but then the servos kept freezing randomly. I thought it was something to do with the cables, but they were high quality cables and they weren't hot either.
What could be the issue? Is it the Arduino's regulator?
Here's the code(s) for the arduino:
// THIS IS THE ARDUINO CODE
#include <Servo.h>
Servo xServo;
Servo yServo;
String cmd = "";
void setup() {
Serial.begin(115200);
xServo.attach(4);
yServo.attach(5);
xServo.write(90);
yServo.write(90);
}
void loop() {
while (Serial.available()) {
char c = Serial.read();
if (c == '\n' || c == '\r') {
if (cmd.length() > 0) {
handleCommand(cmd);
cmd = "";
}
} else {
cmd += c;
}
}
}
void handleCommand(String s) {
char axis = s.charAt(0);
int value = s.substring(1).toInt();
value = constrain(value, 0, 180);
if (axis == 'X' || axis == 'x') {
xServo.write(value);
}
else if (axis == 'Y' || axis == 'y') {
yServo.write(value);
}
Serial.print(axis);
Serial.println(value);
}
# THIS IS THE PYHTON SCRIPT FOR NOW
import tkinter as tk
import serial
import time
PORT = "COM4"
BAUD = 115200
DELAY = 0.0
ser = serial.Serial(PORT, BAUD, timeout=1)
time.sleep(2)
root = tk.Tk()
root.title("XY Servo Controller")
root.minsize(300, 360)
title = tk.Label(
root,
text="XY Servo Controller",
font=("Segoe UI", 16, "bold")
)
title.pack(pady=(10, 5))
info = tk.Label(
root,
text="Drag inside the square to move servos",
font=("Segoe UI", 10),
fg="gray"
)
info.pack(pady=(0, 10))
canvas = tk.Canvas(root, bg="#111", highlightthickness=0)
canvas.pack(fill="both", expand=True, padx=15, pady=10)
label = tk.Label(
root,
text="X: 90 Y: 90",
font=("Consolas", 12)
)
label.pack(pady=10)
def send_xy(event):
w = canvas.winfo_width()
h = canvas.winfo_height()
x = max(0, min(event.x, w))
y = max(0, min(event.y, h))
# HORIZONTAL REVERSED
servo_x = int((w - x) / w * 180)
# VERTICAL NORMAL
servo_y = int(y / h * 180)
servo_x = max(0, min(180, servo_x))
servo_y = max(0, min(180, servo_y))
ser.write(f"X{servo_x}\n".encode())
time.sleep(DELAY)
ser.write(f"Y{servo_y}\n".encode())
label.config(text=f"X: {servo_x} Y: {servo_y}")
r = max(5, min(w, h) // 40)
canvas.delete("cursor")
canvas.create_oval(
x - r, y - r,
x + r, y + r,
fill="#ff4444",
outline="",
tags="cursor"
)
canvas.bind("<Button-1>", send_xy)
canvas.bind("<B1-Motion>", send_xy)
def on_close():
ser.close()
root.destroy()
root.protocol("WM_DELETE_WINDOW", on_close)
root.mainloop()
I'd like to hear from you guys about a solution.
Thanks,
@the_com3_port
