Below is the sketch for the voltmeter exercise

Copy and paste it into your Arduino IDE, (integrated development environment), and save it as voltmeter.

 

//***********************************************************

//Voltmeter – 30/08/2021

//Author Graham Sales

//This sketch can be used as a volt meter

//It takes an analogue value between 0 and 5 volts.

//Passes through the ADC to give a digital value of between 0 and 1023.

//1023 / 5 = 204

//Then 5 * (AnalogueIn/1023) = measured volts

//***********************************************************

//Declare global variables

//we need an analogue input pin. We will use A0

const int sensorPin = A0;

//We need a variable to hold the analogue input value

//This will not be an integer, it will be a real number

// Then it must be declared as a type floating point

float analogueIn = 0;

//We need a variable to hold the calculated voltage

//This will also be a floating point number

float voltage = 0;

//***********************************************************

void setup() {

// setup serial communications with the host computer

Serial.begin(9600);

}

//***********************************************************

void loop() {

// read the value from the sensor:

analogueIn = analogRead(sensorPin);

Serial.println(analogueIn); // test the analogue input value

voltage = 5 * (analogueIn / 1023); // calculate the voltage

Serial.print(“The voltage = “);

Serial.println(voltage); // print the voltage

delay (500); // wait half a second or the serial monitor output will be frantic

}