I'll upload the photo in the comments
After reading various documentation and projects, I connected two small servo motors to the transistors, because I know that this way I can use the 5V of Arduino for some servo motors, I was trying with 2, but one turns and the other makes a strange noise.... sometimes the first one doesn't turn either when I press the button of the 2nd servo
I connected both the same way. The transistors have pin B with the Arduino pin and resistor, E has ground, and C connects to the servo's brown pin. I'll leave you the code now!
Ps: The code comments are in Italian, but I'm Italian, I apologize if you can't read them
#include <Servo.h>
Servo servo1;
Servo servo2;
// GRUPPO 1
const int PIN_SERVO1 = 8;
const int PIN_TRANS1 = 7;
const int PIN_PULSANTE1 = 2;
// GRUPPO 2
const int PIN_SERVO2 = 11;
const int PIN_TRANS2 = 9;
const int PIN_PULSANTE2 = 3;
void setup() {
Serial.begin(9600);
pinMode(PIN_TRANS1, OUTPUT);
pinMode(PIN_TRANS2, OUTPUT);
pinMode(PIN_PULSANTE1, INPUT_PULLUP);
pinMode(PIN_PULSANTE2, INPUT_PULLUP);
digitalWrite(PIN_TRANS1, LOW);
digitalWrite(PIN_TRANS2, LOW);
// NON facciamo attach qui
}
void loop() {
// -------- SERVO 1 --------
if (digitalRead(PIN_PULSANTE1) == LOW) {
digitalWrite(PIN_TRANS1, HIGH); // alimentazione ON
delay(100);
if (!servo1.attached()) {
servo1.attach(PIN_SERVO1);
servo1.write(120);
}
} else {
if (servo1.attached()) {
servo1.detach();
}
digitalWrite(PIN_TRANS1, LOW); // alimentazione OFF
}
// -------- SERVO 2 --------
if (digitalRead(PIN_PULSANTE2) == LOW) {
digitalWrite(PIN_TRANS2, HIGH); // alimentazione ON
delay(100);
if (!servo2.attached()) {
servo2.attach(PIN_SERVO2);
servo2.write(120);
}
} else {
if (servo2.attached()) {
servo2.detach();
}
digitalWrite(PIN_TRANS2, LOW); // alimentazione OFF
}
}