Introduction
In this lesson, we will use a potentiometer and an I2C LCD1602 to make a voltmeter.
Components
– 1* SunFounder Uno board
– 1 * USB data cable
– 1 * Potentiometer (50KΩ)
– 1 * I2C LCD1602
– Several jumper wires
– 1 * Breadboard
Experimental Principle
In this experiment, a potentiometer is used to divide voltage. Since the SunFounder Uno board can only read digital signals, but what the sliding end of the potentiometer outputs are analog signals, we need to convert these analog signals into digital ones with an Analog-to-Digital Convertor (ADC). Fortunately, the SunFounder Uno board itself comes with a 10-bit ADC which we can use to implement this conversion. Then display this digital output voltage on the I2C LCD1602.
Experimental Procedures
Step 1: Build the circuit
Step 2: Program (Please refer to the example code in LEARN -> Get Tutorials on our website)
Note: Here you need to add a library. Refer to the description in Lesson 4 previously in the manual.
Step 3: Compile the code
Step 4: Upload the sketch to the SunFounder Uno board
Now, adjust the potentiometer and you will see the voltage displayed on the I2C LCD1602 varies accordingly.
Code
/**************************************** name: Voltmeter function:adjust the potentiometer and you will see the voltage displayed on the I2C LCD1602 varies accordingly. ******************************************/ //Email:support@sunfounder.com //Website:www.sunfounder.com/*************************************/ // include the library code #include <Wire.h> #include <LiquidCrystal_I2C.h> /****************************************************/ LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display float val = 0; /****************************************************/ void setup() { Serial.begin(9600);//initialize the serial lcd.init(); //initialize the lcd lcd.backlight(); //open the backlight 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 Serial.println( ); 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 } |