Analog Input-Output Functions

An analog signal is one that can take on any number of values, unlike a digital signal which has only two values: HIGH and LOW. To measure the value of analog signals, the Arduino has a built-in analog-to-digital converter (ADC). The ADC turns the analog voltage into a digital value. The function that you use to obtain the value of an analog signal is analogRead(pin). This function converts the value of the voltage on an analog input pin and returns a digital value from 0 to 1023, relative to the reference value. The reference is 5V on most Arduino

Function

The Arduino functions associated with Analog signals that we will be using in this tutorial are

  1. analogWrite(pin_number,value)
  2. anlogRead(pin_number)

analogWrite(pin_number,value)

This function writes a analog value to a pin. Here in Arduino there are 6 Analog pins so use the same to write Value . Value must be range of 0 to 1023

analogWrite(Analogpin,255);   //set Analogpin as  255

analogRead(pin_number)

int reads = analogRead(Analogpin);	//read the analog value on pin

This function reads a analog value from a pin. Analogpin is the number of the analog I/O pin you want to read. This function returns values from a range of 0 to 1023.

Additonal Notes

The Arduino does not have a built-in digital-to-analog converter (DAC), but it can pulse-width modulate (PWM) a digital signal to achieve some of the functions of an analog output. The function used to output a PWM signal is analogWrite(pin, value). pin is the pin number used for the PWM output. value is a number proportional to the duty cycle of the signal. When value = 0, the signal is always off. When value = 255, the signal is always on. On most Arduino boards, the PWM function is available on pins 3, 5, 6, 9, 10, and 11. The frequency of the PWM signal on most pins is approximately 490 Hz. On the Uno and similar boards.

To map an analog input value, which ranges from 0 to 1023 to a PWM output signal, which ranges from 0 - 255, you can use the map(value, fromLow, fromHigh, toLow, toHigh) function. This function has five parameters, one is the variable in which the analog value is stored, while the others are 0, 1023, 0 and 255 respectively.

Next : Miscellaneous