Writing the Code

About: Learn electronics and Arduino with Tinkercad Circuits!

The following information is a single lesson in a larger project. Find more great projects here.

Return to Previous Lesson: Setting Up the Circuit

Lesson Overview:

Now we'll write the pinwheel code!

Step 1: Name Constants and Variables

To begin, set up some constants for the switch and motor pins and a variable named switchState to hold the value of the switch. switchState has an initial value of 0.

  1. Copy the code below into the Code Editor.

    const int switchPin = 2; 
    const int motorPin = 9; 
    int switchState = 0;

  2. Continue to the next step.

Step 2: Declare the Pin Directions

In your setup() function, declare the pinMode() of the motor as an OUTPUT and button as an INPUT.

Note that this is the entire setup() function.

  1. Copy the code below into the Code Editor.

    void setup() { 
    	pinMode(motorPin, OUTPUT); 
    	pinMode(switchPin, INPUT); 

  2. Continue to the next step.

Step 3: Start the Motor

Your loop() function is straightforward. Check the state of the switchPin with digitalRead().

If the switch is pressed, turn the motorPin HIGH. When it's HIGH, the transistor gate will activate, completing the motor circuit. If the button is not pressed, turn the pin LOW. When LOW, the motor will not spin.

  1. Copy the code into the Code Editor.

    void loop() { 
    	switchState = digitalRead(switchPin); 
    	if (switchState == HIGH) { 
    		digitalWrite(motorPin, HIGH); 
    	} else { 
    		digitalWrite(motorPin, LOW); 
    } //end of loop()

  2. This is the entire loop() function, and the end of the program!

  3. Continue to the next step.

Step 4: Use It!

In the circuit simulator, upload and run the code, and try pressing the button. The motor will start up!

If you are conducting the project with your physical kit, plug in your 9V battery first and then power up the Arduino with the USB connection.

You'll learn how to attach this colorful pinwheel in the next lesson!

  1. Upload & Run the code in the simulator, or plug in and upload the code to your Arduino Uno.
  2. Press the button to activate the motor! It will continue spinning for a few seconds after you release the button, then slow down and stop.
  3. Continue to the next step.

Step 5: Think About It...

Transistors are solid state devices, so they have no moving parts. Because of this, you can switch them on and off very quickly. Try hooking up a potentiometer to an analog input and use pulse width modulation (PWM) on pin 9 to rapidly turn the gate on and off.

Alternatively, what do you think will happen to the motor’s speed if you vary the voltage that it’s getting? (See hint.)

  1. Continue to the next step.
  2. Stuck? HINT: Motors have an optimal operating voltage. They will work on as little as 50% of the rated voltage and as much as 50% over that number. If you vary the voltage, you can change the speed at which the motor rotates. Don’t vary it too much, though, or you will burn out your motor!


Step 6: Review

Congratulations on completing your first MOSFET project!

Motors require special consideration when you are controlling them with a microcontroller. Typically the microcontroller cannot provide enough current and/or voltage to power a motor. Because of this, you use transistors to interface between the two. It’s also smart to use diodes to prevent damaging your circuit.

I the next lesson, the project will get a little more fun! Learn how to attach a colored pinwheel to the motor to produce interesting color effects as it spins.

Next Lesson:Assembling the Pinwheel



    • Epilog X Contest

      Epilog X Contest
    • Remix Contest

      Remix Contest
    • Warm and Fuzzy Contest

      Warm and Fuzzy Contest