Menu Close

Raspberry Pi – Digital Output – LED Blinking

This tutorial explains how to take digital output from raspberry. One of the basic tutorials for raspberry. The output is taken on a LED that blinks for an interval of 1 second.

1. Introduction:

Raspberry Pi is a sort of jack of all trades when it comes to being a single board computer based on the Arm processor. The general purpose input output (GPIO) pins on the Raspberry Pi speak and listen to the outside world and can be controlled or programmed. Each pin has a specific role. Its hardware has a limited number of digital I/O pins. You can add 16 digital I/O pins by connecting a Pi wedge B + expander chip to the Raspberry Pi hardware

A step by step illustrated very basic tutorial for digital input and output in physical computing. Here we are taking digital output on a LED. This LED remains ON for one second and OFF for another, this loop runs for an infinite time.

This tutorial is for Raspberry Pi . Please note that the same tutorial can be performed on LUA as well.

If a digital signal is drawn from the Raspberry Pi, it is called a digital output.To see a digital output, an LED is attached. If the Led glows then it is giving a HIGH voltage signal and vice-versa.

1.1 LED Pins:

LED has two pin interface. Both of these pins are to be given input supply to the LED. Long legs is for positive supply the smaller one is for negative supply. following image depicts it clearly.

Digital means either 0/1, in other words HIGH/LOW or ON/OFF. So in the form of digital output we shall get either +5V or 0V on digital pin of raspberry pi. How does it happen that is illustrated ahead.

S.No.ItemQuantity
1 Raspberry Pi 1
2 Pi Wedge B+ 1
2 Breadboard 1
3 LED 1
4 Resistor 1k 1
5 Jumper 2

2. Building Circuit

Schematic Diagram:

Circuit Layout:

3. Programming:

Once we are done with circuit part, here is our programme to this circuit.

You may download this code (Python Code) from here.

// Robo India Tutorial 
// Digital Output on LED 
// Hardware: Raspberry Pi

import RPi.GPIO  as  GPIO
import  time
LedPin = 22         # pin11 
def setup():      
 GPIO.setmode(GPIO.BOARD)       # Set the board mode  to numbers pins by physical location
 GPIO.setup(LedPin, GPIO.OUT)   # Set pin mode as output
GPIO.output(LedPin, GPIO.HIGH) # Set pin to high(+3.3V) to off the led
 GPIO.setup(13,GPIO.OUT) 
def loop():     
  while True:            
         GPIO.output(LedPin, GPIO.LOW)   # led on
         time.sleep(1.0)                                  # wait 1 sec    
         GPIO.output(LedPin, GPIO.HIGH)  # led off
         time.sleep(1.0)                                  # wait 1 sec
def destroy():
        GPIO.output(LedPin, GPIO.HIGH)     # led off
        GPIO.cleanup()                                    # Release resource
 if __name__ == '__main__':                  # Program start from here
        setup()
        try:
                loop()
        except KeyboardInterrupt:            # When 'Ctrl+C' is pressed, the destroy() will be  executed.
               destroy()

4. Output

Python Coding with Raspberry Pi connects your project to the real world. The easiest introduction to Python is through IDLE, a Python development environment.

Open IDLE (Python Development Environment) from the Desktop or applications menu:

Python 3 is the newest version. Write the above code and save your code on desktop or any other location. Open Command Prompt. Write cd location_of_program > Press Enter.

Then write Python 3 file_name.py > Press Enter. (.py is file extension).

Your Python code is connected with your real world project. The execution of the code makes the LED on for one second and off for another one second. This happens in an infinite loop. Thus the LED keeps blinking.

5. Troubleshooting

LED is not glowing: Try changing polarity of LED, Pull it out, rotate it by 180 degree and insert bit again.
Code uploading error: The code will only be uploaded when there is no error in the code. Try removing error, if any.
General error: Never keep open circuit including raspberry pi on a metallic surface. It may shorten the circuit.

If you have any query please write us at support@roboindia.com

Thanks and Regards
Content Development Team 
Robo India
https://roboindia.com

Leave a Reply