Arduino Read Analog Voltage

From WikiOD
Revision as of 18:33, 27 August 2021 by Admin (talk | contribs) (→‎Program)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This example will show you how to read the analog input on analog pin 0. The input is converted from analogRead() to voltage and printed out to the serial monitor of the Arduino software (IDE).

Required components[edit | edit source]

You will need the following components:

  • 1 × Breadboard
  • 1 × Arduino Uno R3
  • 1 × 5K variable resistor (potentiometer)
  • 2 × jumper

Program[edit | edit source]

Connect the components on the breadboard according to the circuit diagram, as shown in the figure below.

Potentiometer[edit | edit source]

The potentiometer is a simple electromechanical sensor. It converts the rotation or linear motion from the input operator into a change in resistance. This change is (or can) be used to control anything, from high-fidelity sound systems to the direction of huge container ships.

We know that the potentiometer was originally called a rheostat (essentially a variable wire-wound resistor). The types of resistors available now are quite amazing, and beginners (especially) may find it difficult to determine which type is suitable for a given task. Several different resistor types can be used for the same task, making it harder to determine the job.


The image on the left shows the standard schematic symbol for resistors. The image on the right is a potentiometer.

Sketch[edit | edit source]

Open the Arduino IDE software on the computer. Use the Arduino language to code and control your circuit. Open a new sketch file by clicking "New".


Arduino code[edit | edit source]

 void setup() {
    //Initialize the serial communication rate as9600bit/s:
 void loop() {
    // Read the input data of analog pin A0
    int sensorValue = analogRead(A0);
    // Convert analog signal to voltage
    float voltage = sensorValue * (5.0 / 1023.0);
    //Print to serial monitor

Code description[edit | edit source]

In the program or sketch given below, the first thing you do in the setup function is to start serial communication between your circuit board and your computer at 9600 bits per second, using the following code:


In the main loop of the code, you need to create a variable to store the resistance value from the potentiometer (its range is between 0 and 1023, which is very suitable for the int data type):

int sensorValue = analogRead(A0);

To change the value from 0-1023 to the range corresponding to the voltage being read by the pin, you need to create another variable, a floating point number and do some calculations. To reduce the number between 0.0 and 5.0, divide 5.0 by 1023.0 and multiply by sensorValue:

float voltage= sensorValue * (5.0 / 1023.0);

Finally, you need to print this information to the serial window. You can use the Serial.println() command in the last line of code:


Now, open the serial monitor in the Arduino IDE by clicking the icon on the right side of the top green bar or pressing Ctrl+Shift+M.


Result[edit | edit source]

You will see a steady stream of numbers from 0.0 to 5.0. When you turn the potentiometer, these values ​​will change, corresponding to the voltage on pin A0.