analogRead()

[Analog I/O]
short analogRead(unsigned short pin);

Description

Reads the value from the specified pin.

This function allows to read the voltage value from every single pin from the Custom Element. The value is returned as an integer between 0 and 1023, where 0 corresponds to 0V and 1023 corresponds to the power voltage.

Parameters

pin: the Custom Element pin.

Returns

An integer value between 0 and 1023.

Example Code

The code reads the analog value from the pin 3 and sets the value obtained to the pin 2.

void setup() {
    pinLabel(3, "IN");   // sets the pin 3 label to "IN"
    pinMode(3, INPUT);   // sets the digital pin 3 as input
    pinLabel(2, "OUT");  // sets the pin 2 label to "OUT"
    pinMode(2, OUTPUT);  // sets the digital pin 2 as output
}

void loop() {s
    int value = analogRead(3);  // reads the analog value from the pin 3
    analogWrite(2, value);      // sets the value obtained to the pin 2
}

Notes and Warnings

The analogRead() function can be used with all the pins, even for the power and ground pins.