Motor Control

2. Encoder (Sep 22)

An Arduino script was written to measure the shaft position of a DC motor. The motor shaft was rotated by hand to check whether the motion was reflected in the output. An interrupt function continuously monitored the encoder’s position.

Code
void setup() {
  pinMode(2, INPUT);
  pinMode(3, INPUT);

  attachInterrupt(digitalPinToInterrupt(2),myFunction,RISING);

  Serial.begin(9600);
}

volatile long int counter = 0;

void loop() {
  Serial.println(counter);
}

void myFunction(){
  if (digitalRead(3) == HIGH){
    counter++;
  }else{
    counter--;
  }
}

1. Basic Motor Control (Sep 17)

An Arduino script was written to mimic the motion of DC motor according to the given duty cycle diagram.