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

wiringpi.wiringPiSetup()

TRIG = 4
TRIG_RX = 4
ECHO_TX = 3

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)
    time.sleep(2)

    wiringpi.digitalWrite(TRIG_RX, GPIO.HIGH)
    time.sleep(0.001)
    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")
    else:
        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.