SubariantoIRFAN ARIANSYAHFerdy DestianZukri juandaAditya putra pradanaZaid HabibullahRendy Tangkas SunyataMuhamad Harits
Published © GPL3+

DC Motor Speed Control using PWM Signal on STM32F401CB

Speed Motor Controller

BeginnerShowcase (no instructions)23 hours267
DC Motor Speed Control using PWM Signal on STM32F401CB

Things used in this project

Hardware components

DC motor (generic)
×1
Dual H-Bridge motor drivers L293D
Texas Instruments Dual H-Bridge motor drivers L293D
×1
Battery, 12 V
Battery, 12 V
×1
STM32F401CB
×1

Software apps and online services

Proteus 8 Profesional
Keil uvision5

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Flux, Soldering
Solder Flux, Soldering
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Mastech MS8217 Autorange Digital Multimeter
Digilent Mastech MS8217 Autorange Digital Multimeter
Plier, Needle Nose
Plier, Needle Nose

Story

Read more

Schematics

Schematic Wiring In Proteus 8

Code

PWM Signal Code In Keil uvision5

C/C++
#include "stm32f4xx.h"
#include <stdint.h>

// Definisi pin motor
#define IN1_PIN     0   // PA0
#define IN2_PIN     1   // PA1
#define EN1_PIN     8   // PA8

#define DELAY_1S    400000U  // delay dasar (disesuaikan dengan clock)

void delay(volatile uint32_t time) {
    while (time--);
}

void GPIO_Init(void) {
    // Aktifkan clock untuk GPIOA
    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;

    // Atur PA0, PA1, PA8 sebagai output (MODER = 01)
    GPIOA->MODER |= (1 << (IN1_PIN * 2)) | (1 << (IN2_PIN * 2)) | (1 << (EN1_PIN * 2));
    // (clear pull-up/down, speed default, type default)
}

void Motor_Forward(void) {
    // IN1 = 1, IN2 = 0 ? motor maju
    GPIOA->ODR |= (1 << IN1_PIN);
    GPIOA->ODR &= ~(1 << IN2_PIN);
}

void Motor_Backward(void) {
    // IN1 = 0, IN2 = 1 ? motor mundur
    GPIOA->ODR &= ~(1 << IN1_PIN);
    GPIOA->ODR |= (1 << IN2_PIN);
}

void Motor_Stop(void) {
    // IN1 = 0, IN2 = 0 ? motor berhenti
    GPIOA->ODR &= ~((1 << IN1_PIN) | (1 << IN2_PIN));
}

void Motor_Enable(void) {
    // EN1 = 1 ? aktifkan driver motor
    GPIOA->ODR |= (1 << EN1_PIN);
}

void Motor_Disable(void) {
    // EN1 = 0 ? nonaktifkan motor
    GPIOA->ODR &= ~(1 << EN1_PIN);
}

int main(void) {
    GPIO_Init();

    while (1) {
        Motor_Enable();

        Motor_Forward();
        delay(3 * DELAY_1S); // Jalan maju 3 detik

        Motor_Stop();
        delay(1 * DELAY_1S); // Berhenti 1 detik

        Motor_Backward();
        delay(3 * DELAY_1S); // Jalan mundur 3 detik

        Motor_Stop();
        delay(2 * DELAY_1S); // Berhenti 2 detik
    }
}

Credits

Subarianto
1 project • 3 followers
IRFAN ARIANSYAH
1 project • 4 followers
Ferdy Destian
1 project • 3 followers
Electrical Engineering
Zukri juanda
1 project • 4 followers
Aditya putra pradana
1 project • 4 followers
Zaid Habibullah
1 project • 3 followers
Rendy Tangkas Sunyata
1 project • 4 followers
Muhamad Harits
1 project • 3 followers

Comments