Ad Code

Interfacing of DC motor with PIC18 controller

interfacing DC motro intro page

DC motors are rotary motors that convert electrical signals into mechanical motion.

DC motors are also one of the most useful peripheral devices in the embedded domain, we can use the DC motors from our academic projects to toys and gadgets sold in the market.

In this article, we will talk about the interfacing of DC motor to the PIC18F4520 controller.

We will cover the topics in the following ways:-

  • About the Task
  • Required components
  • software tools
  • circuit diagram
  • writing code for the controller
  • uploading the hex file to the controller
  • Simulating the project

About the project

In this interfacing task, we will control the rotation of the DC motor with a push button. When the push button is pressed the motor will rotate in a clockwise direction otherwise it will rotate in an antilock wise direction.

Required components

For the simulation, the components which are required are:-

Software tools

The software tools required for this project are
The circuit diagram for the interfacing is given below

interfacing DC motor circuit diagram

In the circuit diagram you can see, the L293D motor driver is connected to the C0 and C1 pin of the PORTC and the push button is connected to the B0 pin of the PORTB. The DC motor is connected to the output pin of the motor driver.

Writing code for the controller

After connecting all the circuit components, now open your MPlab IDE and write the code given below.

code

#include <xc.h>

#define _XTAL_FREQ 20000000

#define button RB0
#define relay RC0

void main()
{
    TRISB0=1;
    TRISC0=0;
    
    if(button==1)
    {
        relay=1;
    }
    else
    {
        relay=0;
    }
}

After writing the code all you need is to run, execute and generate the HEX file.

Upload Hex file to the controller

To upload the generated Hex file all you need is to double click on the controller and select the program file.
Don't forget to change the clock frequency from 4 to 20 Mhz.

how to upload HEX file to controller

Simulate the project

To simulate the project all you need is to click on the play button on the bottom left corner of the simulator.

Video