I’m stuck, I have an ultrasonic distance sensor and two motion sensors with a level jump because both work at 5v and I pass it to 3v3, I have put its resistance on the level jump, with gpiod in python I can’t receive any signal, apparently everything is fine, can someone help me?
Gpod no funcion
please show me the sensor link and your code so that I can help.
#!/usr/bin/env python3
SPDX-License-Identifier: GPL-2.0-or-later
SPDX-FileCopyrightText: 2023 Kent Gibson warthog618@gmail.com
import gpiod
from gpiod.line import Direction, Value
import time
def medir_distancia(chip_path, trig_offset, echo_offset):
# Solicitar la línea TRIG como salida y ECHO como entrada
with gpiod.request_lines(
chip_path,
consumer=“ultrasonic-sensor”,
config={
trig_offset: gpiod.LineSettings(direction=Direction.OUTPUT, output_value=Value.INACTIVE),
echo_offset: gpiod.LineSettings(direction=Direction.INPUT)
}
) as request:
while True:
# Generar un pulso en TRIG
request.set_values({trig_offset: Value.INACTIVE})
time.sleep(0.002) # Pausa para estabilizar
request.set_values({trig_offset: Value.ACTIVE}) # Pulso alto (10 µs)
time.sleep(0.00001)
request.set_values({trig_offset: Value.INACTIVE}) # Regresar a bajo
# Inicializar las variables de tiempo
start_time = 0
end_time = 0
# Medir el tiempo en ECHO
timeout = time.time() + 1 # Configura un tiempo límite de 1 segundo para evitar bucles infinitos
while request.get_value(echo_offset) == 0: # Espera señal alta en ECHO
start_time = time.time()
if time.time() > timeout:
print("Error: Tiempo de espera agotado para señal alta en ECHO.")
break
while request.get_value(echo_offset) == 1: # Medir tiempo mientras ECHO está alto
end_time = time.time()
if time.time() > timeout:
print("Error: Tiempo de espera agotado para señal baja en ECHO.")
break
if start_time and end_time and end_time > start_time:
# Calcular la distancia en base a la duración del pulso
duration = end_time - start_time
distance = (duration * 34300) / 2 # Velocidad del sonido en cm/s
print(f"Distancia: {distance:.2f} cm")
else:
print("Error: Señal ECHO no detectada correctamente.")
# Pausa antes de la siguiente medición
time.sleep(1)
if name == “main”:
try:
medir_distancia("/dev/gpiochip4", 21, 19) # TRIG en línea 21, ECHO en línea 19
except Exception as e:
print(f"Ha ocurrido un error: {e}")
Other example
#!/usr/bin/env python3
SPDX-License-Identifier: GPL-2.0-or-later
SPDX-FileCopyrightText: 2023 Kent Gibson warthog618@gmail.com
import gpiod
from gpiod.line import Direction, Value
import time
def medir_distancia(chip_path, trig_offset, echo_offset):
# Solicitar la lí?nea TRIG como salida y ECHO como entrada
with gpiod.request_lines(
chip_path,
consumer=“ultrasonic-sensor”,
config={
trig_offset: gpiod.LineSettings(direction=Direction.OUTPUT, output_value=Value.INACTIVE),
echo_offset: gpiod.LineSettings(direction=Direction.INPUT)
}
) as request:
while True:
# Generar un pulso en TRIG
request.set_values({trig_offset: Value.INACTIVE})
time.sleep(0.002) # Pausa para estabilizar
request.set_values({trig_offset: Value.ACTIVE}) # Pulso alto (10 µ?s)
time.sleep(0.00001)
request.set_values({trig_offset: Value.INACTIVE}) # Regresar a bajo
# Inicializar las variables de tiempo
start_time = 0
end_time = 0
# Medir el tiempo en ECHO
timeout = time.time() + 1 # Configura un tiempo lí?mite de 1 segundo para evitar bucles infinitos
while request.get_value(echo_offset) == 0: # Espera señ?al alta en ECHO
start_time = time.time()
if time.time() > timeout:
print("Error: Tiempo de espera agotado para señ?al alta en ECHO.")
break
while request.get_value(echo_offset) == 1: # Medir tiempo mientras ECHO está? alto
end_time = time.time()
if time.time() > timeout:
print("Error: Tiempo de espera agotado para señ?al baja en ECHO.")
break
if start_time and end_time and end_time > start_time:
# Calcular la distancia en base a la duració?n del pulso
duration = end_time - start_time
distance = (duration * 34300) / 2 # Velocidad del sonido en cm/s
print(f"Distancia: {distance:.2f} cm")
else:
print("Error: Señ?al ECHO no detectada correctamente.")
# Pausa antes de la siguiente medició?n
time.sleep(1)
if name == “main”:
try:
medir_distancia("/dev/gpiochip4", 21, 19) # TRIG en lí?nea 21, ECHO en lí?nea 19
except Exception as e:
print(f"Ha ocurrido un error: {e}")
Have you tried adjusting the intervals?
And how do I do that? Or am I missing a permission? I access the GPIOs but I don’t receive anything, and when I enter rsetup and monitor the GPIOs, it says (E), error in the pins used.
rsetup prompts that the GPIO is occupied, then you need to see if your program is using this GPIO, if it is not being used by your program, you can tell me exactly which GPIO, I will help you to confirm whether it is being occupied by the kernel.