Contents

1. Objective

2. Analog output - Pulse Width Modulation

3. Stepper Motor

4. Main board PCB

5. Adding library on Eagle

  • 5.1 Component
  • 5.2 Symbol
  • 5.3 Package
  • 6. Stepper Motor Driver PCB

    7. Stepper Motor Driver Carriers

  • 7.1 A4988 Stepper Motor Driver Carriers PCB board
  • 7.1 A4988 Stepper Motor Driver Carriers PCB board
  • 7.2 Current limiter on board
  • 8. Code and Result

    9. Download

    1. Objective

    How to use PWM for analog output and drive a stepper motor using the driver, A4988.

    2. Analog output - Pulse Width Modulation

    Most microcontrollers provide only digital outputs (logic 0 or 1). Transistors are also much more efficient when acting as on and off switches, rather than producing intermediate outputs. They dissipate a lot of power when neither completely on nor off. How can we control an output continuously by only switching it on and off? The common method of simulating a continuously variable output is pulse width modulation (PWM): The duty cycle of a square wave is varied to change the average power delivered to a load.

    For example, the period of the square wave is 256 units and the duration of the pulse output is 1 from 0, fully off to 256, fully on.

    Control of Servos

    Servos are controlled by a stream of pulses: The position of the servo depends on the length of pulse. For example, 1.0 ms - far left (-45 degree or - 90 degree), 1.5 ms - centered (0 degree) and 2.0 ms - far right (+45 degree or +90 degree).

    3. Stepper Motor

    Connect all the cables like the schematic below.

    Wire the A to brown cable, A NOT to orange, B to red and B NOT to yellow cable.

    1 revolution

    After puting all the wires properly, let's write the code to run the stepper motor in one revolution by writing the for loop to have 50% of PWM in every 500 milli seconds.

    2 revolution

    Without changing the direction pin we can easily revolve the motor in two revolution by writing 400 pulses in the for loop. 200 pulse in this case will give run 1 revolution of the stepper motor because according to the data sheet the step angle is 1.8 degree so one on and off signal with rotate the motor 1.8 degree. Thefore to have 360 degree, we have to devide 360 degree by 1.8 degree and get 200 steps.

    Feel free to change the direction by changing the direction pin into HIGH or LOW and the number of pulse for changing the angle of the revolution.

    4. Mainboard PCB

    The hardware that I use is modified from Satshakit and then I made a another board to connect the driver to the main PCB.

    Further documentation about the board can be found in the input assignment.

    5. Adding library on Eagle

    Have you ever wanted to add a component and could not find the library for it? and This is the tutorial how to add an electornic component that you want to put it on the PCB.

    https://www.autodesk.com/products/eagle/blog/library-basics-part-1-creating-first-package-autodesk-eagle/

    Add a new package

    Take a look on the data sheet for the name and the dimension.

    https://cdn-shop.adafruit.com/datasheets/19963.pdf

    5.1 Component

    Create the components that you have take a look by referencing the dimension on the data sheet.

    Draw the component by adding the pins and lines.

    Edit the drawing according to the coordinate to get an accurate dimensions.

    Add a text of ">Name" and ">Value" if needed

    5.2 Symbol

    After finish creating a component, you can continue to add the symbol for creating the schematic later on.

    Click on new and add the packeage that we have made.

    Draw a box.

    Add the pins and name it.

    Save the symbol.

    5.3 Package

    Create a new package for connecting the right connections between the symbol and component.

    Open the right package and component.

    Connect the pins and pad.

    Afterward you can update the library and use the component that you have made.

    6. Stepper Motor Driver PCB

    Since a driver motor needs a driver to run the motor, I made a PCB to connect a A4988 stepper motor driver to my PCB so it is going to be more organize.

    7. Stepper Motor Driver Carriers

    7.1 A4988 Stepper Motor Driver Carriers PCB board

    The board is design to simplify the connection of the stepper motor driver ti the Satsha GBRL board.

    Pololu

    Pins MS1, MS2 and MS3 have to be connected in 5V in the A4988 stepper motor driver carriers so that it offers 1/16 step microstepping.

    This driver operates from 4.5V ti 35V and can diliver ip to approximately 1.5 A.

    7.2 Current limiter on board

    There is an on board timer poteniometer for setting the curent limit, turn it counter clockwise to decrease the current.

    After finished milling the PCB, we need to drill the holes with the diameter of 0.9 mm and 1 mm.

    Part list:

  • PCB board
  • A4988 Stepper Motor Driver Carriers
  • Female pins
  • Male pins
  • Wire board 3.5 mm
  • Solder them all in the board and plug the pololu stepper motor driver.

    8. Result

    Code:


    // defines pins numbers
    const int stepPin1 = 10;
    const int dirPin1 = 12;

    void setup() {
     // put your setup code here, to run once:
     pinMode(stepPin1,OUTPUT);
     pinMode(dirPin1,OUTPUT);
    }

    void loop() {
     // put your main code here, to run repeatedly:
     digitalWrite(dirPin1,HIGH); // Enables the motor  to move in a particular direction
     digitalWrite(stepPin1,LOW);
     //one revolution
     for(int x = 0; x < 200; x++) {
      digitalWrite(stepPin1,HIGH);
      delayMicroseconds(700);
      digitalWrite(stepPin1,LOW);
      delayMicroseconds(700);
     }
     delay(1000);
     digitalWrite(dirPin1,LOW); // Enables the motor  to move in a particular direction
     digitalWrite(stepPin1,LOW);
     //one revolution
     for(int x = 0; x < 200; x++) {
      digitalWrite(stepPin1,HIGH);
      delayMicroseconds(700);
      digitalWrite(stepPin1,LOW);
      delayMicroseconds(700);
    }
     delay(1000);

    }

    Video:

    9. Download

    stepper Motor code

    A4988 PCB