아두이노 기본 예제중 ReadAnalogVoltage 예제입니다.

아두이노관련 영어어휘를 익히고자 일부러 영문으로 설정하여 사용하여 지금은 한글보다 더 익숙하게 되었습니다.

ide설정을 일부러 영문으로 시작해보는 것도 좋은 방법인 것 같습니다.

/*
  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(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);
}

아두이노 우노나 나노의 경우 아날로그 입력핀을 10비트로 읽습니다.

2의 10승 = 1024, 하여 0부터 1023까지 1024단계로 읽습니다.

위의 예문 중간부

// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V): 

는 전압 0-5V까지 0-1023의 값으로 읽는다는 의미입니다.

즉 0V -> 0이고, 5V -> 1023에 대응된다면, 5V를 1023등분하면

5V / 1023 = 0.00488 약 0.0049V = 4.9mV

아날로그 입력핀으로 읽는 값은 4.9mV 단위로 구분되어 읽을수 있게 됩니다(해상력이 4.9mV라고 할수 있음)

예를 들어 읽은 값이 value = 758 이라고 했을때 몇 V인지 계산한다면

758 x 0.0049 = 3.7142V 이고

비례식 5V : 1023 = x : 758

x = 758 x 5V/1023 = 3.7047V (0.00488과 0.0049에 의한 오차) 

위의 예제 코드중 float voltage = sensorValue * (5.0 / 1023.0); 은

x 를 구하는 전압 float voltage, 758 을 아날로그 입력값 sensorValue로 바꾸면 동일한 식입니다.

 

 

 

+ Recent posts