gpiojoystepper

GPIO Joy Stepper Motor

A stepper motor rotates when you energize its four internal magnets in sequence.  Stepper motors are commonly used when you need to control motor rotation very precisely, such as with a 3D printer or a CnC machine.

You can write an XML configuration to control unipolar and bipolar stepper motors.

Unipolar stepper motors typically have five wires. One wire is connected to power, and the other four are connected to pins on the raspberry pi through a transistor (usually part of an integrated chip called a darlington).

In this example, an unipolar stepper motor is wired to pins 35 through 38. It is controlled with a half step sequence. It will run forward and reverse with the right stick up and down. The speed will start out slow with a 100 millisecond delay between steps and it will run with a 2 ms delay when the left stick is all the way up or down.

<GpioConfig>
  <PinModes>
    <Pin>
      <Number>35</Number>
      <Mode>Output</Mode>
    </Pin>
    <Pin>
      <Number>36</Number>
      <Mode>Output</Mode>
    </Pin>
    <Pin>
      <Number>37</Number>
      <Mode>Output</Mode>
    </Pin>
    <Pin>
      <Number>38</Number>
      <Mode>Output</Mode>
    </Pin>
  </PinModes>
  <StepperDrivers>
    <StepperSequence>
      <Id>1</Id>
      <SequenceItems>
        <Item>1, 0, 0, 1</Item>
        <Item>1, 1, 0, 0</Item>
        <Item>0, 1, 1, 0</Item>
        <Item>0, 0, 1, 1 </Item>
      </SequenceItems>
    </StepperSequence>
    <StepperDriver>
      <Name>Stepper One</Name>
      <Sequence>1</Sequence>
      <Pins>35,36,37,38</Pins>
      <MinDelay>2</MinDelay>
      <MaxDelay>100</MaxDelay>
      <Joystick direction="1">RightStickUp</Joystick>
      <Joystick direction="-1">RightStickDown</Joystick>
    </StepperDriver>
  </StepperDrivers>
</GpioConfig>

Bipolar stepper motors are a bit more complicated to drive, because you must reverse the polarity on the wires in order to energize the magnets in the correct sequence. An H-Bridge circuit is required to drive a bipolar stepper motor.

In this example, we are using the Adafruit Motor Hat which includes a PCA9685 port expansion chip and a pair of dual H-Bridges.  The PCA9685 chip is given a pin base number of 100.  The four wires of the bi-polar stepper motor are connected to M1 and M2 terminals on the motor hat.  The <PWMScale> element is used to control the motor at half power.

<GpioConfig>

  <PCAs>
    <PCA>
      <!-- adafruit motor hat at default address 0x60-->
      <Address>96</Address>
      <Base>100</Base>
      <Frequency>50</Frequency>
    </PCA>
  </PCAs>

  <PinModes>
    <Pin>
      <Number>108</Number>
      <Mode>PWMOutput</Mode>
    </Pin>
    <Pin>
      <Number>109</Number>
      <Mode>Output</Mode>
    </Pin>
    <Pin>
      <Number>110</Number>
      <Mode>Output</Mode>
    </Pin>
    <Pin>
      <Number>111</Number>
      <Mode>Output</Mode>
    </Pin>
    <Pin>
      <Number>112</Number>
      <Mode>Output</Mode>
    </Pin>
    <Pin>
      <Number>113</Number>
      <Mode>PWMOutput</Mode>
    </Pin>
  </PinModes>

  <StepperDrivers>
    <StepperSequence>
      <Id>2</Id>
      <SequenceItems>
        <Item>1, 0, 0, 0</Item>
        <Item>0, 1, 0, 0</Item>
        <Item>0, 0, 1, 0</Item>
        <Item>0, 0, 0, 1 </Item>
      </SequenceItems>
    </StepperSequence>

    <StepperDriver>
      <Name>Stepper</Name>
      <Sequence>2</Sequence>
      <Pins>108,-113,-108, 113</Pins>
      <PwmScale>0.5</PwmScale>
      <MinDelay>15</MinDelay>
      <MaxDelay>500</MaxDelay>
      <HBridge>108,110,109</HBridge>
      <HBridge>113,111,112</HBridge>
    </StepperDriver>

  </StepperDrivers>

</GpioConfig>