Running A SparkMax off of an arduino

I am attempting to run a SparkMax off of an Arduino Rev 4 Wifi. The SparkMax has external power, and I am using the pwm attachement that comes with the SparkMax. So far, I have had no luck moving the motors with the Arduino Code… Does anyone have any pointers?

Are you using a standard servo library?

attempted, to no avail. I know all the power and wiring is fine as I am able to spin it off of Rev hardware client, its certainly an arduino issue. Are arduinos able to put off the right frequency?

#include <Servo.h>

Servo sparkMax;  // create servo object to control the Spark MAX

void setup() {
  sparkMax.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  // Control the Spark MAX speed/direction by setting the angle of the servo
  // Mapping the servo angle to PWM values suitable for the REV Spark MAX
  // 0 degrees corresponds to full reverse, 90 degrees to stop, and 180 degrees to full forward
  sparkMax.writeMicroseconds(1000); // Run the motor in reverse at full speed
  delay(2000); // Wait for 2 seconds
  sparkMax.writeMicroseconds(1500); // Stop the motor
  delay(1000); // Wait for 1 second
  sparkMax.writeMicroseconds(2000); // Run the motor forward at full speed
  delay(2000); // Wait for 2 seconds
}

That looks like it should work. Maybe try writing an angle instead of microseconds?

sparkMax.write(45)

Do you have the black wire tied to the Arduino ground?

Edit: After googling, I’m seeing that some people have had issues with the Uno rev4 controlling servos, do you have a different Arduino to test the code on?

This is how I’ve always done it as well, this works as well for TalonSRX/VictorSPX motor controllers over PWM but we’ve always used an Arduino Mega or Arduino Nano and not a rev 4 (we don’t have the latest set).

Hey I ran your exact code on my Arduino UNO and was able to get my NEO 550 to spin with the Spark Max, but it had some ticking occurring. I measured the 2 pwm connections and was reading around 5V.

Yeah, I never got it to fully work as I wanted it to. good luck to you. if you can figure out a way to make it work flawlessly do let me know haha

Using writeMicroseconds() is by far a better choice than angle. It gives better control and much higher resolution.
Have you configured the SparkMax to use PWM instead of CAN?

You could also try using the default arduino analogWrite function for PWM control.

Not quite.
The arduino analogWrite PWM waveform is not the same as the Hobby PWM control signal used by servos, and motor controllers in FIRST.
Hobby servo is, generally speaking, a 50Hz signal with a pulse width ranging from 1000 micro seconds to 2000 micro seconds. This range covers full reverse to full forward, withh 1500 micro seconds representing neutral.

The freq. of the signal can be as high as 400Hz, but 50Hz is standard.

2 Likes