how to turn on led raspberry pi

How to turn on led light on raspberry pi

How to turn on led raspberry pi

This post only covers the simple step to turn ON a LED accessing the GPIO pins using the python GPIO library. To learn how to turn ON/OFF a LED using a web interface follow the link present at the bottom of the post.

How to turn on led raspberry pi

Prerequisites:-

  1. A Raspberry pi, obviously. We are using the raspberry pi 3 model B
  2. A Breadboard. What is it?
  3. A LED
  4. A 330 ohm resistor
  5. Two Male-Female jumper wires

You can buy points 2-5 from a local electric shop. It cost me $1.50.

Setup:-

  • Boot up the Raspberry pi.
  • Create a file on your raspberry pi and name it led.py (for now). If you are connected to raspberry pi via SSH then nano led.py
  • Copy the below code and paste it to the led.py file. We will be using the GPIO pin #17 since it’s common in all raspberry pis. Read the comments to know what each line does.
    import RPi.GPIO as GPIO
    #importing GPIO module from RPi library
    import time
    #to stop execution after some seconds
    GPIO.setmode(GPIO.BCM)
    #setting the naming convention to address the GPIO pins
    GPIO.setwarnings(False)
    #instructs python stop printing warning to the console
    GPIO.setup(17,GPIO.OUT)
    #this is necesary as it tells python that pin 17 is going to be used
    print "LED on" #to notify that the GPIO pin is going to output
    GPIO.output(17,GPIO.HIGH)
    #turns ON outputting in GPIO pin no.17
    time.sleep(1)
    #stop any further execution of the program for one second
    print "LED off"
    #to notify that the GPIO pin is about to stop outputting
    GPIO.output(17,GPIO.LOW)
    #turns OFF outputting in GPIO pin no.17
    
  • Connect jumper wire to GRND and #17 GPIO pin.

    Refer to image below

    How to turn on led light raspberry pi

  • Build the circuit.

    Follow the circuit diagram to complete the connection

    how to turn on led raspberry pi
    This is how the final circuit should look like with a breadboard

Running the code

  • To run this code type: sudo python led.py
  • You’ll see “LED on” printed in the console then stop after one second printing “LED off

Now that you have learned how to turn ON/OFF a LED on a raspberry pi, time for you to move on and build a custom interface to turn ON/OFF the LED.

Read more such articles on Learn2Create.

Published by

Rohit Nair

A full stack developer intrigued by AI and automation. Currently works on NLP and ML. Has previous experiences as a MIS and a web developer.

Leave a Reply

Your email address will not be published. Required fields are marked *