# 아날로그 입력

## 아날로그 입력

이 문서에서는 가변 저항을 이용한 아날로그 입력방법에 대해서 다룬다.

### 결선도

<div align="left"><img src="/files/-Mf1ojdtk5-dZP8tWQDB" alt=""></div>

### 샘플 코드

```
const int PIN = A0;
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  int analogValue = analogRead(PIN);
  Serial.println(analogValue);
}
```

### 컴파일

`Ctrl+R` 또는 스케치 메뉴의 `확인/컴파일`을 눌러 컴파일을 진행한다.

### 업로드

`Ctrl+U` 또는 스케치 메뉴의 `업로드`를 눌러 업로드를 진행한다.

### 실행 결과

다이얼 게이지를 돌리면 값이 변화하는 것을 확인할 수 있다.

### 코드 설명

```
const int PIN = A0;
```

입력에 사용할 PIN을 A0로 선언한다. A0는 A0핀을 의미하는 상수이다.

```
void setup()
{
  Serial.begin(9600);
}
```

`setup` 함수에서는 Serial의 전송속도를 9600bit로 설정한다.

```
void loop()
{
  int analogValue = analogRead(PIN);
  Serial.println(analogValue);
}
```

`loop` 함수에서는 `analogRead`함수를 통해 0\~1023 사이의 값을 읽고 이를 Serial로 출력한다.

### LED 연결 추가

LED를 추가하여 가변저항의 값을 `analogRead`로 읽어들인 뒤 변환하여 `analogWrite`로 LED에 내보내는 예제를 구현한다.

### 결선도

<div align="left"><img src="/files/-Mf1omYZ2z-B4gAz7cmT" alt=""></div>

### 샘플 코드

```
const int LED = 10;
const int PIN = A0;
void setup()
{
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
}

void loop()
{
  int analogValue = analogRead(PIN);
  Serial.println(analogValue);
  analogWrite(LED, analogValue / 4);
}
```

### 컴파일

`Ctrl+R` 또는 스케치 메뉴의 `확인/컴파일`을 눌러 컴파일을 진행한다.

### 업로드

`Ctrl+U` 또는 스케치 메뉴의 `업로드`를 눌러 업로드를 진행한다.

### 실행 결과

가변저항이 변함에 따라 밝기가 달라지는 것을 확인할 수 있다.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sysout.co.kr/base-language/arduino/arduino-control/analog-input.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
