AnalogRead

From Pinguino-Wiki

Jump to: navigation, search

Contents

Name

analogRead - Read a 10 bits analog value on a pin.

Description

pin 13 to 17 can be used as digital I/O or analog input. If one pin between 13 and 17 is used as analog input, all those pins are configured as analog inputs. The read value is a 10 bits analog value ( 0 to 1023 for a 0 to 5Vcc signal ).

Since
Beta 2

Syntax

analogRead(pin);
pin
pin number to read from (13 to 17 for PIC18F2550)
Output
return a value proportional to the voltage applied on the selected pin ( 0..5V )
ground: return 0
+2.5v: return 511
+5v: return 1023

Example

// level detector
 
int level;
 
void setup()
{
pinMode(0,OUTPUT);
digitalWrite(0,LOW);
}
 
void loop()
{
level=analogRead(13);
if (level>512) digitalWrite(0,HIGH);  // if voltage applied on pin 13 > 2,5V set pin 0
else digitalWrite(0,LOW);             // else reset pin 0
}

Bugs

See also

digitalRead

Comments

Personal tools