Setting Up HC-SR04 Ultrasonic Sensor and 16x2 LCD on BeagleBone Black

hello guys, I'm developing an obstacle detection system using a BeagleBone Black running Embedded Linux. The system uses an HC-SR04 ultrasonic sensor to measure distances and displays the measurements on a 16x2 LCD screen. How do I set up the BeagleBone Black with an HC-SR04 ultrasonic sensor and a 16x2 LCD screen? any materials or guides would be helpful
11 Replies
UC GEE
UC GEE2mo ago
@Boss lady Try connecting your HC-SR04 sensor's VCC pin to the BeagleBone Black's 5V pin.Then, to manage the voltage level shift from 5V to 3.3V, try adding a 1kΩ resistor between the Echo pin and the BeagleBone Black GPIO pin. Next, with relation to the 16 x 2 LCD screen, join the VSS pin of the screen to a BeagleBone Black ground pin.
Boss lady
Boss lady2mo ago
please can u be more detailed or suggest to me a material guide or link @UC GEE
RED HAT
RED HAT2mo ago
That sounds cool. To get started, you'll need to interface both the HC-SR04 sensor and the 16x2 LCD screen with the BeagleBone Black. These are some steps to guide you Wiring the HC-SR04 Sensor: Connect the VCC pin of the HC-SR04 to the 5V pin on the BeagleBone Black. Connect the GND pin to a ground (GND) pin on the BeagleBone Black. Connect the Trig pin to a GPIO pin on the BeagleBone Black (e.g., P9_12). Connect the Echo pin to another GPIO pin (e.g., P9_15). Wiring the 16x2 LCD Screen: Connect the VCC pin of the LCD to the 5V pin on the BeagleBone Black. Connect the GND pin to a ground (GND) pin on the BeagleBone Black. Connect the RS, RW, E, and data pins (D4-D7) to GPIO pins on the BeagleBone Black (e.g., P8_11, P8_12, P8_13, P8_14, P8_15, P8_16, P8_17). Libraries and Software: Install necessary libraries for GPIO control and LCD display. Write a Python script to control the HC-SR04 and LCD.
Enthernet Code
Enthernet Code2mo ago
@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()
Boss lady
Boss lady2mo ago
Thank you, @Enthernet Code and @RED HAT This is really helpful. I'll follow these steps and try the script. I'll let you know if I run into any issues
Boss lady
Boss lady2mo ago
hello guys, based on my previous question on developing an obstacle detection system using a BeagleBone Black running Embedded Linux using HC-SR04 ultrasonic sensor to measure distances and displays the measurements on a 16x2 LCD screen. am encoutering a situation where the LCD screen remains blank and does not display any readings, and i have verified the lcd is powered correctly, i have also checked lcd initialization
python
lcd = CharLCD(pin_rs=LCD_RS, pin_e=LCD_E, pins_data=[LCD_D4, LCD_D5, LCD_D6, LCD_D7], numbering_mode=GPIO.BOARD)
python
lcd = CharLCD(pin_rs=LCD_RS, pin_e=LCD_E, pins_data=[LCD_D4, LCD_D5, LCD_D6, LCD_D7], numbering_mode=GPIO.BOARD)
but still my lcd screen remains blank
RED HAT
RED HAT2mo ago
@Boss lady re-check all your connections to ensure the RS, E, and data pins (D4-D7) are correctly connected to the BeagleBone Black GPIO pins. Make sure that the contrast pin (often labeled V0) is correctly set. Sometimes the contrast needs to be adjusted to see anything on the LCD, also ensure the LCD initialization sequence is correct. Sometimes small mistakes in initialization can cause the display to remain blank, try testing the LCD Use to test print hello world on LCD screen
import Adafruit_BBIO.GPIO as GPIO
from RPLCD.gpio import CharLCD
import time

LCD_RS = "P8_11"
LCD_E = "P8_12"
LCD_D4 = "P8_13"
LCD_D5 = "P8_14"
LCD_D6 = "P8_15"
LCD_D7 = "P8_16"

lcd = CharLCD(pin_rs=LCD_RS, pin_e=LCD_E, pins_data=[LCD_D4, LCD_D5, LCD_D6, LCD_D7], numbering_mode=GPIO.BOARD)

lcd.clear()
lcd.write_string("Hello, World!")
time.sleep(5)
lcd.clear()
import Adafruit_BBIO.GPIO as GPIO
from RPLCD.gpio import CharLCD
import time

LCD_RS = "P8_11"
LCD_E = "P8_12"
LCD_D4 = "P8_13"
LCD_D5 = "P8_14"
LCD_D6 = "P8_15"
LCD_D7 = "P8_16"

lcd = CharLCD(pin_rs=LCD_RS, pin_e=LCD_E, pins_data=[LCD_D4, LCD_D5, LCD_D6, LCD_D7], numbering_mode=GPIO.BOARD)

lcd.clear()
lcd.write_string("Hello, World!")
time.sleep(5)
lcd.clear()
Boss lady
Boss lady2mo ago
Hello @RED HAT ahv tried this but thr LCD screen still remains blank
Enthernet Code
Enthernet Code2mo ago
@Boss lady then this might be an eletric issue or wrong connection
Enthernet Code
Enthernet Code2mo ago
check if the LCD is getting a stable 5V power supply and check the potentiometer connections and settings. Sometimes incorrect settings can cause the display to remain blank.
RED HAT
RED HAT2mo ago
@Boss lady make sure there is enough delay during the initialization process. LCD's sometimes need a bit more time to initialize properly. Try adding a delay before writing to the LCD:
import Adafruit_BBIO.GPIO as GPIO
from RPLCD.gpio import CharLCD
import time

LCD_RS = "P8_11"
LCD_E = "P8_12"
LCD_D4 = "P8_13"
LCD_D5 = "P8_14"
LCD_D6 = "P8_15"
LCD_D7 = "P8_16"

lcd = CharLCD(pin_rs=LCD_RS, pin_e=LCD_E, pins_data=[LCD_D4, LCD_D5, LCD_D6, LCD_D7], numbering_mode=GPIO.BOARD)

time.sleep(1)
lcd.clear()
lcd.write_string("Hello, World!")
time.sleep(5)
lcd.clear()
import Adafruit_BBIO.GPIO as GPIO
from RPLCD.gpio import CharLCD
import time

LCD_RS = "P8_11"
LCD_E = "P8_12"
LCD_D4 = "P8_13"
LCD_D5 = "P8_14"
LCD_D6 = "P8_15"
LCD_D7 = "P8_16"

lcd = CharLCD(pin_rs=LCD_RS, pin_e=LCD_E, pins_data=[LCD_D4, LCD_D5, LCD_D6, LCD_D7], numbering_mode=GPIO.BOARD)

time.sleep(1)
lcd.clear()
lcd.write_string("Hello, World!")
time.sleep(5)
lcd.clear()
Want results from more Discord servers?
Add your server