Introduction
In this lesson, we will use two potentiometers and an LCD1602 to make a voltmeter.
Components
– 1 * SunFounder Uno board
– 1 * USB data cable
– 2 * Potentiometer(50k)
– 1 * LCD1602
– Several jumper wires
– 1 * Breadboard
Experimental Principle
One potentiometer is used to adjust the contrast of the LCD1602. And the other is used to divide voltage. When you adjust the potentiometer connected to pin A0 of the SunFounder Uno board, the resistance of the potentiometer will change and the voltage at pin A0 will change accordingly. This voltage change is converted into digital values by A/D converter on the SunFounder Uno board. We can see this change on the serial monitor. Then convert the digital values into voltage with the following formula: the voltage equals the digital value divides by 1024 and then multiplies by 5.0. Finally, display the voltage on the LCD1602.
Experimental Procedures
Step 1: Build the circuit
The schematic diagram
Step 2: Program (Please refer to the example code in LEARN -> Get Tutorials on our website)
Step 3: Compile the code
Step 4: Upload the sketch to the SunFounder Uno board
Now, adjust the potentiometer connected to pin A0, you will see the voltage displayed on the LCD1602 varies accordingly.
Code
/*************************************** * name:Voltmeter * function:adjust the potentiometer connected to pin A0, you will see the voltage displayed on the LCD1602 varies accordingly. ***************************************/ //Email: support@sunfounder.com //Website: www.sunfounder.com#include <LiquidCrystal.h> /****************************************************/ const int analogIn = A0;//potentiometer attach to A0 LiquidCrystal lcd(4, 6, 10, 11, 12, 13);//lcd(RS,E,D4,D5,D6.D7) float val = 0;// define the variable as value=0 /****************************************************/ void setup() { Serial.begin(9600);//Initialize the serial serial lcd.begin(16, 2);// set the position of the characters on the LCD as Line 2, Column 16 lcd.print(“Voltage Value:”);//print “Voltage Value:” } /****************************************************/ void loop() { val = analogRead(A0);//Read the value of the potentiometer to val val = val/1024*5.0;// Convert the data to the corresponding voltage value in a math way Serial.print(val);//Print the number of val on the serial monitor Serial.print(“V”); // print the unit as V, short for voltage on the serial monitor lcd.setCursor(6,1);//Place the cursor at Line 1, Column 6. From here the characters are to be displayed lcd.print(val);//Print the number of val on the LCD lcd.print(“V”);//Then print the unit as V, short for voltage on the LCD delay(200); //Wait for 200ms } |
Video