아두이노 전압 읽기 - adu-ino jeon-ab ilg-gi

아두이노 기본예제인 ReadAnalogVoltage를 활용하여 정확한 전압을 읽는 예제입니다.

아두이노 전압 읽기 - adu-ino jeon-ab ilg-gi

/*
  ReadAnalogVoltage

  Reads an analog input on pin 0, converts it to voltage, and prints the result to the Serial Monitor.
  Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/ReadAnalogVoltage
*/

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(115200);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.print(sensorValue);
  Serial.print("\t");
  Serial.println(voltage);
  delay(100);
}

변경내용은 sensorValue값을 출력하고, delay(100)을 주었습니다.

아두이노 전압 읽기 - adu-ino jeon-ab ilg-gi

5V핀을 A0핀에 직접 연결한 후의 시리얼모니터 출력결과는

아두이노 전압 읽기 - adu-ino jeon-ab ilg-gi

float voltage = sensorValue * (5.0 / 1023.0) = 1023 * (5.0 / 1023.0) = 5.0

5V 핀의 실제 전압을 멀티테스터로 확인결과 4.86V로 확인되었습니다.

소스코드중 float voltage = sensorValue * (5.0 / 1023.0); 을

float voltage = sensorValue * (4.86 / 1023.0); 으로 바꾸면

아두이노 전압 읽기 - adu-ino jeon-ab ilg-gi

아날로그 입력핀 A0에서 읽은 값은 1023으로 동일하지만 5.0에서 4.86으로 실제 전압을 적용하면

voltage값도 그에 맞추어 변경됩니다.

아두이노 기본예제인 ReadAnalogVoltage를 이용하여 아날로그 입력핀으로 정확한 전압값을 읽기위해서는

float voltage = sensorValue * (4.86 / 1023.0); 부분에서 4.86값을 아두이노 보드의 5V핀의 실제전압을

적용해주어야 합니다.


무작정 보고 따라하며 배우는 아두이노 강좌입니다.
아두이노 공식 사이트에서 제공하는 강좌를 바탕으로 작성되었습니다.
해당 게시글은 Arduino UNO 보드를 기준으로 작성되었습니다.

7. 아두이노에서 전압 측정하기

- 5V 이하의 전압 아날로그 입력으로 측정하기

본 강좌에서는 아두이노의 Analog Pin 0 를 이용하여 analogRead()로 읽어온 값을 전압으로 변환하고, 아두이노 시리얼 모니터에 전압을 띄워줍니다. 단, 아두이노에서는 5V 이상의 전압이 입력되면 회로가 타버릴 수 있습니다. 5V 이상의 경우 분압저항을 이용하여 회로를 구성한 후 아두이노로 입력될 전압의 범위를 0~5V 이내로 하여 연결해 줍니다.

  해당 강좌에서는 외부 전압을 따로 측정하지 않고 아두이노에서 출력하는 5V에 가변저항을 달아 전압을 변경시켜 그 값을 아날로그로 읽어옵니다.

준비물 ( 아두이노와 PC는 기본입니다 )

10k 옴 가변저항, 브레드보드, 점퍼케이블


회로도

아두이노 전압 읽기 - adu-ino jeon-ab ilg-gi

먼저, 10k 가변저항의 핀 3개 중 바깥쪽 핀 한개에 GND를 연결해 줍니다. 그 반대쪽 끝 핀에는 아두이노의 5V와 연결해 줍니다. 마지막으로 가변저항의 가운데 핀은 아두이노의 아날로그 0번핀과 연결해 줍니다.

가변저항을 돌리게 된다면 가변저항의 저항값이 변경되게 되고, 이로인해 가운데 핀의 전압이 변경되게 됩니다.

아두이노와 같은 마이크로컨트롤러에는 아날로그-디지털 변환기 ( ADC , analog-to-digital converter )가 내장되어 있습니다. 이 회로를 통해 변화하는 전압을 0에서 1023의 디지털 값으로 변환해 줍니다. 쉽게말해, 가변저항을 통해 변경되는 전압을 아두이노의 analogRead()로 변경하게 됩니다. 0V는 analogRead()의 0 5V는 analogRead()의 1023으로 변환됩니다.

코드 설명

아래 코드에서 가장 먼저, 아두이노와 컴퓨터의 통신 설정을 해야 합니다. 일반적으로 초당 9600비트의 정보를 전송합니다.

 setup() 함수 내의  Serial.begin(9600); 으로 설정합니다.

다음으로 loop()함수 내에서 아날로그 입력을 받은 값을 편하게 사용할 수 있도록 변수로 지정해 줍니다. 0~1023의 값을 사용할 변수로는 int 로 지정해줍니다.

 int sensorValue = analogRead(A0); 

0-1023 까지의 값들을 0-5V의 값으로 변환하기 위해 float 으로 새로 변수를 하나 만들어주고, 약간의 계산식을 추가합니다. float 변수를 사용하며 값을 소수점까지 띄울 수 있습니다. voltage5를 1023으로 나눈 값에 sensorValue를 곱한 값으로 지정합니다.

 float voltage = sensorValue * (5.0 / 1023.0); 

마지막으로 계산된 voltage 값을 시리얼 윈도우로 띄워줍니다. 이전 강좌에서 있던 Serial.println()를 마지막에 추가하여 사용합니다.

 Serial.println(voltage); 

이렇게 구성된 코드를 작동하면, 아두이노를 키고나서 시리얼 모니터를 키게 된다면 모니터에 voltage의 값이 매 줄마다 뜨게 됩니다. 가변저항을 돌리게 된다면, 시리얼 모니터의 값도 실시간으로 변경되게 됩니다.

/*
  ReadAnalogVoltage
  Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor.
  Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.
*/

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println(voltage);
}