3

I am learning raspberry pi and somewhere i found the tutorial of turning the bulb on when clap is heard and off the bulb when clapped again. The following code was written for this to work

#!/usr/bin/python
import RPi.GPIO as GPIO
import time

# telling pi we are not using the pin but BCM standard GPIO names for the pins
GPIO.setmode(GPIO.BCM)

BULB_OUTPUT_PIN = 4
SOUND_INPUT_PIN = 17


GPIO.setup(BULB_OUTPUT_PIN, GPIO.OUT)
# why high is given initially?
GPIO.output(BULB_OUTPUT_PIN, GPIO.HIGH)

# for sound sensor
GPIO.setup(SOUND_INPUT_PIN, GPIO.IN)

SleepTime = 6

# main loop

try:
    while 1:
        print('#######GPIO INPUT PIN##### ', GPIO.input(SOUND_INPUT_PIN))
        if GPIO.input(SOUND_INPUT_PIN) == GPIO.LOW:
            print("##########Sound is sensed############")
            time.sleep(SleepTime)
            # GPIO.LOW is used to switch on
            GPIO.output(BULB_OUTPUT_PIN, GPIO.LOW)
            print("#Relay is triggered in the switch 1########")
            time.sleep(SleepTime)
            GPIO.cleanup()
            print("Good bye!")
# End program cleanly with keyboard
except KeyboardInterrupt:
    print("Quit")

    # Reset GPIO settings
    GPIO.cleanup()

In the code above you can see, GPIO.HIGH is used initially and I dont know why it is done at the very start because in my understanding HIGH means to turn on/switch on but here the thing is opposite. Why is that so? Can anyone explain the use of GPIO.HIGH and GPIO.LOW considering the above example, please?

2 Answers 2

3

Here are some ways to connect a bulb.

schematic

simulate this circuit – Schematic created using CircuitLab

3

The "load" ( light bulb) is connected to GPIO - output pin - and source of power for it. If the load power source / supply is "HIGH" , say 3,3V and the initial state of the GPIO is also "HIGH" there will be practically no current flow between GPIO pin and device power, hence the "light bulb" will be off. Setting the GPIO to "LOW" / ground you create difference of potential between GPIO pin and light bulb power source , hence current will flow turning the light bulb on.

Of course you will make sure resulting current magnitude does not exceed the current capacity of the GPIO pin.

2
  • I appreciate your answer and thanks for that. Can you explain me the last line "make sure resulting current magnitude does not exceed the current capacity of the GPIO pin", please?
    – milan
    Commented Aug 23, 2018 at 5:03
  • GPIO pin can supply ( source ) up to 16mA of current, your load - bulb _ requires some current to actually light up. This "turn on" current CANNOT exceed the GPIO pin ability to supply approximately 16 mA. So you need to limit this current by using resistor. However, this is very basic explanation of what you are asking. Perhaps you could post the schematic of you setup.
    – Jan Hus
    Commented Aug 23, 2018 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.