반응형
JSN-SR04T Waterproof Ultrasonic Module
방수초음파센서 모듈에 대한 설명은 이전 포스팅을 참고하시고 여기서는 예제코드를 알아봅시다. 회로도와 구성도는 그리고 있어요.
Ultrasonic Sensors: Comparison and Test (US42V2, JSN-SR04T, and US-100)
방수 초음파 모듈 JSN-SR04T 테스트 - 20cm 이하 측정 불가하다. 이런~
방수 초음파 모듈 JSN-SR04T - Water Proof Integrated Ultrasonic Ranging Module
계산 방식이 다른 두 가지 코드를 첨부합니다. 거리 계산에서 중요한 아두이노 함수 pulseIn 함수 설명을 참고하세요.
코드 1
#define ECHOPIN 11 // Pin to receive echo pulse
#define TRIGPIN 12 // Pin to send trigger pulse
void setup()
{
Serial.begin(9600);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
digitalWrite(ECHOPIN, HIGH);
}
void loop()
{
digitalWrite(TRIGPIN, LOW); // Set the trigger pin to low for 2uS
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH); // Send a 10uS high to trigger ranging
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW); // Send pin low again
int distance = pulseIn(ECHOPIN, HIGH, 26000); // Read in times pulse 펄스 시작을 기다릴 시간 (마이크로초 단위)
distance= distance/58;
Serial.print(distance);
Serial.println(" cm");
delay(300);// Wait 50mS before next ranging
}
코드 2 - 코드의 출처를 참고하여 상세한 코드의 작동 설명를 참고하세요.
/* Arduino example sketch to control a JSN-SR04T ultrasonic distance sensor with Arduino. No library needed. More info: https://www.makerguides.com */
// Define Trig and Echo pin:
#define trigPin 2
#define echoPin 3
// Define variables:
long duration;
int distance;
void setup()
{
// Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Begin Serial communication at a baudrate of 9600:
Serial.begin(9600);
}
void loop()
{
// Clear the trigPin by setting it LOW:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
// Trigger the sensor by setting the trigPin high for 10 microseconds:
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin. pulseIn() returns the duration (length of the pulse) in microseconds:
duration = pulseIn(echoPin, HIGH);
// Calculate the distance:
distance = duration*0.034/2;
// Print the distance on the Serial Monitor (Ctrl+Shift+M):
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
delay(100);
}
초음파 센서 여러개 사용할 때 코드 참고하세요. 코드 출처와 상세한 설명링크
코드 3
#define TRIG_1 8
#define TRIG_2 9
#define TRIG_3 10
#define ECHO_1 11
#define ECHO_2 12
#define ECHO_3 13
void setup()
{
for(int i=8;i<=10;i++) {
pinMode(i,OUTPUT);
}
for(int i=11;i<=13;i++) {
pinMode(i,INPUT);
}
}
int distance1,distance2,distance3;
void loop()
{
digitalWrite(TRIG_1,HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_1,LOW);
distance1 = pulseIn(ECHO_1,HIGH)/58.2;
delayMicroseconds(2);
digitalWrite(TRIG_2,HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_2,LOW);
distance2 = pulseIn(ECHO_2,HIGH)/58.2;
delayMicroseconds(2);
digitalWrite(TRIG_3,HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_3,LOW);
distance3 = pulseIn(ECHO_3,HIGH)/58.2;
delayMicroseconds(2);
}
반응형
'개발자 > Arduino' 카테고리의 다른 글
nano 33 IoT 보드로 구현한 스마트 가든 예제 (0) | 2020.09.21 |
---|---|
Arduino Nano 33 IoT 개발 가이드 1-1 Blink Test (0) | 2020.09.18 |
컴퓨터에서 직접 Arduino 제어하는 Firmata 사용법 (3) | 2020.09.17 |
Arduino Nano 33 IoT - Ultimate Guide (0) | 2020.09.07 |
리튬 배터리 충전 모듈 TP4056 사용법 (1) | 2020.07.20 |
프로젝트 참고 TinyML Application Development for Everyone (0) | 2020.05.22 |
Arduino Nano 33 IoT 개발 가이드 7 - Bluetooth Low Energy (BLE) (2) | 2020.05.15 |
Arduino Nano 33 IoT 개발 가이드 6 - Accelerator and Gyroscope (0) | 2020.05.15 |
더욱 좋은 정보를 제공하겠습니다.~ ^^