인텔 갈릴레오2 가변저항을 이용한 LED 밝기 조정하기

 인텔 갈릴레오와 가변저항을 이용해서 아날로그 입력값을 읽어 LED 밝기를 조절하는 회로를 만들어보자. LED대신에 모터 등을 연결하여 속도를 조절 할 수도 있다.
AnalogInOutSerial : https://communities.intel.com/docs/DOC-22422
준비물 : 10K옴 가변저항, 브레드보드, 연결전선
기본 스케치 파일 : 파일 -> 예제 -> 03. Analog -> AnalogInOutSerial
 스케치 코드
/*
  Analog input, analog output, serial output

 Reads an analog input pin, maps the result to a range from 0 to 255
 and uses the result to set the pulsewidth modulation (PWM) of an output pin.
 Also prints the results to the serial monitor.

 The circuit:
 * potentiometer connected to analog pin 0.
   Center pin of the potentiometer goes to the analog pin.
   side pins of the potentiometer go to +5V and ground
 * LED connected from digital pin 9 to ground

 created 29 Dec. 2008
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */
// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPin = A0;  // 아날로그 입력
const int analogOutPin = 9; // LDE가 연결될 아날로그 출력
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)
void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}
void loop() {
  sensorValue = analogRead(analogInPin);            // 아날로그 입력값을 읽는다.
   // 읽은 아날로그 값을 map함수를 이용해서 0~1023 값을 0~255 값을 변환한다.
  outputValue = map(sensorValue, 0, 1023, 0, 255);

  analogWrite(analogOutPin, outputValue);           // 아날로그 출력을 한다.

  Serial.print("sensor = " );                                     //시리얼 모니터에 출력
  Serial.print(sensorValue);    
  Serial.print("\t output = ");    
  Serial.println(outputValue);

  delay(2);                                                            // 다음 입력을 위해 2milesecond 대기
}
 기본 회로도
 스케치 코드 분석
analogWrite는 2개의 인자값을 가지는데, 다음과 같은 형태로 사용할 수 있다.
analogWrite(출력 아날로그핀, 출력값) 
map 함수는 총 4개의 인자 값을 가지는데, 입력된 값의 범위를 A범위에서 B범위로 변경한다.
map(값, 기존범위시작값, 기존범위종료값, 변환범위시작값, 변환범위종료값)
 1번 아날로그 입력으로 들어온 가변저항의 값을 읽어온다. 가변저항의 범위는 0에서 1023이기에 map함수를 이용해서 코드 값을 PWM의 범위값인 0~255 값으로 변경한다. 변경된 값을 PWM으로 내보낸다. 

 회로 만들기
 5V출력과 GND를 기판에 연결한다. 그리고 가변저항을 적절한 위치에 꼽는다.
 가변저항은 +,-가 없고 다만 가운데 부분과 옆쪽의 구분만 있다. 옆다리 하나를 +극에 연결하고 다른 하나를 - 극에 연결한다.
 그리고 가운데 다리를 A0 입력단자에 연결한다.
 LED를 위치 시킨다.
LED의 긴다리 쪽을 PWD의 ~9에 연결하고 짧은 다리를 GND(-)에 연결한다.
 가변저항을 좌우로 회전시키면 LED에 불이 드어오는것을 확인할 수 있다. 시리얼 모니터로도 가변저항값이 어떻게 변화되는지 확인할 수 있다.

댓글 쓰기