How the voltage sensor module works with Arduino | Step by step
Hello, welcome back. In this tutorial, we will learn what is the voltage sensor and how to work with an Arduino. We can usually use a Multimeter or Voltmeter to measure the voltages. But, today we do that using the Arduino and the voltage sensor. Also, we can measure voltages between 0v and 25v through this sensor. You can use the knowledge in this tutorial for making voltage measurement projects. Also, we can buy this voltage sensor very cheap price from the market. The voltage sensor is as follows.
How does the voltage sensor work?
This sensor is made using a voltage divider circuit. Also, this voltage divider circuit includes two resistors of 30kohm and 7.5kohm. When a potential is applied to this circuit, we can get the voltage difference in the voltage divider point via the signal pin. So, the voltage can be calculated by getting it to the Arduino board through an analog pin. The voltage divider circuit is as follows.
The PIN structure of this sensor
Ok. let’s learn how to connect this sensor to the Arduino UNO board step by step. The required components are given below.
- Arduino UNO board x 1 -> Our Store / Amazon
- Voltage sensor x 1 -> Our Store / Amazon
- LCD display x 1 -> Our Store / Amazon
- I2C module x 1 -> Our Store / Amazon
- Jumper wires -> Our Store / Amazon
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Step 1
Firstly, identify these components.
Arduino UNO board
Voltage sensor
LCD screen
I2C module
Jumper wires
Step 2
Secondly, connect these components. To do this, use the circuit diagram below.
Step 3
Thirdly, let’s create the program for this project.
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 4);
#define Sensor A0
float vOUT = 0.0;
float vIN = 0.0;
float R1 = 30000.0;
float R2 = 7500.0;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
}
void loop() {
int value = analogRead(Sensor);
vOUT = (value * 5.0) / 1024.0;
vIN = vOUT / (R2 / (R1 + R2));
lcd.setCursor(0, 0);
lcd.print("Voltage :");
lcd.print(vIN);
lcd.print("v ");
Serial.print("Voltage : ");
Serial.println(vIN);
}
Code explanation
Firstly, the I2C library is included and creates an object for the library. Also, it includes the I2C address and size of the LCD
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 4);
Secondly, the sensor pin is defined. Also, four variables have been created to help the program.
#define Sensor A0
float vOUT = 0.0;
float vIN = 0.0;
float R1 = 30000.0;
float R2 = 7500.0;
In the setup function, the serial monitor and LCD are enabled.
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
}
In the loop function, the sensor values are taken and the voltage is calculated using the formula. These R1 and R2 resistors are in the voltage divider circuit. The voltage divider circuit is described above.
void loop() {
int value = analogRead(Sensor);
vOUT = (value * 5.0) / 1024.0;
vIN = vOUT / (R2 / (R1 + R2));
This code prints the voltage values on the serial monitor and LCD.
lcd.setCursor(0, 0);
lcd.print("Voltage :");
lcd.print(vIN);
lcd.print("v ");
Serial.print("Voltage : ");
Serial.println(vIN);
}
Step 4
OK, now select the board and port, Afterward, upload this code to the Arduino board.
OK, enjoy this tutorial using different types of voltage sources. But, you have to remember to give voltage sources from 0v to 25v. For this project, you can also use the following program.
#include <LiquidCrystal_I2C.h>
#define Sensorpin A0
LiquidCrystal_I2C lcd(0x27, 16, 4);
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
}
void loop() {
int value = analogRead(Sensorpin);
double voltage = map(value, 0, 1024, 0, 2500);
voltage /= 100;
lcd.setCursor(1, 0);
lcd.print("Voltage :");
lcd.print(voltage);
lcd.print("v "); Serial.print("Voltage : ");
Serial.println(voltage);
}
The full video guide is given below. So, we will meet in the next tutorial. Have a good day.
How the voltage sensor module works with Arduino | Step by step