Posted on

Interfacing DC Motor with PIC Microcontroller

Interfacing DC Motor with PIC Microcontroller is the second tutorial in a series of tutorials on Design Embedded System with PIC Microcontroller.

In case you are new to PIC Microcontroller and MPLAB X IDE I strongly suggest to visit the tutorial – Getting started with MPLAB X IDE. If this is your first interfacing with PIC microcontroller please go through Interfacing LED with PIC Microcontroller tutorial for a complete overview of the programming and execution process.

For interfacing DC Motor with PIC Microcontroller we will require the following components which can be purchased in OpzLab web store. Links to the individual components are also provided.

  1. PICKIT3 Debugger / Programmer
  2. PIC16F15325
  3. Mini Breadboard
  4. Power Supply
  5. DC Motors
  6. L298N Motor Driver
  7. Jumper Wires

Follow the below tutorial to run the DC Motors in forward, reverse, left and right direction for every 10 seconds from MPLAB X IDE

Start MPLAB X IDE and Create new Project

Open MPLAB Code Configurator and after initial MCLRE and Low Voltage configuration setup the digital output ports.

Save and generate the headers and source files. Update the source file main.c with below code.

#include "mcc_generated_files/mcc.h"

#define MOTOR_L_R RA2   // Left Motor Red Terminal
#define MOTOR_L_B RC0   // Left Motor Black Terminal
#define MOTOR_R_R RC1   // Right Motor Red Terminal
#define MOTOR_R_B RC2   // Right Motor Black Terminal

void forward() {
    MOTOR_L_R = 1;
    MOTOR_L_B = 0;
    MOTOR_R_R = 1;
    MOTOR_R_B = 0;
}

void reverse() {
    MOTOR_L_R = 0;
    MOTOR_L_B = 1;
    MOTOR_R_R = 0;
    MOTOR_R_B = 1;
}

void left() {
    MOTOR_L_R = 0;
    MOTOR_L_B = 0;
    MOTOR_R_R = 1;
    MOTOR_R_B = 0;
}

void right() {
    MOTOR_L_R = 1;
    MOTOR_L_B = 0;
    MOTOR_R_R = 0;
    MOTOR_R_B = 0;
}

void stop() {
    MOTOR_L_R = 0;
    MOTOR_L_B = 0;
    MOTOR_R_R = 0;
    MOTOR_R_B = 0;
}

void main(void) {
    SYSTEM_Initialize();
    while (1) {
        forward();
        __delay_ms(10000);
        reverse();
        __delay_ms(10000);
        left();
        __delay_ms(10000);
        right();
        __delay_ms(10000);
        stop();
        __delay_ms(20000);
    }
}

Connect the circuit as shown below.

Hit the Download button to program and run the device from MPLAB X IDE. The pic microcontroller will be programmed after few seconds, the program will run the dc motors in forward, reverse, left and right directions per 10 seconds each.

Posted on

Interfacing LED with PIC Microcontroller

Interfacing LED with PIC Microcontroller is the very first tutorial in a series of tutorials on Design Embedded System with PIC Microcontroller.

In case you are new to PIC Microcontroller and MPLAB X IDE I strongly suggest to visit the tutorial – Getting started with MPLAB X IDE.

For interfacing LED with PIC Microcontroller we will require the following components which can be purchased in OpzLab web store. Links to the individual components are also provided.

  1. PICKIT3 Debugger / Programmer
  2. PIC16F15325
  3. Mini Breadboard
  4. Power Supply
  5. 470 ohms Resistor
  6. LED
  7. Jumper Wires

Follow the below tutorial to blink LED from MPLAB X IDE

Start MPLAB X IDE

Create New Project

Select PIC microcontroller device.

Select PICKIT 3 debugger.

Select XC8 compiler.

Provide project name and finish.

Open MPLAB Code Configurator.

Save Code Configurator settings.

Uncheck Low-voltage Programming Enable under Project Resource -> System Module -> Easy Setup -> Programming.

Set MCLRE to port defined function under Project Resource – > System Module – > Registers -> Register Config 2.

Set Pin 8 to GPIO Output.

Disable Analog I/O on pin 8 from Project Resource -> Pin Module.

Click Generate button to generate MCC_Project files and main.c source file.

Click on MCC Button to close the MCC editor.

Open main.c from Project Window.

Update main.c to match the below code.

#include "mcc_generated_files/mcc.h"

#define LED RC2

void main(void)
{
    SYSTEM_Initialize();

    while (1)
    {
        LED = 0;
        __delay_ms(1000);
        LED = 1;
        __delay_ms(1000);
    }
}

Circuit – Interfacing LED with PIC16F15325

Connect PICKIT3 to the PIC16F15325 as shown below

Hit the Download button to program and run the device from MPLAB X IDE.

Select Do not show this message again and press OK button.

The pic microcontroller will be programmed after few downloads, the program will start running and blink the LED.

Posted on

Getting started with MPLAB X IDE

  1. Download and install MPLAB X IDE latest version from Microchip site. User Guide (online).
  2. Download and install MPLAB XC8 compiler latest version from Microchip site. You will get the download links under download tab in the page.
  3. Start MAPLAB X IDE and navigate to plugins. Search for MPLAB Code Configurator and install the plugin. For help, visit Microchip video tutorial.
  4. Connect PICKIT3 Programmer/Debugger to PC / Laptop usb port and restart MPLAB X IDE. PICKit3 documentation can be downloaded from Microchip site. Poster link.
  5. You are ready for your first project. Happy Learning!!