Saturday 16 February 2013

Mid semester break

Although it is semester break, I still attempting to test the pulse-width modulation (PWM) function of Arduino. This part is needed to move the fan with a variable speed in my project. After I search the internet, I found the simple code to test PWM with a DC motor. The DC motor speed can be manipulated with the variable resistor. This is the code:


int motor = 9;
int potenciometer = 5;

void setup(){
  pinMode(9,OUTPUT);
  pinMode(5,INPUT);

  Serial.begin(9600);
}

void loop(){
  int value = analogRead(potenciometer);
                     //read input value: range between (0,1023)
  int motor_speed = value/4;
                    //PWM can only ouput 255 different values

  analogWrite(motor,motor_speed);

  Serial.println(motor_speed);//for testing purposes
}


This is the connection I use to construct on breadboard.

Fortunately, the code works when I tested on breadboard and the results were true as what was expected.
This is before I tune the variable resistor. As you can see, the motor doesn't move.

In this picture, the motor move after I tune the variable resistor. If I tune it all the way, the motor will move to its maximum speed.

For this testing, I used the components DC motor 3V, Variable Resistor 10kΩ, Transistor TIP120, Diode 1N4001, resistor 330Ω and the Arduino microcontroller.

No comments:

Post a Comment