small DC motor (you can find these at Radio Shack or you can crack open a child’s toy to find small motors. Make sure the child is done with it first).
Hook up a red wire from the 5V output of the Arduino to one of the power rails on the breadboard
Hook up a black wire from either ground (GND) connection on the Arduino to a ground rail on the breadboard
Hook up power and ground across the breadboard so that the rails on both sides are live.
Add a Potentiometer
Connect the red and black wires from your potentiometer to power and ground on the breadboard.
Attach the remaining middle “output” wire to Arduino analog pin 0.
Add a Motor
Be sure you are using a small motor. It won’t be able to draw more than about 40 mA of current through the Arduino.
Solder hook-up wires onto the two metal contacts on the motor. It’s helpful to use two different colors.
Connect one motor wire to power. Your choice of which wire will affect the direction that the motor spins, but that’s not important right now.
Connect the other motor wire to the center collector pin of the TIP120 transistor.
Also connect the output end of an LED to the same center collector pin of the TIP120. The other input end of the diode gets attached to the right-side emitter pin of the TIP120. This diode will prevent a spinning motor’s “blowback” current from shorting out the circuit.
Connect a 1K ohm resistor to the left-side base pin of the TIP120. The other end of that resistor should be wired to Arduino digital pin 9.
It’s helpful to have some decoupling, to reduce noise in your circuit. So optionally, put a 10µF capacitor between the power and ground rails of your breadboard, near where you are sending current into the motor.
Complete your circuit by connecting the right-side emitter pin of the TIP120 to ground.
Controlling Speed
Upload the following code to translate the analog input into digital output that moves the motor. This code probably looks familiar, it’s pretty much identical to the code you used in the analog labs.
int motorPin = 9; // motor connected to digital pin 9
int analogPin = 0; // potentiometer connected to analog pin 0
int val = 0; // variable to store the read value
void setup()
{
pinMode(motorPin, OUTPUT); // sets the pin as output
}
void loop()
{
val = analogRead(analogPin); // read the input pin
analogWrite(motorPin, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
}
If you want to control a larger motor, you can use the same code, but you’ll need to connect an external power supply directly to the motor’s input to supply the larger current you’ll require to spin a larger motor. Check the Physical Computing book for helpful examples of transistor circuits.
Add a Switch
Now that you’ve had some fun controlling the motor speed, let’s try controlling the direction.
Remove all components except the motor and the wires connecting power between Arduino and the breadboard, and then hook up a regular switch (SPST) to your breadboard.
Connect one wire of the switch to power.
Connect the other wire to a 10 K Ohm resistor going to ground. This is called a “pull down” resistor because it will be pulling the pin to ground when the switch is open rather than letting it “float.” Floating pins aren’t connected to any voltage in your circuit and will pick up static electricity from the air, with unpredictable results. We generally avoid having unpredictable results!
Use hook-up wire to make one last connection from Arduino digital pin 2 to the point where the resistor and the switch are connected. When the switch is open, the pin will be connected to ground via the resistor. When the switch is closed, positive voltage will flow through the switch to the pin. That’s the path of least resistance. So either we’ll be sending power or ground to the pin, but never letting it float.
Add an H-bridge
Inside your H-bridge is actually a collection of transistors that are permanently assembled in such a way as to provide two H-bridge circuits for changing the direction of current flow. Here’s a pin diagram, helpfully annotated by Tom Igoe, who wrote your Physical Computing text:
Fear not, hooking it up is pretty easy, you just need to do it one wire at a time. Note how the pins are numbered, with pin 1 being on the upper left next to the notch in the chip, and the numbers going down the left side and back up the right side until you get to pin 16. This is the standard method of numbering the pins on any microchip.
Using hook-up wire, connect pin 16 to your circuit’s power rail (+5 volts).
Connect pin 8 to the power supply for your motor. For this lab, that’s going to be the same +5 volts, but if you were using a larger motor it would be to the motor’s supply voltage, maybe +9 volts, +12 volts or more.
Pin 1 gets connected to power. This pin enables the motor circuitry. For this project, we just leave it enabled all the time since we have an external microcontroller making all of our logic decisions.
Pins 4, 5, 12 and 13 all can be connected to ground. For small motors actually any one of these pins will do, the extra pins are there just to conduct excess heat away from the chip. That’s important if you are handling a larger load.
Connect pin 2 to Arduino digital pin 3. Setting this pin high will enable the motor to spin in one direction.
Connect pin 7 to Arduino digital pin 7. This will enable the motor to spin in the other direction.
Attach one wire from your motor to pin 3 on the H-bridge.
Attach the other motor wire to pin 6 on the H-bridge.
Attach pin 9 on the H-bridge to Arduino digital pin 9.
Connect an LED to Arduino digital pin 13 and the other end to a 1 K Ohm resistor going to ground.
Controlling Direction
Load the following code to control the motor’s direction with the switch. Note that there’s a function to blink the LED on the board that’s permanently attached to pin 13. You may want to copy that function to use in other code you write.
const int switchPin = 2; // switch input
const int motor1Pin = 3; // H-bridge leg 1 (pin 3, 1A)
const int motor2Pin = 7; // H-bridge leg 2 (pin 7, 2A)
const int enablePin = 9; // H-bridge enable pin
const int ledPin = 13; // LED voidsetup() {
// set the switch as an input:pinMode(switchPin, INPUT);
// set all the other pins you're using as outputs:pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(ledPin, OUTPUT);
// set enablePin high so that motor can turn on:digitalWrite(enablePin, HIGH);
// blink the LED 3 times. This should happen only once.// if you see the LED blink three times, it means that the module// reset itself,. probably because the motor caused a brownout// or a short.blink(ledPin, 3, 100);
}
voidloop() {
// if the switch is high, motor will turn on one direction:if (digitalRead(switchPin) == HIGH) {
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge lowdigitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
}
// if the switch is low, motor will turn in the other direction:else {
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge highdigitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
}
}
/* blinks an LED */voidblink(int whatPin, int howManyTimes, int milliSecs) {
int i = 0;
for ( i = 0; i < howManyTimes; i++) {
digitalWrite(whatPin, HIGH);
delay(milliSecs/2);
digitalWrite(whatPin, LOW);
delay(milliSecs/2);
}
}
Hack a Toy (Optional but educational and potentially creepy)
Many battery-operated children’s toys include motors to create motion. Try finding one and opening it up to see if you can alter the behavior of the existing toy, (or a frightening disembodiment of that toy) by controlling the speed and direction of its motors. Note that many toys use clever mechanisms to convert simple rotary motion into an amazing array of different actions and sounds. There’s a lot that can be done with a simple motor.
Lab-Motors
Parts
Connect power from Arduino to Breadboard
Add a Potentiometer
Add a Motor
Controlling Speed
Upload the following code to translate the analog input into digital output that moves the motor. This code probably looks familiar, it’s pretty much identical to the code you used in the analog labs.
int motorPin = 9; // motor connected to digital pin 9 int analogPin = 0; // potentiometer connected to analog pin 0 int val = 0; // variable to store the read value void setup() { pinMode(motorPin, OUTPUT); // sets the pin as output } void loop() { val = analogRead(analogPin); // read the input pin analogWrite(motorPin, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255 }If you want to control a larger motor, you can use the same code, but you’ll need to connect an external power supply directly to the motor’s input to supply the larger current you’ll require to spin a larger motor. Check the Physical Computing book for helpful examples of transistor circuits.
Add a Switch
Now that you’ve had some fun controlling the motor speed, let’s try controlling the direction.
Add an H-bridge
Inside your H-bridge is actually a collection of transistors that are permanently assembled in such a way as to provide two H-bridge circuits for changing the direction of current flow. Here’s a pin diagram, helpfully annotated by Tom Igoe, who wrote your Physical Computing text:
Fear not, hooking it up is pretty easy, you just need to do it one wire at a time. Note how the pins are numbered, with pin 1 being on the upper left next to the notch in the chip, and the numbers going down the left side and back up the right side until you get to pin 16. This is the standard method of numbering the pins on any microchip.
Controlling Direction
Load the following code to control the motor’s direction with the switch. Note that there’s a function to blink the LED on the board that’s permanently attached to pin 13. You may want to copy that function to use in other code you write.
Hack a Toy (Optional but educational and potentially creepy)
Many battery-operated children’s toys include motors to create motion. Try finding one and opening it up to see if you can alter the behavior of the existing toy, (or a frightening disembodiment of that toy) by controlling the speed and direction of its motors. Note that many toys use clever mechanisms to convert simple rotary motion into an amazing array of different actions and sounds. There’s a lot that can be done with a simple motor.