For Ward
Published © GPL3+

BeagleV-Fire, O-Scope, and LED (Testing 1...2)

Can you decipher exactly what it is I am seeing in my project? It is a square wave but what exactly does it mean?

BeginnerProtip1 hour9
BeagleV-Fire, O-Scope, and LED (Testing 1...2)

Things used in this project

Hardware components

BeagleV-Fire
×1
LED (generic)
LED (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Ethernet Cable, Cat6a
Ethernet Cable, Cat6a
×1
oscilloscope
×1
a probe for your oscilloscope/Channel 1 Only
×1
USB Cable, USB Type C Plug
USB Cable, USB Type C Plug
×1
Resistor 1k ohm
Resistor 1k ohm
×1
Silicon Labs CP210x USB to UART Bridge
×1

Software apps and online services

bb-imager
Debian Trixie from beagleboard.org
PWM Testing

Story

Read more

Code

Random, Needing a Debug Source Code

Python
#!/usr/bin/python3

"""
This Code uses the:
* Maxbotic LV-EZ2 Ultrasonic Sensor

Tested with the BBBW 
This sketch reads the LV-EZ2 by pulse count
Then prints the distance to the console

The circuit:
* 3.3v on the BBBW to LV-EZ2 Pin 7 Vcc
* GND on BBBW to the LV-EZ2 Pin 6 GND 
* LV-EZ2 Ultrasonic Sensor PW pin to BBBW P9_14
* LV-EZ2 Sensor PWM usage!
"""

from time import sleep
import sys

PWM_E = "/sys/class/pwm/pwmchip0/enable"
PWM_P = "/sys/class/pwm/pwmchip0/period"
PWM_D = "/sys/class/pwm/pwmchip0/duty_cycle"

PWM_E == "0"
PWM_P == "25000000"
PWM_D == "7500000"

# calculated mode or median distance
mode_result = 0

# storing multiple pulses
# read in time for pin to transition
samples = 9
pulses = (PWM_E = "1", len==samples)

# sensor reads which are in range will be stored here
rangevalue = [0, 0, 0, 0, 0, 0, 0, 0, 0]

# 1s sensor power up pause
sleep(1)


def tof_cm(time_of_flight):
    """
    EZ1 ultrasonic sensor is measuring "time of flight"
    Converts time of flight into distance in centimeters
    """
    convert_to_cm = 58
    cm = time_of_flight / convert_to_cm

    return cm


def tof_inches(time_of_flight):
    """
    EZ1 ultrasonic sensor is measuring "time of flight"
    Converts time of flight into distance in inches
    """
    convert_to_inches = 147
    inches = time_of_flight / convert_to_inches

    return inches


def find_mode(x):
    """
    find the mode (most common value reported)
    will return median (center of sorted list)
    should mode not be found
    """
    n = len(x)

    max_count = 0
    mode = 0
    bimodal = 0
    counter = 0
    index = 0

    while index < (n - 1):
        prev_count = counter
        counter = 0

        while (x[index]) == (x[index + 1]):
            counter += 1
            index += 1

        if (counter > prev_count) and (counter > max_count):
            mode = x[index]
            max_count = counter
            bimodal = 0

        if counter == 0:
            index += 1

        # If the dataset has 2 or more modes.
        if counter == max_count:
            bimodal = 1

        # Return the median if there is no mode.
        if (mode == 0) or (bimodal == 1):
            mode = x[int(n / 2)]

        return mode

try:
    while True:

        # wait between samples
        sleep(.5)

        if len(pulses) == samples:
            j = 0  # rangevalue array counter

            # only save the values within range
            # range readings take 49mS
            # pulse width is .88mS to 37.5mS
            for i in range(0, samples):
                tof = pulses[i]  # time of flight - PWM HIGH

                if 880 < tof < 37500:
                    if j < len(rangevalue):
                        rangevalue[j] = tof_cm(tof)
                        j += 1

            # clear pulse samples
            pulses.clear()  # clear all values in pulses[]

            # sort samples
            rangevalue = sorted(rangevalue)

            # returns mode or median
            mode_result = int(find_mode(rangevalue))

            # python console prints both centimeter and inches distance
            cm2in = .393701
            mode_result_in = mode_result * cm2in
            print(mode_result, "cm", "\t\t", int(mode_result_in), "in")

            # result must be in char/string format for LCD printing
            # digit_string = str(mode_result)

            sleep(2)
except KeyboardInterrupt:
    PWM_E == "0"
    print("Yep and then some...")
    pass

Credits

For Ward
8 projects • 1 follower
Yeppers, one day...one day.
Thanks to a fellow on the forums at forum.beagleboard.org.

Comments