Components and supplies
1
DC motor (generic)
1
Rotary potentiometer (generic)
2
LED (generic)
1
Pushbutton switch 12mm
1
Breadboard (generic)
1
General Purpose Transistor NPN
2
Resistor 1k ohm
1
Arduino UNO
2
Resistor 221 ohm
1
Capacitor 100 µF
1
Elegoo Starter Kit
1
1N4007 – High Voltage, High Current Rated Diode
Apps and platforms
1
Arduino IDE
Project description
Code
Motor_Control
arduino
1//Control motor with PWM 2 3#define MOTOR 13 4#define LED_ON 5 5#define LED_OFF 4 6 7//Button variables 8int button_state = 0; 9int last_button_state = 1; 10 11//State of motor 12bool motor_state = false; //->0 Motor off -> 1 Motor ON 13double cont = 0; 14 15//frequency of PWM: 5kHz 16int timer = 0; 17 18//Other variables 19int percent = 0; 20int pot_value = 0; 21 22//start variables 23bool start = false; 24 25 26 27void setup() { 28 pinMode(MOTOR, OUTPUT); 29 digitalWrite(MOTOR, LOW); 30 pinMode(LED_ON, OUTPUT); 31 pinMode(LED_OFF, OUTPUT); 32 pinMode(3, INPUT); 33 34 //**SET TIMER0** 35 TCCR0A=(1<<WGM01); //Set the CTC mode 36 OCR0A = 0x04; //Value for ORC0A for 2us 37 38 TIMSK0 |= (1 << OCIE0A); //Set the interrupt request 39 TCCR0B |= (1 << CS01) | (0 << CS00); //Set the prescale 1/8 clock 40 //Now timer is on 41 42 //**SET TIMER0** 43 44 digitalWrite(LED_OFF, HIGH); 45 digitalWrite(LED_ON, LOW); 46} 47 48void loop() { 49 50 pot_value = analogRead(A0); 51 52 //The value doesn't start from 0 to 100, but from 30 to 100 because with a duty cycle < 30% the motor can't turn 53 percent = map(pot_value, 0, 1024, 30, 100); 54 55 //**This routine controls if the state of the push button is changes 56 button_state = digitalRead(3); 57 58 if (button_state == LOW && last_button_state == HIGH) { 59 motor_state = !motor_state; 60 } 61 last_button_state = button_state; 62 //** 63 64 65 if (motor_state == true ) { 66 //**PWM** 67 if (start == false) 68 { 69 //I must starting the engine once 70 digitalWrite(LED_ON, HIGH); 71 digitalWrite(LED_OFF, LOW); 72 digitalWrite(13, HIGH); 73 //cont is in the ISR() routine so it is like timer variable 74 if (cont > 40000) { 75 start = true; 76 cont = 0; 77 } 78 79 } 80 else { 81 //THIS CREATES A VARAIBLE PWM 82 if (timer <= (percent * 2)) 83 digitalWrite(13, HIGH); 84 else 85 digitalWrite(13, LOW); 86 } 87 //**PWM** 88 } 89 90 //if motor_state==false motor is OFF so 13 must be LOW 91 else { 92 digitalWrite(13, LOW); 93 digitalWrite(LED_ON, LOW); 94 digitalWrite(LED_OFF, HIGH); 95 start = false; 96 } 97} 98 99 100//This routine control the pwm but the sum of high time and low time must be ALWAYS 200us -> 5kHz 101ISR(TIMER0_COMPA_vect) { //This is the interrupt request 102 103 timer++; 104 if (start == false && motor_state == true)cont++; 105 106 //After 200us ( timer == 200) -> timer = 0 107 if (timer >= 200) timer = 0; 108 109} 110 111 112
Motor_Control
arduino
1//Control motor with PWM 2 3#define MOTOR 13 4#define LED_ON 5 5#define LED_OFF 4 6 7//Button variables 8int button_state = 0; 9int last_button_state = 1; 10 11//State of motor 12bool motor_state = false; //->0 Motor off -> 1 Motor ON 13double cont = 0; 14 15//frequency of PWM: 5kHz 16int timer = 0; 17 18//Other variables 19int percent = 0; 20int pot_value = 0; 21 22//start variables 23bool start = false; 24 25 26 27void setup() { 28 pinMode(MOTOR, OUTPUT); 29 digitalWrite(MOTOR, LOW); 30 pinMode(LED_ON, OUTPUT); 31 pinMode(LED_OFF, OUTPUT); 32 pinMode(3, INPUT); 33 34 //**SET TIMER0** 35 TCCR0A=(1<<WGM01); //Set the CTC mode 36 OCR0A = 0x04; //Value for ORC0A for 2us 37 38 TIMSK0 |= (1 << OCIE0A); //Set the interrupt request 39 TCCR0B |= (1 << CS01) | (0 << CS00); //Set the prescale 1/8 clock 40 //Now timer is on 41 42 //**SET TIMER0** 43 44 digitalWrite(LED_OFF, HIGH); 45 digitalWrite(LED_ON, LOW); 46} 47 48void loop() { 49 50 pot_value = analogRead(A0); 51 52 //The value doesn't start from 0 to 100, but from 30 to 100 because with a duty cycle < 30% the motor can't turn 53 percent = map(pot_value, 0, 1024, 30, 100); 54 55 //**This routine controls if the state of the push button is changes 56 button_state = digitalRead(3); 57 58 if (button_state == LOW && last_button_state == HIGH) { 59 motor_state = !motor_state; 60 } 61 last_button_state = button_state; 62 //** 63 64 65 if (motor_state == true ) { 66 //**PWM** 67 if (start == false) 68 { 69 //I must starting the engine once 70 digitalWrite(LED_ON, HIGH); 71 digitalWrite(LED_OFF, LOW); 72 digitalWrite(13, HIGH); 73 //cont is in the ISR() routine so it is like timer variable 74 if (cont > 40000) { 75 start = true; 76 cont = 0; 77 } 78 79 } 80 else { 81 //THIS CREATES A VARAIBLE PWM 82 if (timer <= (percent * 2)) 83 digitalWrite(13, HIGH); 84 else 85 digitalWrite(13, LOW); 86 } 87 //**PWM** 88 } 89 90 //if motor_state==false motor is OFF so 13 must be LOW 91 else { 92 digitalWrite(13, LOW); 93 digitalWrite(LED_ON, LOW); 94 digitalWrite(LED_OFF, HIGH); 95 start = false; 96 } 97} 98 99 100//This routine control the pwm but the sum of high time and low time must be ALWAYS 200us -> 5kHz 101ISR(TIMER0_COMPA_vect) { //This is the interrupt request 102 103 timer++; 104 if (start == false && motor_state == true)cont++; 105 106 //After 200us ( timer == 200) -> timer = 0 107 if (timer >= 200) timer = 0; 108 109} 110 111 112
Downloadable files
layout_motor_speed_control_tY0ycJvpYw.fzz
layout_motor_speed_control_tY0ycJvpYw.fzz
Schematic
Schematic

layout_motor_speed_control_tY0ycJvpYw.fzz
layout_motor_speed_control_tY0ycJvpYw.fzz
Comments
Only logged in users can leave comments