본문 바로가기

개발자/Arduino

수심 자동 측정 장치 JSN-SR04T

반응형

 

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

 

JSN-SR04T Waterproof Ultrasonic 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); 
}

 

 

 

반응형

캐어랩 고객 지원

취업, 창업의 막막함, 외주 관리, 제품 부재!

당신의 고민은 무엇입니까? 현실과 동떨어진 교육, 실패만 반복하는 외주 계약, 아이디어는 있지만 구현할 기술이 없는 막막함.

우리는 알고 있습니다. 문제의 원인은 '명확한 학습, 실전 경험과 신뢰할 수 있는 기술력의 부재'에서 시작됩니다.

이제 고민을 멈추고, 캐어랩을 만나세요!

코딩(펌웨어), 전자부품과 디지털 회로설계, PCB 설계 제작, 고객(시장/수출) 발굴과 마케팅 전략으로 당신을 지원합니다.

제품 설계의 고수는 성공이 만든 게 아니라 실패가 만듭니다. 아이디어를 양산 가능한 제품으로!

귀사의 제품을 만드세요. 교육과 개발 실적으로 신뢰할 수 있는 파트너를 확보하세요.

지난 30년 여정, 캐어랩이 얻은 모든 것을 함께 나누고 싶습니다.

카카오 채널 추가하기

카톡 채팅방에서 무엇이든 물어보세요

당신의 성공을 위해 캐어랩과 함께 하세요.

캐어랩 온라인 채널 바로가기

캐어랩