Introduction to Physical Computing
Week 7, Lab- DC Motor Control



In preparation for the DC motor lab I decied to build one of the
protoshield's sold for the arduino. There isn't any documentation on
Sparkfun's site about assembling it, and while fairly straight forward,
I found this (a href="http-//www.atomicsalad.com/archive/2007/03/11/tutorial_sparkfun_protoshield_assembly_use.php) great tutorial. It took me about 2 hours to put together.
What's a good way to cut that female header? Drove me nuts. Was it
worth the time? Yes, having the breadboard and arduino in that little
unit is sweet.

Protoshield aside, this week's lab was about working with a DC motor.
It took awhile of looking at Tom's diagram, but eventually I wired
things up. Looking at the tech specs for the SN754410 helped.
The chip is surprisingly easy to use once you get the hang of wiring
it up. Vcc1 goes to 5V to power the chip, Vcc2 is to power the motor.

I connected a POT to control the speed of the motor which worked fine.
I had to turn the dial around to get it up to 5V before the motor
started turning. Below is my code, based off of Tom's-


int switchPin = 2;    // switch input
int motor1Pin = 3;    // H-bridge leg 1
int motor2Pin = 4;    // H-bridge leg 2
int speedPin = 9;    // H-bridge enable pin
int ledPin = 13;    //LED
int potPin = 0;        // Analog input pin for the pot
int potValue = 0;    // value read from the pot

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(speedPin, OUTPUT);
    pinMode(ledPin, OUTPUT);

    // set speedPin high so that motor can turn on-
    digitalWrite(speedPin, HIGH);

    Serial.begin(9600);

        // 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() {
    potValue = analogRead(potPin); // read the pot value
    analogWrite(speedPin, potValue/4);
    Serial.println(potValue);        

    if (digitalRead(switchPin) == HIGH) {
        digitalWrite(motor1Pin, LOW);
        digitalWrite(motor2Pin, HIGH);
    }
    else {
        digitalWrite(motor1Pin, HIGH);
        digitalWrite(motor2Pin, LOW);
    }
    delay(10);
}

// 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);
    }
}



-- Wed, 24 Oct 2007 00-57 -0400

Copyright © 1996-2008 Alexander Reeder
All rights reserved unless otherwise noted