I'm using an Arduino Uno with a 25Kg Feetech servo motor and encoder.
The problem I'm having is that when the Arduino is powered via USB from the computer the program works correctly, while if it is connected via USB to an external power supply or if it is powered via the Jack from 12V to 7V the program works for the first 10 seconds then it stops.
#include <Servo.h>
#define servoPIN 9
#define inputCLK 4
#define inputDT 5
#define inputButton 7
#define led 13
// Create a Servo object
Servo myservo;
int counter = 0, currentStateCLK, previousStateCLK, servoPositionMin = 94, servoPositionMid1 = 85, servoPositionMid2 = 75, servoPositionMid3 = 65, servoPositionMax = 56, valButton = 0;
bool stateFunction = false, goAtoB = true;
unsigned long oldTimeMillis = 0;
void setup() {
pinMode(inputCLK, INPUT);
pinMode(inputDT, INPUT);
pinMode(inputButton, INPUT_PULLUP);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
// Setup Serial Monitor
Serial.begin(9600);
// Attach servo on pin 9 to the servo object
myservo.attach(servoPIN);
//EEPROM.write(0, true); //togliere il commento per far resettare la posizione del servomotore**************************************************
// Read the initial state of inputCLK
// Assign to previousStateCLK variable
previousStateCLK = digitalRead(inputCLK);
myservo.write(servoPositionMin);
}
void loop() {
// Read the current state of inputCLK
currentStateCLK = digitalRead(inputCLK);
valButton = digitalRead(inputButton);
if (valButton == LOW) {
if (millis() - oldTimeMillis >= 10000) {
if (!stateFunction) {
stateFunction = true;
digitalWrite(led, HIGH);
} else {
stateFunction = false;
digitalWrite(led, LOW);
}
myservo.write(servoPositionMin);
counter = 0;
oldTimeMillis = millis();
}
} else if (valButton == HIGH) {
oldTimeMillis = millis();
}
if (currentStateCLK != previousStateCLK) {
// If the inputDT state is different than the inputCLK state then
// the encoder is rotating counterclockwise
if (digitalRead(inputDT) != currentStateCLK) {
counter--;
goAtoB = false;
} else {
// Encoder is rotating clockwise
counter++;
goAtoB = true;
}
// Update previousStateCLK with the current state
previousStateCLK = currentStateCLK;
}
if (!stateFunction) {
// If the previous and the current state of the inputCLK are different then a pulse has occured
if (counter == 6 && goAtoB == true) {
myservo.write(servoPositionMax);
}
if (counter == 4 && goAtoB == false) {
myservo.write(servoPositionMin);
}
} else {
if (counter == 0) {
myservo.write(servoPositionMin);
}
if (counter == 2) {
myservo.write(servoPositionMid1);
}
if (counter == 4) {
myservo.write(servoPositionMid2);
}
if (counter == 6) {
myservo.write(servoPositionMid3);
}
if (counter == 8) {
myservo.write(servoPositionMax);
}
}
}
Yes I know. I said I've tried all kinds of voltages, but nothing has worked
With the 5V of Arduino the are enough in my opinion.
But my real question is: why does it work by powering the arduino from the USB socket of the computer, while if I connect it to an external power supply it doesn't!!!
We don't work on opinions here we work on facts. Your opinions are at odds with the facts.
How much current will your stepping motor take? Remember. A stepping motor draws the maximum current when it is stationary.
A servo can pull up to 1A stall current every time it starts to move from a standing start.
This is almost certainly caused by an internal regulator over heating because you are drawing too much current. When powering it from the DC jack.
It sounds like your computer is not a very good one and the USB current protection it should have is not kicking in correctly.
I tried to connect the external power via the round DC connector with 12/10/9/8/7V and nothing works. Via the USB of the Arduino UNO connecting it first to the USB socket of the computer everything works, then to a USB socket of a my power strip and nothing works anymore
Your scheme is incorrect. Please note that the arduino can not a power source for the servos, motors, displays etc
When you power the board via round DC connector - it works through internal board regulator. This regulator doesn't have enough ability to feed your servo, it designed to power the board only.
You need to power your servo with separate power supply. As a compromise, you can power both board and servo with 5v external power supply, connected to board (to 5v and GND pins) and to servo in parallel .
I've tried powering the Arduino and all other components via the Vin and GND pin and everything works fine, so I guess I'll use this power mode.
Instead, trying to power everything through the 5V pin, it works for the first 10 seconds and then doesn't make the servo move anymore