Skip to main content

Analog I/O

Analog I/O

This library is for analog signal input and output.

analogReference

Description
Configures the reference voltage used for analog input (i.e. the value used as the top of the input range). When the input is the same as the reference voltage, the analogRead function returns 1023. For example, default is set to the Arduino compatible reference voltage 5.0V; therefore, when 3.3V is applied, 675 is returned. However, note that 5V cannot be applied. When RAW12BIT (for 12-bit A/D conversion) is set to the reference voltage, 4095 is returned.
Syntax
analogReference(uint8_t mode)
Parameters
mode:
   DEFAULT: 5.0V (Arduino compatible)
   INTERNAL: 1.1V (internal voltage)
   EXTERNAL: AVREF pin supply voltage
   RAW12BIT: 3.3V (for 12-bit A/D conversion)
Returns
None

analogRead

Description
Reads the value from the specified analog pin.
Syntax
int analogRead(analog_pin)
Parameters
analog_pin: The number of the analog input pin to read from A0 to A7 (or 22 to 27)
Returns
int 0 to 1023; for 12-bit A/D conversion, 0 to 4095

analogWrite

Description
Writes an analog value (PWM wave) to a pin. Can be used to light an LED at varying brightnesses or drive a motor at various speeds. The frequency of the PWM signal is approximately 490Hz.
Syntax
analogWrite(uint8_t pin, int value);
Parameters
pin: The number of the pin to write to (53 only)
value: Analog converted value (0 to 1023)
Returns
None

analogWriteDAC

Description
Outputs digital values which it first converts to analog. Unlike analogWrite, does not output a PWM signal. 3.3V are output when the digital value is 1023.
Syntax
analogWriteDAC(int pin, int value)
Parameters
pin: The number of the pin to write to (53 only)
value: Analog converted value (0 to 1023)
Returns
None

Example

GR-KAEDE's LED lights up smoothly.


#include <Arduino.h>
unsigned char ledpat[6][4] = {
    { 20, 50, 100, 200 },
    {  50,100,200,  20 },
    { 100,200, 20,  50 },
    { 200, 20, 50, 100 },
    { 100,200, 20,  50 },
    {  50,100,200,  20 },
};
 
void setup()
{
    pinMode(PIN_LED0,OUTPUT);
    pinMode(PIN_LED1,OUTPUT);
    pinMode(PIN_LED2,OUTPUT);
    pinMode(PIN_LED3,OUTPUT);
}
 
void loop()
{
    int i;
    for (i = 0; i < 6; i++){
        analogWrite(PIN_LED0, ledpat[i][0]);
        analogWrite(PIN_LED1, ledpat[i][1]);
        analogWrite(PIN_LED2, ledpat[i][2]);
        analogWrite(PIN_LED3, ledpat[i][3]);
        delay(100);
    }
}