Arduino Basic Tutorial

Arduino Basic Tutorial: Analog Input Pins in Arduino

Description:

Analog input pins are a feature of the Arduino microcontroller board that allows it to read analog signals from sensors or other input devices. These pins are labeled with the letter “A” followed by a number, such as A0, A1, A2, and so on. The Arduino has a built-in analog-to-digital converter (ADC) that converts analog input voltages to a digital value that can be read by the microcontroller. This digital value can then be used by the Arduino to perform a variety of tasks, such as controlling a motor or displaying data on a screen. The analog input pins are an important feature of the Arduino and are commonly used in a wide range of projects and applications.




Amazon Links:

Arduino Uno:

Mega 2560:

Arduino Nano

12v Adaptor:

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

What is analog input?

Arduino is a microcomputer board, various Arduino boards are equipped with digital input/output terminals (I/O terminals).

The number varies depending on the Arduino board, but for example, the commonly used Arduino Uno has a total of 14 digital input/output terminals from D0 to D13.
Since it exchanges digital signals (HIGH or LOW), it is a terminal that can be used for ON/OFF judgment of switches and exchange with sensors.

arduino Analog Input pins

In addition to these digital input/output terminals, Arduino boards also have analog input terminals.
Terminals that can be used to measure analog values ​​such as joysticks and potentiometers.



Arduino analog input pins

Arduino Uno has a total of 6 analog input terminals from A0 to A5,

arduino Analog Input pins

Nano and Pro mini have a total of 8 analog input terminals from A0 to A7.

This analog input terminal can also be used as a digital input/output terminal.
If you run out of digital terminals due to the usage of Arduino, you can use it as a digital terminal with a simple setting.

Analog input pins, as the name suggests, are pins for reading analog values.

For example, “Time”.

Time is a continuous quantity that lasts forever. Also, the voltage of a dry battery is expressed as “1.5V”. However, in reality, it should be “1.49875……” forever. However, the world of computers is expressed in the world of binary numbers of “0” and “1”. It’s called “digital”.



In this state, “analog values” represented by irrational numbers cannot be handled. That ‘s where the ” A/D converter ” comes in. This “A/D converter” exists everywhere in the world. why?

That’s because humans are “analog” . Connecting “human: analog” and “computer: digital”. For that reason, it is necessary to convert it with an “A/D” converter so that the computer can understand human behavior. Autonomous driving, which has become popular recently, converts analog data obtained from sensors into digital data for computer processing.

Arduino AD converter

The Arduino is equipped with a 10-bit A/D converter.

In this case, the resolution is 10 bits.

What is 10bit resolution? That’s what I mean.

Binary numbers that can be represented by 10 bits are as follows.

“0000000000” ~ “1111111111”

There are 1024 combinations in total.

*Calculation method 2^10 = 1024



Let’s apply it to the 5V voltage of the Arduino UNO.

It means that it has a resolution of 5÷1024 = about 4.88mV.

The table below shows the relationship between “voltage”, “binary number” and “decimal number”.

Input voltage[mV] binary number decimal number
0.00 ~ 4.88 0000000000 0
4.88 ~ 9.76 0000000001 1
・・・ ・・・ ・・・
4990.24 ~ 4995.12 1111111110 1022
4995.12 ~ 5000.00 1111111111 1023

*It is an easy-to-understand expression and may differ from the actual value.

To calculate an accurate value, measure the voltage of the Arduino’s 5V power supply with a tester.

Actually, it’s not 5V.




Analog Input Example

Analog inputs correspond to analog input terminals 0, 1, 2, 3, 4, and 5 shown in the above image.
*A0, A1, A2, A3, A4, A5

Terminal voltage can be measured by connecting an electronic part to the target pin.

Also, it takes 100 microseconds (0.0001 seconds) to read the analog input .

The maximum reading rate is 10000 times (10,000 times) per second .

The control commands to be used are as follows.

analogRead (pin_number)

Pin_number: Specify the pin number to read (0, 1, 2, 3, 4, 5)

Return value: Integer value from 0 to 1023

The read value is converted to an integer value from 0 to 1023.

Therefore, when obtaining the voltage, it is necessary to calculate it.

Calculate below. *For 5V

Calculate the voltage value from the return value

Return value ÷ 1024 × 5[V]

Please note that the voltage obtained has only a resolution of 1LSB (4.88mV).

If you want to increase the accuracy even a little.

For the 5V value, enter the actual value of the Arduino’s 5V power supply measured with a tester.

Example Read the value of analog input 3 (A3: pin number 3) and calculate the voltage value

int val;

float volt;

val = analogRead( 3 );

volt = (float)val / 1024.0f * 5.0f;

The float is used here to avoid calculation overflow.



Here is a simple code.

//sample code

int analogPin = 3; //Set analog input pin to number 3

int  val = 0; //Set variable for read value

float v_convert = 0.0 f; //Set variable for calculated value , set flaot to not overflow

void setup() {

 Serial.begin( 9600 ); //Specify the data transfer rate of serial communication at 9600bps. bps is bits per second.

}

void loop () {

 val = analogRead( analogPin ); // read pin value

v_convert = ( float ) val / 1024.0 f * 5.0 f; // convert read value to voltage, calculate with float

Serial.print(“value / volt : “);// print the display

Serial.print (val); // print the reading

 Serial. print(” / “) ; // display the calculated voltage value, new line

delay( 500 ); //Delay to display every 500ms (0.5 seconds)

}

A potentiometer makes it easy to understand how to use the analogRead() function. A potentiometer is a variable resistor. It seems that it can be practiced by reading the voltage of the output of the variable resistor.

Related Articles

Leave a Reply

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

Back to top button