Analog I/O

This library is to input/output an analog signal.

analogReference

Description
Configures the reference voltage used for analog input (i.e. the value used as the top of the input range). When 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(uint8_t analog_pin)
Parameters
analog_pin: The number of the analog input pin to read from A0 (14) to A7 (21)
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 (max 15 pins)
value: The duty cycle of the output PWM (0 - 255).
Returns
None

Sample Program

The LED on the GR-SAKURA board 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);
    }
}