top of page

Arduino Motor Testing 

During these class demonstration we tinkered around with the Arduino IDE UNO Boards with 3 main objectives: Get use to the Arduino Workflow, Distinguish between Analog & Digital Signals, and Build circuits with 1-bit digital control & Power Signals. The general workflow can be seen below. 

 

image.png

Arduino Work-Flow

01

DC Motor Speed Ramp Control Using PWM

This Arduino program controls the speed of a DC motor using Pulse Width Modulation (PWM) on pin 5. The code gradually increases the PWM output value from 0 to 255, which increases the duty cycle of the signal and causes the motor to slowly accelerate from a stop to its maximum speed. After reaching the maximum value, the program then decreases the PWM value from 255 back down to 0, which gradually reduces the duty cycle and slows the motor down until it stops. A small delay is added between each step to create a smooth ramping effect in both acceleration and deceleration. This continuous loop demonstrates how PWM can be used to precisely control motor speed in embedded systems and electromechanical designs.

image.png

Arduino PWM Motor Speed Control 

Motor Control

DC Motor Control

02

DC Motor Speed Control Using Encoder Feedback

This Arduino program implements a closed-loop speed control system for a DC motor using encoder feedback and Pulse Width Modulation (PWM). An encoder connected to pins 2 and 3 measures the motor’s rotation and speed. Pin 2 is set as an interrupt, which triggers the function myFunction() whenever a rising signal from the encoder is detected. This function updates a pulse count variable, allowing the program to track how much the motor has rotated.

 

The function getSpeed() calculates the motor’s angular displacement and rotational speed by measuring the change in encoder counts over a set time interval. This change is then converted into motor speed in RPM. In the main loop(), the measured motor speed (current_speed) is compared to a target speed. The difference between these values creates an error term, which is multiplied by a proportional gain constant kp. This produces a correction value that adjusts the PWM output (v_out) on pin 5, which controls the motor driver. If the motor is running too slowly, the PWM value increases to speed it up; if it is running too fast, the PWM value decreases to slow it down.

 

This proportional feedback control allows the motor to automatically adjust its speed and maintain the desired RPM, demonstrating a basic example of closed-loop motor control using encoder feedback in an embedded system.

image.png
image.png

Code Snippet 1: Encoder Feedback

Motor Control w/ Encoder

DC Motor Control w/ Encoder 

Code Snippet 2: Encoder Feedback

03

DC Motor Speed Control Using Encoder Feedback

This Arduino program generates a two-way triangular velocity profile over time. Using the millis() function, the program tracks elapsed time and calculates a target velocity value that changes linearly in stages. The velocity increases from 0 to 255, then decreases back to 0, then continues to –255, and finally returns to 0, creating a symmetric triangular motion profile. Each phase lasts about 3 seconds, producing a smooth acceleration and deceleration pattern in both forward and reverse directions. The calculated target value is printed to the Serial Monitor, which can be used as a reference input for motor speed control or motion profile testing in embedded systems.

* This form of motor control program was implemented into our controlled closed loop car project!*

image.png

Arduino Two-Way Motor Velocity Profile

bottom of page