Introduction
A digital voltmeter is a useful tool in any electronics project, allowing you to measure the voltage of a given circuit accurately. In this project, we will build a simple yet effective digital voltmeter using a PIC microcontroller. This project will involve using the Analog-to-Digital Converter (ADC) module of the PIC microcontroller to convert the analog voltage signal into a digital value, which will then be displayed on a 16x2 LCD.
System Overview
The digital voltmeter will measure DC voltage within a specified range (e.g., 0 to 5V). The PIC microcontroller’s ADC module will be used to sample the input voltage. The ADC value will be converted into a corresponding voltage, which will then be displayed on the LCD screen. The circuit can be extended to measure higher voltages using a voltage divider circuit.
Components Required
PIC Microcontroller (e.g., PIC16F877A) - The central controller for processing the ADC data and interfacing with the LCD.
16x2 LCD Display - Displays the measured voltage.
Voltage Divider Circuit - Reduces the input voltage if it exceeds the ADC input range.
Resistors and Capacitors - For setting up the voltage divider and filtering.
Potentiometer - To adjust the LCD contrast.
Breadboard and Connecting Wires - For assembling the circuit.
Power Supply - To power the system.
Working Principle
The PIC microcontroller’s ADC module converts the input analog voltage into a digital value. This digital value is then processed to calculate the corresponding voltage level. The calculated voltage is displayed on the 16x2 LCD screen. The input voltage should be within the ADC’s input range, typically 0 to 5V, but higher voltages can be measured using a voltage divider circuit.
Step-by-Step Implementation
Step 1: Setting Up the Development Environment
Before starting with the hardware, ensure you have the necessary software tools installed:
MPLAB X IDE - Integrated Development Environment for writing and debugging code for PIC microcontrollers.
XC8 Compiler - A compiler for converting C code into a hex file that the PIC microcontroller can understand.
Step 2: Writing the Code
Start by initializing the PIC microcontroller and setting up the ADC module.
Code Outline:
#include <xc.h>
#include "lcd.h" // Include LCD library
#define _XTAL_FREQ 20000000 // Define the crystal frequency (20MHz)
#define VREF 5.0 // Reference voltage for ADC (5V)
#define RESOLUTION 1024.0 // ADC resolution (10-bit ADC)
// Configuration bits for PIC16F877A
#pragma config FOSC = HS // High-Speed Oscillator
#pragma config WDTE = OFF // Watchdog Timer Disabled
#pragma config PWRTE = OFF // Power-up Timer Disabled
#pragma config BOREN = ON // Brown-out Reset Enabled
#pragma config LVP = OFF // Low Voltage Programming Disabled
#pragma config CPD = OFF // Data EEPROM Memory Code Protection Disabled
#pragma config WRT = OFF // Flash Program Memory Write Enable
#pragma config CP = OFF // Flash Program Memory Code Protection Disabled
void ADC_Init(void) {
ADCON0 = 0x41; // ADCON0: ADC enabled, Channel 0 selected (AN0)
ADCON1 = 0x80; // ADCON1: Right justified result, Vref+ = VDD, Vref- = VSS
}
unsigned int ADC_Read(unsigned char channel) {
ADCON0 &= 0xC5; // Clearing channel selection bits
ADCON0 |= channel << 3; // Selecting channel
__delay_ms(2); // Acquisition time
GO_nDONE = 1; // Start ADC conversion
while (GO_nDONE); // Wait for conversion to complete
return ((ADRESH << 8) + ADRESL); // Return result
}
void main(void) {
float voltage;
unsigned int adc_value;
// Initialize LCD and ADC
Lcd_Init();
Lcd_Clear();
ADC_Init();
while(1) {
adc_value = ADC_Read(0); // Read from channel 0 (AN0)
voltage = (adc_value * VREF) / RESOLUTION; // Convert ADC value to voltage
// Display voltage on LCD
Lcd_Set_Cursor(1, 1);
Lcd_Write_String("Voltage: ");
Lcd_Write_Float(voltage); // Function to display float values
Lcd_Write_String(" V");
__delay_ms(500); // Update every 500 ms
}
}
Step 3: Interfacing the ADC and Voltage Divider
Analog Input (AN0): Connect the analog input voltage to the AN0 pin (RA0) of the PIC microcontroller.
Voltage Divider (optional): If the input voltage exceeds the ADC’s input range, use a voltage divider circuit to step down the voltage to a measurable level. For example, using two resistors in series, the output voltage can be measured across one of the resistors.
Step 4: Interfacing the LCD
Connect the LCD data pins (D4-D7) to the microcontroller’s PORTD.
Connect the RS, RW, and E control pins to the appropriate digital I/O pins on the PIC (e.g., RD0, RD1, and RD2).
Use a potentiometer to adjust the contrast of the LCD.
Step 5: Compiling and Uploading the Code
Write the code in MPLAB X IDE and compile it using the XC8 compiler to generate a hex file.
Upload the hex file to the PIC microcontroller using a programmer (e.g., PICkit 3).
Step 6: Testing the System
Power up the system and connect a known voltage source to the input. The measured voltage should be displayed on the LCD. Verify the accuracy of the readings by comparing them with a standard multimeter. If using a voltage divider, ensure the scaling factor is correctly applied in the code.
Applications
Electronics Testing: Measure voltage in various circuits during development or troubleshooting.
Battery Monitoring: Track battery voltage in embedded systems.
Educational Tools: Use this project as a practical demonstration in electronics and microcontroller courses.
Conclusion
Building a digital voltmeter using a PIC microcontroller is a great way to learn about ADCs, LCD interfacing, and basic voltage measurement techniques. This project can be extended to measure AC voltages, higher voltage ranges, or even multiple channels. It’s a versatile tool that can be adapted to many applications in electronics and embedded systems.
Want us to guide you through your project or make the project for you ?
Create Various Projects
Check out our Free Arduino Projects Playlist - Arduino Projects
Check out our Free Raspberry Pi Projects Playlist - Raspberry Pi Projects
Check out our Free TinkerCAD Projects Playlist - TinkerCAD Projects
Check out our Free IoT Projects Playlist - IoT Projects
Check out our Free Home Automation Projects Playlist - Home Automation Projects
Check out our Free NodeMCu Projects Playlist - NodeMCu Projects
Comments