Motor Lab
Blinking lights and making sounds are great, but sometimes you just want to make things move. The Motors lab is a quick exercise in wiring and simple code that I’ve already used once between working through it and posting this write-up.
Basic speed control
The wiring for simple pulse-width modulated control of a motor is pretty basic, though a few additional precautions are necessary. First, rather than connecting directly to an Arduino analog output, we use that output to control a (TIP 120) transistor, allowing more voltage to go to the motor than the Arduino can handle. For the small motor in this example, 5v is more than enough. In addition, a diode is needed to prevent blowback from the motor. The “diodes” in our kit don’t seem to be directional, but using an LED instead seemed to make things work nicely.
As in so many other labs, a potentiometer provides analog input.
The following code is loaded to the Arduino:
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
}
And the results:
Basic Motor Speed Control from Jeff Kirsch on Vimeo.
Controlling direction
Controlling the direction of a motor is as simple as changing the direction of current. That, however, is easier said than done. Basically, you need a set of electronically controlled relays to force current across the motor in one direction, or the other.
Luckily, such a setup exists, in one package. This H-Bridge chip actually allows directional control of two motors. In our setup, a switch is used as a digital input for the Arduino; this is then translated into output to two legs of the H-bridge, causing current to the motor to reverse direction. The wiring is pretty straightforward once you see what’s going on; the results are below.
Directional Motor from Jeff Kirsch on Vimeo.
Code follows:
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
void setup() {
// 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);
}
void loop() {
// 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 low
digitalWrite(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 high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
}
}
/*
blinks an LED
*/
void blink(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);
}
}
Lab instructions available here.
About this entry
You’re currently reading “Motor Lab,” an entry on Jeff Kirsch
- Published:
- 11.4.09 / 1pm
- Category:
- Coursework, Fundamentals of Physical Computing
- Tags:


No comments
Jump to comment form | comments rss [?] | trackback uri [?]