Today: June 13, 2024 9:30 am
A collection of Software and Cloud patterns with a focus on the Enterprise

Ultrasonic Distance Measurement on Orange pi zero 2 W

Recently I decided to give the Orange pi zero 2 W a try, and one of the experiments I decided to run was use an Ultrasonic Distance Measurement based on the Waterproof Ultrasonic Module JSN-SR04T. The code provided is for the Raspberry pi and assumes RPi.GPIO is available, which it isn’t on the Orange pi zero 2 W.

It’s important to note that the wiring is identical for the Orange pi zero 2 W and the Raspberry pi zero 2 W. This means you use the same pins. The wiring diagram shown is exactly what I used.

The Orange pi uses wiringpi for python. The following code runs on my Orange pi

import time
import wiringpi
from wiringpi import GPIO


TRIG = 4

wiringpi.pinMode(TRIG_RX, GPIO.OUTPUT)
wiringpi.pinMode(ECHO_TX, GPIO.INPUT)
wiringpi.digitalWrite(TRIG_RX, GPIO.LOW)

while True:
    wiringpi.digitalWrite(TRIG_RX, GPIO.LOW)

    wiringpi.digitalWrite(TRIG_RX, GPIO.HIGH)
    wiringpi.digitalWrite(TRIG_RX, GPIO.LOW)

    while wiringpi.digitalRead(ECHO_TX)==GPIO.LOW:
        pulse_start = time.time()

    while wiringpi.digitalRead(ECHO_TX)==GPIO.HIGH:
        pulse_end = time.time()

    pulse_duration = pulse_end - pulse_start

    distance = pulse_duration * 17150
    distance = round(distance, 2)
    if distance > 20 and distance < 400:
        print("Distance:",distance - 0.5,"cm")
        print("Out Of Range")

Recommended Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.