Arduino-Analog I/O Function
This article details the use of Arduino's Analog I/O functions.
Analog I/O functions allow the Arduino board to output or input analog signals.
For other Arduino functions and libraries, please refer to the following article.
Analog I/O Functions
Functions | Parameters | Returns | Explanation |
---|---|---|---|
void analogReference(type) | type: Reference voltage setting | None | Configures the reference voltage for analog input |
int analogRead(pin) | pin: The analog pin number to set | 10bits: 0 to 1023 12bits: 0 to 4095 | Reads the value from the analog pin |
void analogWrite(pin, value) | pin: The digital pin number to set value: The duty cycle(0 to 255) | None | Writes an analog value (PWM wave) to the digital pin |
analogReference()/Configures the reference voltage for analog input
- Function: void analogReference(type)
- Parameter: type⇒Reference voltage setting
- Return: None
The analogReference() function sets the reference voltage for the analog input by setting the "reference voltage" as an parameter.
The reference voltage can be set at "5V" for "DEFAULT" or "1.1V" for "INTERNAL", and the voltage applied to the "AREF" pin becomes the reference voltage for "INTERVAL". However, a voltage exceeding 5 V cannot be applied to the "AREF" pin.
analogRead()/Reads the value from the analog pin
- Function: int analogRead(pin)
- Parameter: pin⇒The analog pin number to set
- Return: 0 to 1023(10bits)/0 to 4095(12bits)
The analogRead() function can be used to check the status of analog inputs by setting the "pin number" as an parameter.
The voltage applied to the analog input returns 0 to 1023 (10bits) or 0 to 4095 (12bits) in the 0 to 5V range.
For example, the Arduino Uno has a 10-bit analog input, so 1 V will return a value of about 205.
analogWrite()/Writes an analog value (PWM wave) to the digital pin
- Function: void analogWrite(pin, value)
- Parameter: pin⇒The digital pin number to set
- : value⇒The duty cycle(0 to 255)
- Return: None
The analogWrite() function performs analog output (PWM) by setting the "pin number" and "output value" as parameters.
The output value can be set in the range of "0 to 255". For example, a value of 51 results in a PWM output of about 1V.
Sample Programs (Sample Sketches)
Sample programs that use Analog I/O functions to move electronic parts such as CdS photoresistors and DC motors are introduced.
CdS Photoresistor
The voltage is read from a voltage divider circuit that combines a CdS photoresistor and a resistor with a program that uses Analog I/O functions.