Vernier Software and Technology
Vernier Software & Technology
Arduino and Vernier Sensors

DC Motor Control with PWM

DC motors are simple electronic components. Apply a voltage across the two leads and the motor spins. Apply a greater voltage and it spins faster. Switch the leads and it spins in the opposite direction.

One limitation with many microcontrollers, like the Arduino, is that it only offers digital lines for output. You can set an output line HIGH (max voltage) or LOW (zero voltage), but you can’t directly output a voltage in between. There is an indirect method however, that allows you to output a voltage some place between max and zero. This process is called pulse-width modulation (PWM). In order to output a voltage midrange between max and zero, a microcontroller can alternate between the HIGH and LOW output settings very quickly. If it switches equally between max and zero, the output voltage is effectively one-half the max. If it is HIGH twice as long as it is LOW, then it is effectively outputting a voltage two-thirds max.

The sample sketch, VernierLibTutorialDCUPWM, uses PWM to gradually speed up and slow down a DC motor. Connect the DC motor to lines D4 and GND on a Vernier DCU. Plug the DCU into the Digital 2 port on the Vernier Arduino Interface Shield or a Digital Protoboard Adapter wired to Arduino pins 6, 7, 8, and 9 as explained in the Connecting Vernier Sensors to the Arduino Using a Breadboard section.

/* VernierLibTutorialDCUPWM (v2018)
 * This sketch uses pulse-width modulation (PWM) to control
 * the speed of a DC motor connected to a Vernier DCU. The 
 * motor will slowly increase its speed from 0 to max and 
 * then slowly decrease its speed from max to 0.
 * 
 * Plug the DCU into the Digital 2 port on the Vernier Arduino 
 * Interface Shield or into a Digital Protoboard Adapter wired 
 * to Arduino pins 6, 7, 8, and 9. Connect the DC motor to DCU
 * lines D4 and GND.
 */

#include "VernierLib.h" //include Vernier functions in this sketch
VernierLib Vernier; //create an instance of the VernierLib library

int PWMSetting; //create global variable for level of PWM

void setup() {
}

void loop() {
    // demononstrate PWM by fade in from 0 to max in increments of 5 points
    for(int PWMSetting = 0 ; PWMSetting <= 255; PWMSetting +=5) 
    { 
      Vernier.DCUPWM(PWMSetting); //turn on motor to PWM level 
      delay(30); //wait 30 milliseconds to see timing effect                           
    } 
     // fade out from max to 0 in increments of 5 points
     for(int PWMSetting = 255 ; PWMSetting >= 0; PWMSetting -=5) 
     { 
       Vernier.DCUPWM(PWMSetting); //turn on motor to PWM level    
       delay(30); //wait 30 milliseconds to see timing effect                           
      } 
     delay(1000); //wait 1 sec before repeating
   }


Contents

Introduction

Vernier Engineering Contest

Vernier recognizes instructors for creative uses of Vernier sensors to introduce engineering concepts and/or practices. Vernier Engineering Award »

Go to top