Nick2308
Posts: 4
Joined: Mon Jan 15, 2018 8:14 pm

NEED HELP! COMBINING ULTRASONIC SENSOR AND LED DIODE

Mon Jan 15, 2018 8:25 pm

I am trying to make a code that would combine ultrasonic sensor (HC-SR04) and a led diode, but I need help with this. What I am trying to do is make the led diode to turn on when the ultrasonic sensor measures a certain distance (for ex. If the distance is more than 100cm, turn the led on.... or if the distance is lower that 20cm turn the led on...). Please somebody help as soon as possible since I need it in a short time.
Thanks so much for any help!

User avatar
davidcoton
Posts: 5084
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: NEED HELP! COMBINING ULTRASONIC SENSOR AND LED DIODE

Mon Jan 15, 2018 9:42 pm

School assignment?

How far have you got? Is the problem hardware or software?
HARDWARE: Upload a picture of you setup to a hosting site and link here. Post a schematic.
SOFTWARE: Post your code so far and explain in more detail what is not working.
Signature retired

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: NEED HELP! COMBINING ULTRASONIC SENSOR AND LED DIODE

Mon Jan 15, 2018 9:53 pm

Perhaps you could collaborate with this guy?

viewtopic.php?f=91&t=202289

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: NEED HELP! COMBINING ULTRASONIC SENSOR AND LED DIODE

Mon Jan 15, 2018 10:06 pm

Or steal this guy's code

viewtopic.php?f=37&t=202616
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Nick2308
Posts: 4
Joined: Mon Jan 15, 2018 8:14 pm

Re: NEED HELP! COMBINING ULTRASONIC SENSOR AND LED DIODE

Mon Jan 15, 2018 10:59 pm

I'm kind of a newbie here...and hardware is properly connected since I tried running codes separately and it worked without errors. The only problem I am facing is combining these 2 codes, so here are 2 codes I want to combine in such way so the led would turn on when a certain distance is detected by the ultrasonic sensor

Code for ultrasonic sensor:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

TRIG = 23 
ECHO = 24

print("Distance Measurement In Progress")

GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)

def measure():
        GPIO.output(TRIG, False)
        print("Waiting For Sensor To Settle")
        time.sleep(.5)
        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)

        print("Distance:",distance,"cm")

while True:
        measure()


Code for LED diode:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
print("LED on")
GPIO.output(18,GPIO.HIGH)
time.sleep(1)
print("LED off")
GPIO.output(18,GPIO.LOW)

I would really appreciate any explanations on how to do this or something... Thanks.

Nick2308
Posts: 4
Joined: Mon Jan 15, 2018 8:14 pm

Re: NEED HELP! COMBINING ULTRASONIC SENSOR AND LED DIODE

Tue Jan 16, 2018 12:12 am

davidcoton wrote:
Mon Jan 15, 2018 9:42 pm
School assignment?

How far have you got? Is the problem hardware or software?
HARDWARE: Upload a picture of you setup to a hosting site and link here. Post a schematic.
SOFTWARE: Post your code so far and explain in more detail what is not working.
It's kind of a school assignment... more like doing it for myself to learn new stuff and to be able to clearly understand what the professor is going to be explaining in class.

I got as far as you can see in the reply below... I was trying to combine it somehow but it always failed. As I said, the problem is software only.

jbudd
Posts: 1446
Joined: Mon Dec 16, 2013 10:23 am

Re: NEED HELP! COMBINING ULTRASONIC SENSOR AND LED DIODE

Tue Jan 16, 2018 12:43 am

Something like this (using your code)?

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

TRIG = 23 
ECHO = 24

print("Distance Measurement In Progress")

GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.setup(18,GPIO.OUT)

def measure():
        GPIO.output(TRIG, False)
        print("Waiting For Sensor To Settle")
        time.sleep(.5)
        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)

        print("Distance:",distance,"cm")
        
        if distance < 20 :
                print("LED on")
                GPIO.output(18,GPIO.HIGH)
                time.sleep(1)
                print("LED off")
                GPIO.output(18,GPIO.LOW)

while True:
        measure()


Nick2308
Posts: 4
Joined: Mon Jan 15, 2018 8:14 pm

Re: NEED HELP! COMBINING ULTRASONIC SENSOR AND LED DIODE

Tue Jan 16, 2018 6:02 am

jbudd wrote:
Tue Jan 16, 2018 12:43 am
Something like this (using your code)?

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

TRIG = 23 
ECHO = 24

print("Distance Measurement In Progress")

GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)
GPIO.setup(18,GPIO.OUT)

def measure():
        GPIO.output(TRIG, False)
        print("Waiting For Sensor To Settle")
        time.sleep(.5)
        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)

        print("Distance:",distance,"cm")
        
        if distance < 20 :
                print("LED on")
                GPIO.output(18,GPIO.HIGH)
                time.sleep(1)
                print("LED off")
                GPIO.output(18,GPIO.LOW)

while True:
        measure()

Thank you sir! :)
Will try the code as soon as possible and report back if it works.

Return to “Troubleshooting”