Intermediate
...


Learning Objectives

  • Emphasis: Hardware
  • Hardware: DC Motor, Transistor, Diode
  • Software: Run a DC motor; simple state machine

Materials

  • Arduino Uno
  • Breadboard and wires
  • Small DC Motor
  • Diode (1N4001 or similar)
  • Transistor (PN2222 or similar)
  • 330 ohm resistor
  • Pushbutton switches
  • 10kohm resistor
Reference Files

Breadboard Layout

  1. Switch: Let's do the easy part first - hook up one end of the pushbutton switch to power and the other to the 10kohm resistor and ground. Connect a wire from pin 11 to the joint between the button and the resistor.
  2. Transistor: To control the motor, we could just send 5 V straight through. However, DC motors generally draw more power than an Arduino can provide. Therefore, we need to use a transistor to act as a middleman between the Arduino and the motor. The transistor has three pins: from left to right, looking at the flat side, collector, base, and emitter. When a little bit of current is sent to the base, it turns the transistor on and allows a large amount of current to flow from the collector to the emitter. Connect the collector pin of the transistor to one end of the motor and the emitter pin to ground. Connect the base pin through the 330-ohm resistor to pin 9 on the Arduino.
  3. Diode: Turning off the power to the motor could potentially create a reverse current that might damage the transistor or the Arduino. To protect against this, we need to add a diode, which only allows current to flow in one direction. The striped end of the diode is the positive end. Connect this to power and to the unconnected end of the motor. Connect the unstriped end to the other end of the motor and the collector pin on the transistor. Congratulations, you have a safe, functional motor circuit!
  4. Motor: To use your circuit as a wheel of fortune, simply attach a colorful wheel to the spinning end of your DC motor. We used the cardboard wheel from the Game of Life, but you can make your own if you'd like.
  5. You're done! Plug your Arduino into the USB port on your computer.
Reference Schematic
...

...

Software

  1. Getting started: Our program needs to read the button input and use it to decide whether to start, stop, or continue running the motor. Declare variables to store your button and transistor (motor) pins. Also, declare a variable to store the state of the motor - on or off.
  2. Setup: Set the motor to input and the button to output.
  3. Change button state: First read the button input to determine if the motor needs to change state. If the button has been pressed, we need to switch the state of the motor from on to off. We can do this with a single line of code: running = !running. The delay statement debounces the button so it doesn't get read more than once.
  4. Run motor: We plugged our motor into a PWM pin, so it can be run at any speed from 0-255. We chose 150 as a medium value. AnalogWrite your chosen value to the motorPin. You need to multiply your value by the running variable so that the motor won't run if it needs to be turned off. That's it! Run your program and experiment with different speeds. If your wheel doesn't work immediately, try spinning it manually to get the motor started - it tends to get stuck at first.
  5. Experiment: Can you make the motor go backwards? (Hint: Try switching the motor pins.) What about having it change up halfway through the spin? Can you make it choose a random amount of spin time rather than spinning only when the button is pressed? Try using a potentiometer to change spin speed.
Reference Code

The Finished Product




<-- Previous: Mood Sensor Start Advanced Projects!