Display Potentiometer Value

About The Project

In this project, we will learn how to display the analog value from the middle pin of a potentiometer when it is used in a voltage divider circuit.

Potentiometer

A rotary potentiometer or a pot, is an electromechanical component used to measure angular displacement or rotation. It consists of three terminals:

  1. Terminal 1 (GND): Connected to ground (0V).
  2. Terminal 2 (Wiper): The output terminal that moves along a resistive track inside the potentiometer as it is rotated.
  3. Terminal 3 (Vcc): Connected to a voltage source (typically 5V or 3.3V).

Circuit wiring

This circuit continuously reads the analog voltage from a potentiometer connected to pin A2 (analogPin). It prints the analog value to the Serial Monitor every 500 milliseconds. This allows you to monitor and observe the changing analog readings in real-time.

Programme Code

C
// www.matthewtechub.com

// print the analog value on the serial monitor.

int analogPin=2;

int value;

void setup() {

pinMode(analogPin,INPUT);

Serial.begin(9600);

}

void loop() {

value=analogRead(analogPin);

delay(500);

Serial.print("Value:");

Serial.println(value);

}

Code Explanation

int analogPin = 2;  // Pin connected to the potentiometer

int value;          // Variable to store the analog reading

  • analogPin: Defines the Arduino pin number (pin A2 in this case) to which the potentiometer is connected.
  • value: Integer variable used to store the analog reading obtained from the potentiometer.· 

void setup() {

  pinMode(analogPin, INPUT);  // Configures analogPin as an input pin to read analog data

  Serial.begin(9600);         // Initializes serial communication with baud rate 9600

}

  • pinMode(analogPin, INPUT);: Configures analogPin (pin A2) as an input pin, indicating it will read analog data from the potentiometer.
  • Serial.begin(9600);: Initializes serial communication between Arduino and the connected computer at a baud rate of 9600 bits per second. This allows data to be sent and received via the Serial Monitor.

void loop() {

  value = analogRead(analogPin);  // Reads the analog value from analogPin and stores it in ‘value’

  delay(500);                     // Delays for 500 milliseconds (0.5 seconds)

Serial.print(“Value:”);         // Prints the label “Value:” to the Serial Monitor

  Serial.println(value);          // Prints the analog value stored in ‘value’ followed by a newline

}

  • analogRead(analogPin);: Reads the analog voltage value from the potentiometer connected to analogPin (pin 2) and assigns it to the variable value.
  • delay(500);: Introduces a delay of 500 milliseconds (0.5 seconds) between each loop iteration. This delay allows time for stable analog readings and prevents rapid updates on the Serial Monitor.
  • Serial Output:
  • Serial.print(“Value:”);: Prints the label “Value:” to the Serial Monitor.
  • Serial.println(value);: Prints the analog value stored in value to the Serial Monitor.

Modify Yourself

  1. Please turn on an LED when the value reaches 500.
  2. Modify the program to measure the voltage between the middle pin and the ground terminal of the potentiometer.

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!