From SainSmart Wiki
Jump to: navigation, search


What’s Analog Pins?

  • A/D converter

The Atmega168 contains an onboard 6 channel analog-to-digital (A/D) converter. The converter has 10 bit resolution, returning integers from 0 to 1023. While the main function of the analog pins for most Arduino users is to read analog sensors, the analog pins also have all the functionality of general purpose input/output (GPIO) pins (the same as digital pins 0 - 13). Consequently, if a user needs more general purpose input output pins, and all the analog pins are not in use, the analog pins may be used for GPIO.

  • Pin mapping

The Arduino pin numbers corresponding to the analog pins are 14 through 19. Note that these are Arduino pin numbers, and do not correspond to the physical pin numbers on the Atmega168 chip. The analog pins can be used identically to the digital pins, so for example, to set analog pin 0 to an output, and to set it HIGH, the code would look like this:

pinMode(14, OUTPUT);

digitalWrite(14, HIGH);

  • Pullup resistors

The analog pins also have pullup resistors, which work identically to pullup resistors on the digital pins. They are enabled by issuing a command such as digitalWrite(14, HIGH); // set pullup on analog pin 0 while the pin is an input. Be aware however that turning on a pullup will affect the value reported by analogRead() when using some sensors if done inadvertently. Most users will want to use the pullup resistors only when using an analog pin in its digital mode.

  • Details and Caveats

The analogRead command will not work correctly if a pin has been previously set to an output, so if this is the case, set it back to an input before using analogRead. Similarly if the pin has been set to HIGH as an output, the pullup resistor will be on, after setting it back to an INPUT with pinMode. The Atmega168 datasheet also cautions against switching digital pins in close temporal proximity to making A/D readings (analogRead) on other analog pins. This can cause electrical noise and introduce jitter in the analog system. It may be desirable, after manipulating analog pins (in digital mode), to add a short delay before using analogRead() to read other analog pins.

  • analogRead()
    • Description

Reads the value from the specified analog pin. The Arduino board contains a 6 channel (8 channels on the Mini and Nano, 16 on the Mega), 10-bit analog to digital converter. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per unit. The input range and resolution can be changed using analogReference(). It takes about 100 microseconds (0.0001 s) to read an analog input, so the maximum reading rate is about 10,000 times a second.

    • Syntax


    • Parameters

pin: the number of the analog input pin to read from (0 to 5 on most boards, 0 to 7 on the Mini and Nano, 0 to 15 on the Mega)

    • Returns

int (0 to 1023)

What’s Potentiometer?

A potentiometer is a simple knob that provides a variable resistance, which we can read into the Arduino board as an analog value. In this example, that value controls the rate at which an LED blinks. We connect three wires to the Arduino board. The first goes to ground from one of the outer pins of the potentiometer. The second goes from 5 volts to the other outer pin of the potentiometer. The third goes from analog input 2 to the middle pin of the potentiometer. By turning the shaft of the potentiometer, we change the amount of resistence on either side of the wiper which is connected to the center pin of the potentiometer. This changes the relative "closeness" of that pin to 5 volts and ground, giving us a different analog input. When the shaft is turned all the way in one direction, there are 0 volts going to the pin, and we read 0. When the shaft is turned all the way in the other direction, there are 5 volts going to the pin and we read 1023. In between, analogRead() returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.

QQ图片20130716175727.jpg QQ图片20130716175748.jpg

Example code:

int potpin = 0 ; //define analog pin0 int ledpin = 13 ; //define analog pin13 int val = 0 ; //set val is0.

void setup() {

   pinMode(ledpin,OUTPUT);//set analog pin13 output
   Serial.begin(9600);//set baud rate 9600 

} void loop() {

   digitalWrite(ledpin,HIGH);//light up led in pin13
   delay(50);//delay 0.05s
   digitalWrite(ledpin,LOW);//go out led in pin13
   delay(50);//delay 0.05s
   val =  analogRead(potpin);//give the value of pin0 to val
   Serial.println(val) ; //print val’s value