Skip to main content
Engineering LibreTexts

Grove LED

  • Page ID
    51681
  •  

    Grove LED small.png

     

    LED stands for light-emitting diode 

     

    LED Wiki diagram.png

    From Wiki page.

    https://en.wikipedia.org/wiki/Light-emitting_diode

    The grove LED is a digital controlled device.

    It is turned on by setting to “high” the digital port it is connected to.

    Question, what port (pin) is LED connected to?

    Copy and paste the following python code in the Thonny IDE and save it with the name onOffLED.py.

    #!/usr/bin/python3
    
    import RPi.GPIO as GPIO # Import the library to use the GPIO ports.
    import time             # Import the library to use time sleep function.
    
    port = 16               # In this example port 16 is used on the grove hat. This may need to be changed
                            #  depending on where you attached the grove connector on the RPi hat.
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)   # Turn off warnings.
    GPIO.setup(port,GPIO.OUT) # Set the port for output.
    
    print("LED on")
    
    GPIO.output(port,GPIO.HIGH) # <- Set port high, turn on.
    

    The above code turns on the LED.  To turn off the LED modify the last line from

    GPIO.output(port,GPIO.HIGH)

    to the following and run the script again.

    GPIO.output(port,GPIO.LOW)

    The LED should now be off.

    Next modify your python code to look like the following example;

    #!/usr/bin/python3
    
    import RPi.GPIO as GPIO # Import the library to use the GPIO ports.
    import time             # Import the library to use time sleep function.
    
    port = 16               # In this example port 16 is used on the grove hat. This may need to be changed
                            #  depending on where you attached the grove connector on the RPi hat.
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)   # Turn off warnings.
    GPIO.setup(port,GPIO.OUT) # Set the port for output.
    
    print("LED on")
    
    GPIO.output(port,GPIO.HIGH) # <- Set port high, turn on.
    time.sleep(5)               # Wait here for X seconds.
    
    print("LED off")
    
    GPIO.output(port,GPIO.LOW)  # <- Set port low, turn off.
    

    Again run the python script and notice that the LED comes on and stays on for 5 seconds before going off.

    Now try different values in the sleep function and confirm how long the LED stays on.

    If you can turn on an LED you can turn on anything connected to the relay shown here.

    Controllable Four Outlet Power Relay small.png

    https://eng.libretexts.org/Courses/University_of_Arkansas_Little_Rock/IFSC_4399_-_The_Internet_of_Things_(IoT)/Controllable_Four_Outlet_Power_Relay

     

     

     

     

     

     

    • Was this article helpful?