Enthernet Code
Enthernet Code
DIIDevHeads IoT Integration Server
Created by Boss lady on 8/5/2024 in #firmware-and-baremetal
Setting Up HC-SR04 Ultrasonic Sensor and 16x2 LCD on BeagleBone Black
@Boss lady Adding to @RED HAT points, here's a sample script to help you get started: updating and installing Libraries
sudo apt-get update
sudo apt-get install python3-pip
pip3 install Adafruit_BBIO
pip3 install RPLCD
sudo apt-get update
sudo apt-get install python3-pip
pip3 install Adafruit_BBIO
pip3 install RPLCD
writing script
import Adafruit_BBIO.GPIO as GPIO
import time
from RPLCD.gpio import CharLCD

TRIG = "P9_12"
ECHO = "P9_15"
LCD_RS = "P8_11"
LCD_RW = "P8_12"
LCD_E = "P8_13"
LCD_D4 = "P8_14"
LCD_D5 = "P8_15"
LCD_D6 = "P8_16"
LCD_D7 = "P8_17"

GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
lcd = CharLCD(pin_rs=LCD_RS, pin_rw=LCD_RW, pin_e=LCD_E,
pins_data=[LCD_D4, LCD_D5, LCD_D6, LCD_D7],
numbering_mode=GPIO.BOARD)

def measure_distance():
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)

while GPIO.input(ECHO) == 0:
pulse_start = time.time()

while GPIO.input(ECHO) == 1:
pulse_end = time.time()

pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)

return distance

try:
while True:
dist = measure_distance()
lcd.clear()
lcd.write_string("Distance: {} cm".format(dist))
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
import Adafruit_BBIO.GPIO as GPIO
import time
from RPLCD.gpio import CharLCD

TRIG = "P9_12"
ECHO = "P9_15"
LCD_RS = "P8_11"
LCD_RW = "P8_12"
LCD_E = "P8_13"
LCD_D4 = "P8_14"
LCD_D5 = "P8_15"
LCD_D6 = "P8_16"
LCD_D7 = "P8_17"

GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
lcd = CharLCD(pin_rs=LCD_RS, pin_rw=LCD_RW, pin_e=LCD_E,
pins_data=[LCD_D4, LCD_D5, LCD_D6, LCD_D7],
numbering_mode=GPIO.BOARD)

def measure_distance():
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)

while GPIO.input(ECHO) == 0:
pulse_start = time.time()

while GPIO.input(ECHO) == 1:
pulse_end = time.time()

pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)

return distance

try:
while True:
dist = measure_distance()
lcd.clear()
lcd.write_string("Distance: {} cm".format(dist))
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
12 replies