2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ラズベリーパイで距離測定センサーを作る

Posted at

作りたいもの

  • 距離計測センサー

使用したもの

  • ラズベリーパイ
  • HC-SR04

コード

sensor.py
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time

# HIGH or LOWの時計測
def pulseIn(PIN):

    while GPIO.input(PIN) == 0:
        t_start = time.time()

    while GPIO.input(PIN) == 1:
        t_end = time.time()

    return t_end - t_start

# 距離計測
def calc_distance(TRIG_PIN, ECHO_PIN, num, v=34000):
    for i in range(num):

        # TRIGピンを0.3[s]だけLOW
        GPIO.output(TRIG_PIN, GPIO.LOW)
        time.sleep(0.3)

        # TRIGピンを0.00001[s]だけ出力(超音波発射)
        GPIO.output(TRIG_PIN, True)
        time.sleep(0.00001)
        GPIO.output(TRIG_PIN, False)

        # HIGHの時間計測
        t = pulseIn(ECHO_PIN)

        # 距離[cm] = 音速[cm/s] * 時間[s]/2
        distance = v * t/2
        print(distance, "cm")

    # ピン設定解除
    GPIO.cleanup()


# TRIGとECHOのGPIO番号
TRIG_PIN = 14
ECHO_PIN = 15

# 音速[cm/s]
v = 34000

# ピン番号をGPIOで指定
GPIO.setmode(GPIO.BCM)

# TRIG_PINを出力, ECHO_PINを入力
GPIO.setup(TRIG_PIN,GPIO.OUT)
GPIO.setup(ECHO_PIN,GPIO.IN)
GPIO.setwarnings(False)

# 距離計測(TRIGピン番号, ECHO_PIN番号, 計測回数, 音速[cm/s])
calc_distance(TRIG_PIN, ECHO_PIN, 10, v)

参考サイト

【ラズベリーパイ3】Pythonで超音波距離センサ(HC-SR04)の精度向上(気温考慮)

参考サイトから修正した部分

  • pulseIn関数の中身
  • 音速を34000に固定

疑問

参考サイトにある以下の部分の、start、end、t_start、t_endの役割がわからなかった。

# HIGH or LOWの時計測
def pulseIn(PIN, start=1, end=0):
    if start==0: end = 1
    t_start = 0
    t_end = 0
    # ECHO_PINがHIGHである時間を計測
    while GPIO.input(PIN) == end:
        t_start = time.time()
        
    while GPIO.input(PIN) == start:
        t_end = time.time()
    return t_end - t_start
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?