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.

Syntax

analogReference(mode)

Parameters

mode: DEFAULT(VCC voltage), INTERNAL(Internal Voltage 1.45V), EXTERNAL(VCC Voltage)

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 14 to 21)

Returns

0 to 1023

Notes

Use A0 to A5 pins from the smaller pin as much as possible. When you set digital IO with pinMode(), the larger pin also becomes digital IO. For example, if you set A2 to digital IO, then A3, A4 and A5 will become digital IO, even if you use those pins as analog input.

Remark

If you use setPowerManagementMode() for SNOOZE mode and then call anlogRead(), the program stops until data is received from lower to upper.

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. After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin). The frequency of the PWM signal is approximately 490Hz (Some pins are 125Hz. Refer to the remark.). The frequency can be changed by analogWriteFrequency.

Syntax

analogWrite(int pin, int value);

Parameters

pin: The pin to write to (3, 5, 6, 9, 10, 11, 22, 23, and 24)
value: The duty cycle: Between 0 (always LOW) and 255 (always HIGH)

Returns

None

Remark

Pins 11, 22, 23, and 24 are implemented by software. The frequency is 125Hz.

analogWriteFrequency

Description

Writes a frequency value to the analog pin (PWM).

Syntax

analogWriteFrequency(int Hz)

Returns

Hz: Frequency

Returns

None

Remark

This function should be called during all PWM stops. Pins that can be changed are 3, 5, 6, 9, and 10.


Example

The green LED on the GR-COTTON board is lit slowly.


        #include <Arduino.h>
        void setup() {
          pinMode(23, OUTPUT); //Green LED
        }
         
        void loop() {
            for(int i = 0; i<250; i+=5){
                analogWrite(23, i);
                delay(30);
            }
            for(int i = 0; i<250; i+=5){
                analogWrite(23, 255-i);
                delay(30);
            }
        }