Make It Better

Controlling Speed

We played with the Metro's ability to control the brightness of an LED earlier. Now, we will use the same feature to control the speed of our motor.

The Metro does this using something called Pulse Width Modulation (PWM). This relies on the METRO’s ability to operate really, really fast. Rather than directly controlling the voltage coming from the pin, the Metro will switch the pin on and off very quickly.

In the computer world this is going from 0 to 5 volts many times a second, but in the human world we see it as a voltage. For example: if the Metro is PWM'ing at 50%, we see the light dimmed 50% because our eyes are not quick enough to see it flashing on and off. The same feature works with transistors. (if you want a visual explanation of this concept, click here)

Don't believe me? Try it out!

Copy and paste the code snippet below into the loop() function of your code:

Download: file
// motorOnThenOff();
motorOnThenOffWithSpeed();
// motorAcceleration();

Then Upload the program.

You can change the speeds by changing variables onSpeed and offSpeed to any number between 0 (stop the motor) and 255 (full power!)

Accelerating and Decelerating

Why stop at two speeds? Why not accelerate and decelerate the motor.

To do this simply change the loop() code to read:

Download: file
// motorOnThenOff();
// motorOnThenOffWithSpeed();
motorAcceleration();

Then Upload the program and watch as your motor slowly accelerates up to full speed then slows down again.

If you would like to change the speed of acceleration, change the variable delayTime (larger means a longer acceleration time) to a different value.

This guide was first published on Aug 18, 2017. It was last updated on Aug 18, 2017. This page (Make It Better) was last updated on Apr 08, 2020.