Servo doesn't work with arduino but it does work with a servo tester

so for my project i a using a couple servo's. before i say anything else YES the sevo's are powerd by a external power supply. they are power by a 6s (22,2v) 5000mAh li-po battery that goes trough this V-reg to bring it down to 6v :
QSKJ DC-DC Adjustable Step-down Buck Converter XL4016 200W

the servo:
TD-8120MG 20KG 180°

new servo:
TD-8125MG 25KG 180°

i tried different pins on the arduino mega 5, 6, 10 and none of them showed any movement, so i though it was broken. bought a new one and that one works on pin 5. so for 'fun' i decided to test the 'broken' servo with a servo tester and it worked. and i have no idea why.

the servo also had a side effect or something, because after like a minute of being turned on, it decided to start moving to a couple angles while nothing was telling it to do so. the arduino had a code that was for another servo on a different pin. (that servo worked fine).

right now i have a servo that i think really is broken because a cable was smoking and it got quite warm without moving (it used to work just fine). and i was thinking if i can replace the broken servo with the 'broken' servo.

(for both servo's: i have 0 clue on how the broke)

i just came on here to learn what had gone wrong and maybe how i can detect it in the future.

1 Like

Have the external power supply and the Arduino got a common GND connection ?

No good will come of that

A couple of servo's what? :wink:

Can you show us your code?

yes everything in my system is power by that battery

here are my schematics:
(it is the best i could do because it is alot)


1x MG995 (the one that is broken rn)
2x TD-8125MG 25KG 180°
1x an older one i got form my dad's boxes of servo's and motors that i turned into a 360° servo (i think it is quite similair to the MG995 servo.

as for the code i can show you it but i dont think it will be of any use because the 'broken' servo has been 'broken' for a while now and i was using a different code back then that i don't have anymore.

note: the 'broken servo also didn't move when i had a code dedicated to it

but here you go:

dedicated code:

#include<Servo.h>

Servo servo;

void setup() {
  
servo.attach(5);
servo.write(90);

}

void loop() {

servo.write(80);
delay(800);
servo.write(90);
delay(800);
servo.write(100);
delay(800);
servo.write(90);
delay(800);
}

real code: (something is still wrong with it bcs it doesn't work)

transmitter:

#include <SPI.h>
#include <RF24.h>
#include <nRF24L01.h>

#define CE_PIN 7
#define CSN_PIN 6

RF24 radio(CE_PIN, CSN_PIN);
const byte pipe[] = "00001";

const byte joystickPins[] = {A7, A2, A3, A5};
const byte joysticksCount = sizeof joystickPins / sizeof * joystickPins;

struct __attribute__ ((packed)) t_message {
  int16_t rawValues[joysticksCount];
} payload, previousPayload;


void setup() {
  pinMode(10, OUTPUT);
  radio.begin();
  Serial.begin(9600);
  radio.openWritingPipe(pipe);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}

void loop() {
  // read the joysticks
  for (byte i = 0; i < joysticksCount; i++) {
    payload.rawValues[i] = analogRead(joystickPins[i]);
    payload.rawValues[i] = analogRead(joystickPins[i]) & 0xFFFD; // two reads for stability, dropping the 2 LSb to filter out instability
  }

  // broadcast the data if it has changed
  if (memcmp(&payload, &previousPayload, sizeof(t_message)) != 0) {  // returns 0 when they match, https://cplusplus.com/reference/cstring/memcmp/
    radio.write(&payload, sizeof(payload));
    previousPayload = payload;
  }
}

receiver:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>

#define CE_PIN 49
#define CSN_PIN 48

RF24 radio(CE_PIN, CSN_PIN);
const byte pipe[] = "00001";

const byte servoPins[] = {2, 5, 4, 3};     //s1 =(2) s2 =(5) s3 =(4) s4 =(3)
const byte servosCount = sizeof servoPins / sizeof * servoPins;
const int initialPositions[servosCount] = {90, 90, 90, 84};
const int mappedRanges[servosCount][2] = {{80, 100}, {65, 115}, {65, 115}, {80, 88}};

Servo servos[servosCount];

struct __attribute__ ((packed)) t_message {
  int16_t rawValues[servosCount];
} payload;
uint8_t messageBuffer[sizeof(t_message)];

void setup() {
  pinMode(10, OUTPUT);

  for (byte i = 0; i < servosCount; i++) {
    servos[i].write(initialPositions[i]);     //set starting positions
    servos[i].attach(servoPins[i]);
  }

// Serial
  Serial.begin(9600);
//radio
  radio.begin();
  radio.openReadingPipe(0, pipe);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    radio.read(messageBuffer, sizeof messageBuffer);
    memcpy(&payload, messageBuffer, sizeof payload);
    Serial.print(F("New positions: "));
    for (byte i = 0; i < servosCount; i++) {
      int angle = map(payload.rawValues[i], 0, 1023, mappedRanges[i][0], mappedRanges[i][1]);
      Serial.print(angle); Serial.write('\t');
      servos[i].write(angle);     //set starting positions
    }
    Serial.println();
  }
}

note 2: i did not write this code, someone else wrote it for me

Does that compile?
(It's not a rhetorical question, I'm on my phone here)
What does it do, if it does compile?

sorry, english is not my first language, but after searching up what 'compile' means i think u mean 'what does that do?' or 'what type of information does that gather?'

if my english is correct i think your answer is that it counts the servo pins and then connects them to their intial postitions, map() functions and eventually for the servo.write

To find the number of elements in an array, you'd normally find the number of bytes the whole array uses (using the size of operator) and divide that value by the number of bytes used by one element of that array.
It looks like you're dividing by the number of bytes in a pointer.

When I say "does it compile?", I mean (more or less) "does the compiler complain about that line or expression?".

to be honest...
i know what you are saying, but at the same time my brain just stops working

for the complaining part, it is not giving any errors, i can upload the code with no problems.

read it a couple of times and now i understand it but what do you mean with 'in a pointer'
or, what is 'a pointer' ?

int X; // this is a variable
int * pX= &X; // this is a variable pointer to the variable X. NB the sizeof X may or may not be the same sizeof pX (depending on platform)

so you mean that they could be the same?
(im very very new to all this sizeof stuff)

Normally, this would be written Please ignore me, I'm an idiot.

so nothing is wrong with that bit of code?

I don't think so, it's just written in a slightly obscure way.

okay,

update:

did nothing to the code, checked the serial monitor of the receiver, got nothing, turned my transmitter on and of and al of a sudden a servo starts moving when i move the joysticks, the new one i just put in. so that is good.
and now we are back to my starter question(s).

plus: why does my transmitter stop working now and then?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.