DC motor control

By

// global variables

const int motorPin=9; // controls transistor (attached to gate)

const int switchPin=2; // reads manual switch

int switchState=0;

// the motor draws current from the 9V battery, not the Arduino board

// the Arduino board controls (opens/ closes) the motor circuit via the transistor

void setup() {

  pinMode(motorPin, OUTPUT);

  pinMode(switchPin, INPUT);

  Serial.begin(9600); // to print stuff on the serial monitor

}

void loop() {

  switchState=digitalRead(switchPin);

  if(switchState==1) {

    // manual switch is closed

    digitalWrite(motorPin, HIGH);

    Serial.println(“motor ON”);

    Serial.println(“~~~ ~~~ ~~~”);

  }

  else {

    // manual switch is open

    digitalWrite(motorPin, LOW);

  }

} // nothing gets printed on the serial monitor unless the manual switch is closed

Comments

Leave a comment

Check also

View Archive [ -> ]