Skip to main content

Analog I/O

Analog I/O

This library is to input/output an analog signal.

analogReference

Description
Configures the reference voltage used for an analog input. When the input is the same as the reference voltage, 1023 will be returned. 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 A3(17)
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 20 pins)
value: The duty cycle of the output PWM (0 - 255).
Returns
None

Sample Program

Make an LED on the GR-CITRUS board light up.


#include <Arduino.h>
void setup(){
}
    
void loop(){
    for(int i = 0; i < 256; i++){
        analogWrite(PIN_LED0, i);
        delay(3);
    }
    for(int i = 0; i < 256; i++){
        analogWrite(PIN_LED0, 256 - i - 1);
        delay(3);
    }
        
}