analogRead()

설명

지정한 아날로그 핀에서 값을 읽습니다. 아두이노 보드는 6채널(미니와 나노는 8채널, 메가는 16채널), 10비트의 아날로그-디지털 변환기를 가지고 있습니다. 이는, 0에서 5V 사이의 입력 전압을 0에서 1024 사이의 정수 값으로 대응시키는 것을 뜻합니다. 이는 해상도가 5V/1024 혹은 0.0049V (0.49mV) 단위라는 것을 뜻합니다. 입력 범위와 해상도는 analogReference() 를 사용해서 바꿀 수 있습니다.

아날로그 입력을 읽는 데는 약 100마이크로초(0.0001초)가 걸리므로, 최대 읽기 속도는 1초에 약 1만 번입니다.

Board Operating voltage Usable pins Max resolution
Uno 5 Volts A0 to A5 10 bits
Mini, Nano 5 Volts A0 to A7 10 bits
Mega, Mega2560, MegaADK 5 Volts A0 to A14 10 bits
Micro 5 Volts A0 to A11[1] 10 bits
Leonardo 5 Volts A0 to A11[1] 10 bits
Zero 3.3 Volts A0 to A5 12 bits[2]
Due 3.3 Volts A0 to A11 12 bits[2]
MKR Family boards 3.3 Volts A0 to A6 12 bits[2]
  • [1]: A0에 A5까지는 보드에 표시되어 있고 A6부터 A11은 4, 6, 8, 9, 10 및 12를 통해 각각 이용 가능합니다.
  • [2]: 이 보드들은 호환성을 위해 기본적으로 10bit의 해상도를 가지고 있습니다. analogReadResolution() 함수를 통해 12bit로 조절이 가능합니다.

문법

analogRead(pin)

매개변수

  • pin: 읽을 아날로그 입력 핀(대부분의 보드에서 0~5, Mini와 Nano는 0~7, Mega는 0~15) 번호

반환값

  • int(0 에서 1023)

예제 코드

이 코드는 아날로그 핀의 전압을 읽어서 표시합니다.

Hardware Required

1×Arduino UNO or Genuino UNO
1×Potentiometer
1×Breadboard
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

Wiring Diagram

Arduino Potentiometer Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Arduino Code

int analogPin = A0; // 가변 저항의 가운데 핀이 아날로그 핀 A0에 연결됨 // 바깥쪽은 그라운드와 +5V에 연결됨 int val = 0; // 읽은 값을 저장할 변수 void setup() { Serial.begin(9600); // 시리얼 설정 } void loop() { val = analogRead(analogPin); // 입력 핀 읽기 Serial.println(val); // 값 디버그 }

Rotate the potentiometer and see the result on Serial Monitor

COM6
Send
0 0 126 281 517 754 906 1023 1023
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

For more detailed instruction, see Arduino - Potentiometer

※ 주의 및 경고:

아날로그 입력 핀이 아무데도 연결되지 않으면, analogRead()가 반환하는 값은 여러 요인(e.g. 다른 아날로그 입력 값, 여러분의 손이 보드에 얼마나 가까운가 등)에 따라 변합니다.

더보기

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit

※ OUR MESSAGES