Example Code for Arduino-Motor Control

Explore sample code to control motors using Arduino, focusing on PPM signals to adjust speed and direction. Ideal for DIY enthusiasts looking to enhance their projects with precise motor manipulation.

Hardware Preparation

  • DFRduino UNO R3 (or similar) x 1
  • USB Cable
  • PPM Motor Driver Modules
  • M-M/F-M/F-F Jumper wires

Software Preparation

Sample Code

/*PPM output signal is a 50Hz PWM signal, so every control signal should be guaranteed to be 20ms 
     * The sum of delayMicroseconds in each control signal should be 20ms
     * The High level duration of every control signal determines the motor's speed, direction(forward, backward), and stop. 
     * 1500us high level duration: stop rotating 
     * Increment in high level duration from 1500us to 2000us: move backward and speed up 
     * Decrement in high level duration from 1500us to 1000us: move forward and speed up
     * delayMicroseconds():since 20ms will overflow, the value inside should perferably not exceed 16000. Delay it twice to achieve 20ms PWM cycle.
    */
    uint8_t motor1_pin=3;                       /*Set control pin for motor 1 */
    uint8_t motor2_pin=5;                       /*Set control pin for motor 2*/
    int num = 50;

    void motor_rotation(uint8_t pin,int t)      /*Motor rotate*/
    {
            digitalWrite(pin,HIGH);
            delayMicroseconds(t);
            digitalWrite(pin,LOW);
            delayMicroseconds(10000);
            delayMicroseconds(10000-t);
    }

    void all_motor_rotation(uint8_t pin0, uint8_t pin1, int t)  /*Two motor rotate at the same speed and direction */
    {
            digitalWrite(pin0,HIGH);
            digitalWrite(pin1,HIGH);
            delayMicroseconds(t);
            digitalWrite(pin0,LOW);
            digitalWrite(pin1,LOW);
            delayMicroseconds(10000);
            delayMicroseconds(10000-t);
    }

    void motor_forward_acc(uint8_t pin) {        /*Motor speeds up in forward movement*/
            for(int i = 0; i<10; i++) {
                    for(int j = 0; j<num; j++) {
                            motor_rotation( pin, 1500-i*50); /*Decrement in high level duration from 1500us to 1000us: move forward and speed up*/
                    }
            }
    }

    void motor_reversal_acc(uint8_t pin) {       /*Motor speeds up in backward movement*/
            for(int i = 0; i<10; i++) {
                    for(int j = 0; j<num; j++) {
                            motor_rotation( pin, 1500+i*50); /*Increment in high level duration from 1500us to 2000us: move backward and speed up*/
                    }
            }
    }

    void setup()
    {
            pinMode(motor1_pin,OUTPUT);
            pinMode(motor2_pin,OUTPUT);
    }
    void loop()
    {
            //motor_forward_acc(motor1_pin);                     /*Motor 1 speeds up in forward movement*/
            //motor_forward_acc(motor2_pin);                     /*Motor 2 speeds up in forward movement*/
            //motor_reversal_acc(motor1_pin);                    /*Motor 1 speeds up in backward movement*/
            //motor_reversal_acc(motor2_pin);                    /*Motor 2 speeds up in backward movement*/
            for(int i =0; i<num; i++) {                          /*Motor 1 rotates backward*/
                    motor_rotation( motor1_pin, 1800);               /*High level duration is 1800us, which is in 1500us-2000us, motor 1 rotates backward */
            }
            for(int i =0; i<num; i++) {                           /*Motor 1 stops rotating*/
                    motor_rotation(motor1_pin, 1500);                /*High level duration equals to 1500us, motor 1 stops rotating*/
            }
            for(int i =0; i<num; i++) {                          /*Motor 1 rotates forward*/
                    motor_rotation( motor1_pin, 1200);               /*High level duration is 1200us, which is in 1500us-1000us, motor 1 rotates forward*/
            }
            for(int i =0; i<num; i++) {                          /*Motor 1 stops rotating */
                    motor_rotation( motor1_pin, 1500);               /*High level duration equals to 1500us, motor 1 stops rotating*/
            }
            for(int i =0; i<num; i++) {                          /*Motor 2 rotates backward*/
                    motor_rotation( motor2_pin, 1800);               /*High level duration is 1800us, which is in 1500us-2000us, motor 2 rotates backward*/
            }
            for(int i =0; i<num; i++) {                          /*Motor 2 stops rotating*/
                    motor_rotation( motor2_pin, 1500);               /*High level duration equals to 1500us, motor 2 stops rotating*/
            }
            for(int i =0; i<num; i++) {                          /*Motor 2 rotates forward*/
                    motor_rotation( motor2_pin, 1200);               /*High level duration is 1200us, which is in 1500us-1000us, motor 2 rotates forward*/
            }

            for(int i =0; i<num; i++) {                          /*Motor 2 stops rotating*/
                    motor_rotation( motor2_pin, 1500);               /*High level duration equals to 1500us, motor 2 stops rotating*/
            }

            for(int i =0; i<num; i++) {                          /*Motor 1 and 2 rotate backward*/
                    all_motor_rotation(motor1_pin, motor2_pin, 1800);/*High level duration is 1800us, which is in 1500us-2000us, motor 1 and 2 rotate backward*/
            }

            for(int i =0; i<num; i++) {                          /*Motor 1 and 2 stop rotating*/
                    all_motor_rotation(motor1_pin, motor2_pin, 1500);/*High level duration equals to 1500us, motor 1 and 2 stop rotating*/
            }

            for(int i =0; i<num; i++) {                          /*Motor 1 and 2 rotate forward*/
                    all_motor_rotation(motor1_pin, motor2_pin, 1200);/*High level duration is 1200us, which is in 1500us-1000us, motor 1 and 2 rotate forward*/
            }

            for(int i =0; i<num; i++) {                          /*Motor 1 and 2 stop rotating*/
                    all_motor_rotation(motor1_pin, motor2_pin, 1500);/*High level duration equals to 1500us, motor 1 and 2 stop rotating*/
            }
    }

Was this article helpful?

TOP