BASIC4MCU | 질문게시판 | 적외선 거리센서 VL6180
페이지 정보
작성자 탈창곽 작성일2020-04-28 20:55 조회17,671회 댓글3건본문
안녕하세요. 적외선 거리센서 VL6180 을 이용하게 되었습니다.
입력 전압은 센서가 2.8V 인데 우노 5V와 연결하고 저항을 이용하여 조절하였습니다.
코드를 업로드 시키고 시리얼 모니터를 틀면 계속 값이 이상하게 나오네요.
그래서 코드도 인터넷 찾아 2가지 버전으로 다 해봤는데 둘 다 값이 이상하게 나와요.
[코드 1]
\
#include
#include
#define VL6180X_ADDRESS 0x29
VL6180xIdentification identification;
VL6180x sensor(VL6180X_ADDRESS);
void setup() {
Serial.begin(115200);
Wire.begin();
delay(100);
sensor.getIdentification(&identification);
printIdentification(&identification);
if(sensor.VL6180xInit() != 0){
Serial.println("FAILED TO INITALIZE");
};
sensor.VL6180xDefautSettings();
delay(1000);
}
void loop()
{
Serial.print("Ambient Light Level (Lux) = ");
Serial.println( sensor.getAmbientLight(GAIN_1) );
Serial.print("Distance measured (mm) = ");
Serial.println( sensor.getDistance() );
delay(500);
};
void printIdentification(struct VL6180xIdentification *temp){
Serial.print("Model ID = ");
Serial.println(temp->idModel);
Serial.print("Model Rev = ");
Serial.print(temp->idModelRevMajor);
Serial.print(".");
Serial.println(temp->idModelRevMinor);
Serial.print("Module Rev = ");
Serial.print(temp->idModuleRevMajor);
Serial.print(".");
Serial.println(temp->idModuleRevMinor);
Serial.print("Manufacture Date = ");
Serial.print((temp->idDate >> 3) & 0x001F);
Serial.print("/");
Serial.print((temp->idDate >> 8) & 0x000F);
Serial.print("/1");
Serial.print((temp->idDate >> 12) & 0x000F);
Serial.print(" Phase: ");
Serial.println(temp->idDate & 0x0007);
Serial.print("Manufacture Time (s)= ");
Serial.println(temp->idTime * 2);
Serial.println();
Serial.println();
}
이걸 했을 때 값이
이렇게 나오구요.
[코드 2]
#include
#include
#include // Wire.h 호출 I2C통신 관련 함수
#include // VL6180X.h 호출 VL6180 관련 함수
VL6180X sensor;
int LedPin1 = 7; // 1번 LED의 핀 번호
int LedPin2 = 6; // 2번 LED의 핀 번호
int LedPin3 = 5; // 3번 LED의 핀 번호
int LedPin4 = 4; // 4번 LED의 핀 번호
int LedPin5 = 3; // 5번 LED의 핀 번호
int LedPin6 = 2; // 6번 LED의 핀 번호
void setup()
{
Serial.begin(9600); // Serial 모니터 9600 보드레이트 설정
Wire.begin(); // I2C 통신 초기화
sensor.init(); // VL61480 센서 초기화 함수 호출
sensor.configureDefault(); //센서 기본 설정 함수 호출
sensor.setTimeout(500); // 센서가 동작 준비되지 않은 경우 대기시간 설정
}
void loop()
{
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }// 마지막 호출 이후에 읽기 시간 초과가 발생 했는지 여부를 확인
int range = sensor.readRangeSingleMillimeters(); // 센서 데이터호출 후 int 형 range에 저장
Serial.print("range : ");Serial.print(range); // Serial 모니터에 출력 "range : 데이터 값 "
Serial.println(); // Serial 입력줄 내리기
}
코드를 이렇게 했을 때
이게 무한 반복 되고 있습니다.
아두이노 초보라 어떻게 해야할 지 감이 안 잡히네요.
혹시 방법을 아신다면 도움 부탁드립니다!!
댓글 3
조회수 17,671master님의 댓글
master 작성일
아두이노용 예제가 있지 않나요?
라이브러리 설치하면 예제도 함께 설치됩니다.
//
I2C는 적당한 풀업저항 사용해야 합니다.
통신속도도 지나치게 빠르게 하면 안됩니다.
적당히 느린 속도로 테스트 하세요 (예제소스의 속도 그대로 테스트하면 됩니다.)
탈창곽님의 댓글
탈창곽 작성일
예제도 해봤어요..
/******************************************************************************
* SparkFun_VL6180X_demo.ino
* Example Sketch for VL6180x time of flight range finder.
* Casey Kuhns @ SparkFun Electronics
* 10/29/2014
* https://github.com/sparkfun/SparkFun_ToF_Range_Finder-VL6180_Arduino_Library
*
* The VL6180x by ST micro is a time of flight range finder that
* uses pulsed IR light to determine distances from object at close
* range. The average range of a sensor is between 0-200mm
*
* Resources:
* This library uses the Arduino Wire.h to complete I2C transactions.
*
* Development environment specifics:
* IDE: Arduino 1.0.5
* Hardware Platform: Arduino Pro 3.3V/8MHz
* VL6180x Breakout Version: 1.0
* **Updated for Arduino 1.6.4 5/2015**
*
* This code is beerware. If you see me (or any other SparkFun employee) at the
* local pub, and you've found our code helpful, please buy us a round!
*
* Distributed as-is; no warranty is given.
******************************************************************************/
#include <Wire.h>
#include <SparkFun_VL6180X.h>
/*const float GAIN_1 = 1.01; // Actual ALS Gain of 1.01
const float GAIN_1_25 = 1.28; // Actual ALS Gain of 1.28
const float GAIN_1_67 = 1.72; // Actual ALS Gain of 1.72
const float GAIN_2_5 = 2.6; // Actual ALS Gain of 2.60
const float GAIN_5 = 5.21; // Actual ALS Gain of 5.21
const float GAIN_10 = 10.32; // Actual ALS Gain of 10.32
const float GAIN_20 = 20; // Actual ALS Gain of 20
const float GAIN_40 = 40; // Actual ALS Gain of 40
*/
#define VL6180X_ADDRESS 0x29
VL6180xIdentification identification;
VL6180x sensor(VL6180X_ADDRESS);
void setup() {
Serial.begin(115200); //Start Serial at 115200bps
Wire.begin(); //Start I2C library
delay(100); // delay .1s
sensor.getIdentification(&identification); // Retrieve manufacture info from device memory
printIdentification(&identification); // Helper function to print all the Module information
if(sensor.VL6180xInit() != 0){
Serial.println("FAILED TO INITALIZE"); //Initialize device and check for errors
};
sensor.VL6180xDefautSettings(); //Load default settings to get started.
delay(1000); // delay 1s
}
void loop() {
//Get Ambient Light level and report in LUX
Serial.print("Ambient Light Level (Lux) = ");
//Input GAIN for light levels,
// GAIN_20 // Actual ALS Gain of 20
// GAIN_10 // Actual ALS Gain of 10.32
// GAIN_5 // Actual ALS Gain of 5.21
// GAIN_2_5 // Actual ALS Gain of 2.60
// GAIN_1_67 // Actual ALS Gain of 1.72
// GAIN_1_25 // Actual ALS Gain of 1.28
// GAIN_1 // Actual ALS Gain of 1.01
// GAIN_40 // Actual ALS Gain of 40
Serial.println( sensor.getAmbientLight(GAIN_1) );
//Get Distance and report in mm
Serial.print("Distance measured (mm) = ");
Serial.println( sensor.getDistance() );
delay(500);
};
void printIdentification(struct VL6180xIdentification *temp){
Serial.print("Model ID = ");
Serial.println(temp->idModel);
Serial.print("Model Rev = ");
Serial.print(temp->idModelRevMajor);
Serial.print(".");
Serial.println(temp->idModelRevMinor);
Serial.print("Module Rev = ");
Serial.print(temp->idModuleRevMajor);
Serial.print(".");
Serial.println(temp->idModuleRevMinor);
Serial.print("Manufacture Date = ");
Serial.print((temp->idDate >> 3) & 0x001F);
Serial.print("/");
Serial.print((temp->idDate >> 8) & 0x000F);
Serial.print("/1");
Serial.print((temp->idDate >> 12) & 0x000F);
Serial.print(" Phase: ");
Serial.println(temp->idDate & 0x0007);
Serial.print("Manufacture Time (s)= ");
Serial.println(temp->idTime * 2);
Serial.println();
Serial.println();
}
위 예제로 했구요. 결과는
FAILED TO INITALIZE
Model ID = 255
Model Rev = 255.255
Module Rev = 255.255
Manufacture Date = 31/15/115 Phase: 7
Manufacture Time (s)= 65534
FAILED TO INITALIZE
Ambient Light Level (Lux) = 31.68
Distance measured (mm) = 255
Ambient Light Level (Lux) = 31.68
Distance measured (mm) = 255
Ambient Light Level (Lux) = 31.68
Distance measured (mm) = 255
Ambient Light Level (Lux) = 31.68
Distance measured (mm) = 255
Ambient Light Level (Lux) = 31.68
Distance measured (mm) = 255
Ambient Light Level (Lux) = 31.68
Distance measured (mm) = 255
Ambient Light Level (Lux) = 31.68
Distance measured (mm) = 255
Ambient Light Level (Lux) = 31.68
(생략)
이런 식으로 똑같은 값만 계속 나오고 있습니다.
master님의 댓글
master
오실로스코프를 사용해서 정상적으로 데이터가 얻어지는지 체크해보세요