Difference between revisions of "Lesson 40--Microduino DC Motor Drive (Potentiometer PWM control speed)"
From Microduino Wiki
Line 15: | Line 15: | ||
**Potentiometer one | **Potentiometer one | ||
**NPN transistor one | **NPN transistor one | ||
− | **2.2k | + | **2.2k resistor one |
**Diode one | **Diode one | ||
**USB cable one | **USB cable one | ||
Line 21: | Line 21: | ||
==Schematic== | ==Schematic== | ||
[[File:第四十课-Microduino电位器PWM调速直流电机.jpg|600px|center|thumb]] | [[File:第四十课-Microduino电位器PWM调速直流电机.jpg|600px|center|thumb]] | ||
− | + | Description: | |
When connect the NPN transistor, Base (B) connects with 2.2K resistor, emitter (E) connects the GND and collector (C) connects to motor. As follows: | When connect the NPN transistor, Base (B) connects with 2.2K resistor, emitter (E) connects the GND and collector (C) connects to motor. As follows: | ||
Line 38: | Line 38: | ||
void loop() { | void loop() { | ||
potValue = analogRead(potPin); //Read the value of potentiometer | potValue = analogRead(potPin); //Read the value of potentiometer | ||
− | motorValue = map(potValue, 0, 1023, 0, 255);// | + | motorValue = map(potValue, 0, 1023, 0, 255);//Map to a value among 0~255 |
analogWrite(motorPin, motorValue);//Output PWM to motor | analogWrite(motorPin, motorValue);//Output PWM to motor | ||
//Display | //Display | ||
Line 52: | Line 52: | ||
==Debug== | ==Debug== | ||
− | Step | + | Step 1: Copy the code to IDE and compile it |
− | Step | + | Step 2: Set up circuit, as follows: |
[[File:第四十课-Microduino电位器PWM调速直流电机连接图.jpg|600px|center|thumb]] | [[File:第四十课-Microduino电位器PWM调速直流电机连接图.jpg|600px|center|thumb]] | ||
− | Step | + | Step 3: Run program |
− | Step | + | Step 4: Adjust potentiometer, and check the speed of the motor |
==Result== | ==Result== |
Latest revision as of 08:57, 12 September 2016
Language: | English • 中文 |
---|
ObjectiveThis tutorial will teach you how to control the speed of a DB motor via the potentiometer. Equipment
SchematicDescription: When connect the NPN transistor, Base (B) connects with 2.2K resistor, emitter (E) connects the GND and collector (C) connects to motor. As follows: Programint potPin = A0; //Potentiometer input pin
int motorPin = 9;//Motor pin
int potValue = 0;//Define the value of potentiometer
int motorValue = 0;//Define the value of motor
void setup() {
Serial.begin(9600);
}
void loop() {
potValue = analogRead(potPin); //Read the value of potentiometer
motorValue = map(potValue, 0, 1023, 0, 255);//Map to a value among 0~255
analogWrite(motorPin, motorValue);//Output PWM to motor
//Display
Serial.print("potentiometer = " );
Serial.print(potValue);
Serial.print("\t motor = ");
Serial.println(motorValue);
delay(2);
} DebugStep 1: Copy the code to IDE and compile it Step 2: Set up circuit, as follows: Step 3: Run program Step 4: Adjust potentiometer, and check the speed of the motor ResultTwist potentiometer to adjust the motor's speed Video |