개발자/Arduino

방수 초음파 모듈 JSN-SR04T 테스트 - 20cm 이하 측정 불가능

지구빵집 2018. 2. 6. 15:35
반응형

 

 

 

방수 초음파 모듈 JSN-SR04T 테스트 - 20cm 이하 측정을 못하네.

 

방수 초음파 센서로 만들게 있어서 테스트 중인데 거리가 20cm 이하로 측정을 못하는 사실을 확인했다. 하~ 이런 $@#%@#^$^%&*^&(*&^&

 

또 다른 방수 초음파 센서 Weather-proof Ultrasonic Sensor SKU : SEN0207 도 찾아밨는데 결론은 마찬가지다. 20~25cm 가 최저 범위가 된다. 참고 사이트 https://www.dfrobot.com/wiki/index.php/Weather-proof_Ultrasonic_Sensor_SKU_:_SEN0207

 

방수 초음파 모듈 JSN-SR04T 센서에 대한 자세한 정보와 헤더파일 자료는 얼마전 포스팅한 사이트를 참고한다.

 

방수 초음파 모듈 JSN-SR04T - Water Proof Integrated Ultrasonic Ranging Module  설명 -  http://fishpoint.tistory.com/2582

 

연결은 사진을 참고하여 아래와 같이 연결한다. 

 

JSN-SR04T 센서  -  Arduino Nano 보드

VCC - VCC

GND - GND

Trigger -  12

Echo - 11

 

 

소스코드 1

이건 중간 중간 값이 0 으로 출력되는 코드. 아래 코드는 NewPing 라이브러리를 사용한다. 맨위에 <NewPing.h> 파일이 인클루드 되어있다. 이 라이브러리는 위 설명 사이트에 가서 다운 받아서 압푹을 풀고 NewPing 폴더를 아두이노가 설치된 폴더의 Libraries 폴더 아래에 설치하고 실행해야 한다. 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// jsn-sr04T
// ---------------------------------------------------------------------------
 
#include <NewPing.h>
 
#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 450 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
 
void setup() {
  Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
 
 
}
 
void loop() {
  delay(500);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
}
cs

 

 

위 코드보다 정확한 코드를 아래에 나타낸다. 0 값이 안나오고 비교적 잘 동작한다. 라이브러리도 필요없는 코드다. 역시 20cm 이하의 범위는 읽지 못한다. 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// ---------------------------------------------------------------------------
// Example - other Code.
// jsn-sr04T
// www.diymaker.net
// ---------------------------------------------------------------------------
 
#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
}
cs

 

왜 20cm 보다 못하나 여러곳을 찾아 봤지만 핑을 쏘고 받는 트랜시버와 리시버가 하나로 되어 있기 때문이란 답만 얻었다.  ㅠ.ㅠ

 

Test 결과화면 

 

 

 

20cm 이하까지 나오는 방수 초음파 센서를 찾든가, 그냥 진행하든가 해야겠다. 나중에 포스팅을 더 하든가.

 

참고로 같은 센서를 가지고 라즈베리 파이에서 돌아가는 파이썬 코드를 참고로 올려둔다.

 

Measuring distance with JSN-SR04T and Raspberry

Distance sensor JSN-SR04T  - 코드 설명 출처 - http://www.bambusekd.cz/dev/raspberry-measure-distance-JSN-SR04T

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python
 
# Import required Python libraries
import time               # library for time reading time
import RPi.GPIO as GPIO   # library to controll Rpi GPIOs
 
def main():
  # We will be using the BCM GPIO numbering
  GPIO.setmode(GPIO.BCM)
 
  # Select which GPIOs you will use
  GPIO_TRIGGER = 23
  GPIO_ECHO    = 24
 
  # Set TRIGGER to OUTPUT mode
  GPIO.setup(GPIO_TRIGGER,GPIO.OUT)
  # Set ECHO to INPUT mode
  GPIO.setup(GPIO_ECHO,GPIO.IN)
 
  # Set TRIGGER to LOW
  GPIO.output(GPIO_TRIGGER, False)
 
  # Let the sensor settle for a while
  time.sleep(0.5)
 
  # Send 10 microsecond pulse to TRIGGER
  GPIO.output(GPIO_TRIGGER, True) # set TRIGGER to HIGH
  time.sleep(0.00001# wait 10 microseconds
  GPIO.output(GPIO_TRIGGER, False) # set TRIGGER back to LOW
 
  # Create variable start and give it current time
  start = time.time()
  # Refresh start value until the ECHO goes HIGH = until the wave is send
  while GPIO.input(GPIO_ECHO)==0:
    start = time.time()
  # Assign the actual time to stop variable until the ECHO goes back from HIGH to LOW
  while GPIO.input(GPIO_ECHO)==1:
    stop = time.time()
 
  # Calculate the time it took the wave to travel there and back
  measuredTime = stop - start
  # Calculate the travel distance by multiplying the measured time by speed of sound
  distanceBothWays = measuredTime * 33112 # cm/s in 20 degrees Celsius
  # Divide the distance by 2 to get the actual distance from sensor to obstacle
  distance = distanceBothWays / 2
  # Print the distance
  print("Distance : {0:5.1f}cm".format(distance))
 
  # Reset GPIO settings
  GPIO.cleanup()
 
# Run the main function when the script is executed
if __name__ == "__main__":
    main()
 
 
cs

  

사진없으면 섭섭하니 작업 사진 몇 장 ^^

 

 

 

 

 

 

 

 

 

 

 

반응형