Simple sketch to drive a stepper motor with a RC transitter

Hello All,

I thought I would share this simple sketch that I have cobbled together to operate a Leadshine 57HS22-C stepper motor using a Leadshine M325 driver and a Arduino Mega 2560 via remote control. The radio transmitter is a Turnigy 9x and a Turnigy reciever. The Arduino is recieving the signals from Ch1 of the reciever via pin 6. Pin 5 of the Arduino is going to the DIR (direction) pin of the stepper driver and Pin 4 is going to the PUL (pulse) pin of the stepper driver and 5V from the Arduino is going to the OPTO pin of the stepper driver.

When operating the stepper moves up to 180 degrees in one direction for a left or right movement of the transmitter control stick (in this case the aileron control of the mode 1 Turnigy 9x). It seems to work reasonably well although there is some "chatter" when stationary.

I hope someone finds this of use but please feel free to offer suggestions or improvements.

#include <AccelStepper.h>
// Define a stepper and the pins it will use
AccelStepper stepper(1, 4, 5);

int steps;
int ch1; // Here's where we'll keep our channel values
void setup()
{  
  Serial.begin(9600);
  pinMode(6, INPUT); // Set our input pins as such
  stepper.setMaxSpeed(3000);
  stepper.setAcceleration(3000);
}
void loop()
{
  if (stepper.distanceToGo() == 0)
  ch1 = pulseIn(6, HIGH, 25000); // Read the pulse width of 
  steps=(map(ch1, 1065,1915,-100,100)); // center at 0
  stepper.moveTo(steps);
  stepper.run();
  Serial.println(ch1);
}

The chatter may be caused by the Tx/Rx not producing a single pulsewidth at the mid point (or at any point, perhaps). You might consider building a little slack into your code so very small variations in pulsewidth are ignored.

...R

Thanks Robin. I will give that a go. Cheers.

Hi Im wondering if you can help me, I get readings from my code but the stepper doesnt move

#define THROTTLE_SIGNAL_IN 0
#define THROTTLE_SIGNAL_IN_PIN 2
#define NEUTRAL_THROTTLE 1500

volatile int nThrottleIn = NEUTRAL_THROTTLE;
volatile unsigned long ulStartPeriod = 0;
volatile boolean bNewThrottleSignal = false;

int x=100;

void setup()
{

pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
attachInterrupt(THROTTLE_SIGNAL_IN,calcInput,CHANGE);
Serial.begin(9600);
}

void loop()
{
// if a new throttle signal has been measured, lets print the value to serial, if not our code could carry on with some other processing
if(bNewThrottleSignal)
{
if (nThrottleIn > 1600)
{
noInterrupts();
Serial.println(nThrottleIn);
digitalWrite(8, HIGH);
digitalWrite(9,LOW);
digitalWrite(9, HIGH); //
delayMicroseconds(x);
digitalWrite(9, LOW);
interrupts();
}

Serial.println(nThrottleIn);

digitalWrite(8,LOW);
bNewThrottleSignal = false;
}
}

void calcInput()
{
// if the pin is high, its the start of an interrupt
if(digitalRead(THROTTLE_SIGNAL_IN_PIN) == HIGH)
{
// get the time using micros - when our code gets really busy this will become inaccurate, but for the current application its
// easy to understand and works very well
ulStartPeriod = micros();
}
else
{
// if the pin is low, its the falling edge of the pulse so now we can calculate thepulse duration by subtracting the
// start time ulStartPeriod from the current time returned by micros()
if(ulStartPeriod && (bNewThrottleSignal == false))
{
nThrottleIn = (int)(micros() - ulStartPeriod);
ulStartPeriod = 0;
// tell loop we have a new signal on the throttle channel
// we will not update nThrottleIn until loop sets
// bNewThrottleSignal back to false
bNewThrottleSignal = true;
}
}
}

Post a link to the datasheet for your stepper motor.
What stepper motor driver you are using?
What power supply are you using for the motor (volts and amps).

I'm not sure why you attached your question to a 2.5-years-dead Thread - but it was a short Thread so we can easily ignore the earlier stuff.

...R
Stepper Motor Basics
Simple Stepper Code