Example Code for Arduino-Speed Control

Last revision 2026/01/13

Explore how to use Arduino to control module speed with detailed example code and guidance for better performance in your electronic projects.

Hardware Preparation

Software Preparation

Wiring Diagram

DFR0411 Gravity: 130 DC Motor Module Connection Diagram
UNO R3 PIN Motor PIN
UNO R3 Digital pin 3 Motor Digital pin(GREEN)
UNO R3 VCC Motor VCC(RED)
UNO R3 GND Motor GND(BLACK)

Sample Code

Copy the sample code, and paste it in the Arduino IDE

 /* 130 DC Motor
     by DFRobot <https:www.dfrobot.com>

    */

    int motorPin = 3;   //Motor drive pin D3
    int motorSpeed;     //Define motor speed

    void setup()
    {
      Serial.begin(9600);
    }

    void loop()
    {

      for(motorSpeed = 0 ; motorSpeed <= 255; motorSpeed+=5)
      {
        analogWrite(motorPin, motorSpeed);   //PWM speed control
        delay(30);
      }
      for(motorSpeed = 255 ; motorSpeed >= 0; motorSpeed-=5)
      {
        analogWrite(motorPin, motorSpeed);   //PWM speed control
        delay(30);
      }
    }

Result

The motor starts to accelerate, and after reaching the maximum speed, it begins to decelerate.

Was this article helpful?

TOP