Making Things Interactive

February 12, 2008

Controlling a DC motor and Solenoid

Filed under: 5: Making Motion,Tiffany Yang — tyang1 @ 9:17 pm

My project uses a potentiometer to change the speed of the motor.  On a different pin, a solenoid is just opening and closing.       

int MotPin = 9;                                  // sets base of motor transistor to PWM 9
int potPin = 0;                                  // sets potentiometer to analog 0
int potValue = 0;                                // value returned from the potentiometer

int SolPin = 1;                                  // sets base of solenoid transistor to digital 1
int switchPin = 3;                              // sets switch pin to digital 3
int switchStatus;                                // status of the switch

void setup() {
  pinMode(MotPin, OUTPUT);                       // sets motor transistor pin as output
  pinMode(potPin, INPUT);                        // sets potentiometer pin as input
  pinMode(SolPin, OUTPUT);                       // sets solenoid transistor pin as output
  pinMode(switchPin, INPUT);                     // sets switch pin as input
  Serial.begin(9600);
}

void loop() {
  potValue = analogRead(potPin) / 4;             // read potentiometer, convert to 0 - 255
  analogWrite(9, potValue);                      // use potentiometer to control transistor

  switchStatus = digitalRead (switchPin);        // reads status of switch
  if (switchStatus == HIGH) {                    // If switch is pressed...
    digitalWrite(SolPin, HIGH);                  // turn solenoid on
    Serial.println("button pressed");
    delay(100);
  } 
    else {                                       //  otherwise ... 
    digitalWrite(SolPin, LOW);                   // turn solenoid off
    Serial.println("button not pressed");
    delay(100);
  }
}

      

Advertisements

1 Comment »

  1. We’d like to see a circuit schematic, a photo of the project, and perhaps a video of it as well.
    Posting the code alone doesn’t really tell us enough.

    Comment by mdgross — February 13, 2008 @ 7:07 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: