본문 바로가기

Arduino/ADC

Arduino ADC example

아두이노를 이용하여 아나로그 입력을 디지털 데이터로 변환하는 예


https://www.arduino.cc/en/Reference/AnalogRead


아두이노에서는 0V~5V 사이의 전압의 크기를 0~1023 ( 10 bit )사이의 숫자로 변환하여 읽을 수 있는 아나로그 입력핀 6개가 별도로 지정되어 있고 샘플링 횟수는 10,000 회/ sec 이다. 그러므로 샘플링 간격은 100 마이크로 초가 된다. 아나로그 핀의 번호는 대부분의 보드에서 0~5 이고 특정 보드에서는 약간의 차이가 있다


아나로그 입력은 조이스틱이나 가변저항을 조작함에따라 그 위치를 디지털 데이터(숫자)로 변환하여 입력받고자 할 때 많이 사용된다


아래의 코드는 아나로그 핀 3번으로부터 아나로그 입력을 받아서 디지털 값으로 변환하는 예이다

아나로그 2번 핀에 가변저항(Potentionmeter)의 중간단자(wiper)가 연결된 경우에 가변저항의 핸들을 조정함에 따라서 0~1023 까지의 값을 출력하게 된다

int analogPin = 2;     // potentiometer wiper (middle terminal) connected to analog pin 2
                       // outside leads to ground and +5V
int val = 0;           // variable to store the value read

void setup()
{
  Serial.begin(9600);          //  setup serial
}

void loop()
{
  val = analogRead(analogPin);    // read the input pin
  Serial.println(val);             // debug value
}



Potentionmeter 를 이용하여 LED의 밝기를 조정하는 예

https://www.arduino.cc/en/tutorial/potentiometer


/* Analog Read to LED
 * ------------------ 
 *
 * turns on and off a light emitting diode(LED) connected to digital  
 * pin 13. The amount of time the LED will be on and off depends on
 * the value obtained by analogRead(). In the easiest case we connect
 * a potentiometer to analog pin 2.
 *
 * Created 1 December 2005
 * copyleft 2005 DojoDave <http://www.0j0.org>
 * http://arduino.berlios.de
 *
 */

int potPin = 2;    // select the input pin for the potentiometer
int ledPin = 13;   // select the pin for the LED
int val = 0;       // variable to store the value coming from the sensor

void setup() {
  pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
}

void loop() {
  val = analogRead(potPin);    // read the value from the sensor
  digitalWrite(ledPin, HIGH);  // turn the ledPin on
  delay(val);                  // stop the program for some time
  digitalWrite(ledPin, LOW);   // turn the ledPin off
  delay(val);                  // stop the program for some time
}