Notes of PhiL    About    Categories    Tags    Archive    Feed    GitHub Repo

Arduino Projects Book - Project 09: Motorized Pinwheel


Welcome back! Today I’ll get the Arduino to spin a colorful pinwheel using a DC motor. In this project, we will use the DC motor for the first time with the Arduino. Working with a DC motor is fun because I can make things move using electronics. However, there are some important details that I think I should take a careful note. Let’s begin with a new component–the transistor.


Transistors are components that allow us to control high current and high voltage from the low current output of microcontrollers, especially Arduino. There are many different kinds of a transistor, but they usually fall into two categories: NPN and PNP transistors.


When we provide voltage to one of the transistor’s pins, called the gate, it closes the circuit between the other two pins, which are the source and the drain. I would say the physical switch and this transistor have a correlation. In other words, transistors are digital switches or “a smarter switch”. Next, let’s take a look at the DC motor.


Motors are the type of inductive device. But, what exactly is induction? Induction is a process by which a changing electrical current in a wire can create a magnetic field around the wire. When we supply voltage to the DC motor, its coil generates a magnetic field. This field causes the shaft to spin around.


Nevertheless, when we turn off the motor, it doesn’t stop immediately due to inertia. A motor can create electricity when it’s spinning, and the current generated will be in the opposite direction of the original current–which could lead to damaging the components.

To solve this problem, we must connect a diode parallel to the DC motor such that the anode (+) points to the original current and the cathode (-), which has a band on it, points to the motor. The diode only allows the current to flow in one direction (from the anode to the cathode but not the other way around), and so we can apply this attribute to prevent the inductive current from damaging the transistor (or more badly, the Arduino).

The concepts in this project are a bit complicated, but the program and the usage are fairly easy.








Virtual breadboard layout by Fritzing:


They are available on my GitHub. Here is my circuit layout:



See below for my code, or view it on my GitHub.

 * Project Name: Arduino Projects Book - Project 09: Motorized Pinwheel
 * File Name: motorized_pinwheel.ino
 * Description: Gets the Arduino to spin a colorful pinwheel using a motor.
 * Author: Phi Luu
 * Location: Portland, Oregon, United States
 * Created: August 13, 2016
 * Updated: June 22, 2017

// Required hardware I/O connections
const byte SWITCH_PIN = 2; // connect switch to 2
const byte MOTOR_PIN  = 9; // connect dc motor to ~9

void setup() {
    pinMode(SWITCH_PIN, INPUT);
    pinMode(MOTOR_PIN,  OUTPUT);

void loop() {
    byte switch_state = digitalRead(SWITCH_PIN);

    // toggle the switch to toggle the motor
    if (switch_state) {
        digitalWrite(MOTOR_PIN, HIGH);
    } else {
        digitalWrite(MOTOR_PIN, LOW);