Posted on

Interfacing Ultrasonic Sensor HY-SRF05 with PIC Microcontroller

Interfacing Ultrasonic Sensor HY-SRF05 with PIC Microcontroller is the fourth tutorial in a series of tutorials on Design Embedded System with PIC Microcontroller. In this tutorial we will measure the distance of an object using ultrasonic sensor and transmit it using Bluetooth.

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 Bluetooth 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. HC-05 Bluetooth Modules
  6. HY-SRF05 Ultrasonic Sensor
  7. Buzzer
  8. Jumper Wires

Follow the below tutorial to measure the distance of an object using ultrasonic sensor and transmit it using Bluetooth.

Start MPLAB X IDE, create a new project with the device settings as mentioned in the tutorial Interfacing LED with PIC Microcontroller.

After the project is created open MPLAB Code Configurator and proceed with the below settings.

Follow the tutorial Interfacing Bluetooth Module HC05 with PIC Microcontroller to setup UART module.

Circuit

Posted on

Interfacing Bluetooth Module HC05 with PIC Microcontroller

Interfacing Bluetooth with PIC Microcontroller is the third tutorial in a series of tutorials on Design Embedded System with PIC Microcontroller. In this tutorial we will control two DC Motors and a buzzer connected to the PIC microcontroller from our android phone via bluetooth.

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 Bluetooth 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. HC-05 Bluetooth Modules
  8. Buzzer
  9. Jumper Wires

Follow the below tutorial to control the DC Motors in forward, reverse, left and right direction and blow the buzzer horn using your android phone using Bluetooth.

Start MPLAB X IDE, create a new project with the device settings as mentioned in the tutorial Interfacing LED with PIC Microcontroller.

After the project is created open MPLAB Code Configurator and proceed with the below settings.

Do Not disable Low Voltage Mode and Do Not change MCLR settings. Let it be default.

Change the HF Internal Clock to 8 MHz and Clock Divider to 1 under Project Resource -> System -> System Module -> Easy Setup.

Assign the Output Ports for the motor controls and buzzer and disable analog for the ports under Project Resources -> System -> Pin Module.

Double Click on Device Resources -> EUSART -> EUSART1 to select UART1 module for bluetooth serial communication.

Enable Receiver and Transmitter under Project Resources -> Peripherals -> EUSART1. Keep all other default settings make sure the Baud Rate is set to 9600.

Change the Rx and Tx ports to pin number 2 and 3 respectively. Make sure to remove Rx1 and Tx1 assignments on any other pins.

Disable Analog on all the port pins.

Save and and click on Generate to create Header and Source Files. Close MPLAB Code Configurator.

Update the source file main.c with below code.

#include "mcc_generated_files/mcc.h"

#define BUZZER RC2
#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 RC3   // 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) {
    char ch = '0';
    SYSTEM_Initialize();
    BUZZER = 1;
    while (1) {
        ch = EUSART1_Read();
        if (ch == 'b') {
            BUZZER = 0;
            __delay_ms(100);
            BUZZER = 1;
            __delay_ms(100);
            ch = '0';
        }
        if (ch == 'f') {
            stop();
            __delay_ms(1000);
            forward();
        } else if (ch == 'r') {
            stop();
            __delay_ms(1000);
            reverse();
        } else if (ch == 'l') {
            stop();
            __delay_ms(1000);
            left();
        } else if (ch == 'w') {
            stop();
            __delay_ms(1000);
            right();
        } else if (ch == 's') {
            stop();
        }
    }
}

Circuit diagram

Hit the Download button to program and run the device from MPLAB X IDE. The pic microcontroller will be programmed after few seconds.

The red light on the Bluetooth module will blink rapidly. It is in discover mode. Scan for HC-05 device in you android phone and pair it with pass code 1234.

Install BlueTooth Serial Controller from Google Play in you Android phone.

Search for the paired HC-05 device and connect. The red light on the Bluetooth module will now blink less frequently. A serial communication link has been now established between your android phone and the PIC microcontroller.

Send the below commands to interact with the motors and buzzer.

You can also configure buttons in the android app to control the motors and the buzzer. Good Luck!

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!!