Building Robots with Python and Raspberry Pi
  • Introduction
  • Chapter1
  • Chapter2
  • Chapter3
  • Chapter4
  • Chapter5
  • Chapter6
  • Chapter7
  • Chapter8
  • Chapter9
  • Chapter10
Powered by GitBook
On this page
  • Ports of Raspberry PI
  • LED Controlling Through Raspberry Pi

Chapter3

PreviousChapter2NextChapter4

Last updated 7 years ago

Ports of Raspberry PI

LED Controlling Through Raspberry Pi

Raspberry PI GPIO can be used to send current to LED and light it. Please note that a resister is needed to lower the current, as shown below.

As an easy experiment, let's connect a LED and a resister on a breadboard and connects them. Raspberry PI GPIO14 (3.3V) connects to one leg of LEG and Raspberry PI GND (ground) port connects to the resistor. (Refer to the pinout diagram above to find out the physical pin number -- 8 for GPIO14). The whole setup is shown below.

Caution: If you make a mistake and short circuit the two pins from the Raspberry PI, you may burn the board. So make sure the current from one pin goes through the resistor and LED and then goes back to the other pin.

The program below turns the LED on for one second and then turns off for one second. The LED flashes for five times.

import sys
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(ONE, GPIO.OUT)

for x in range (5):
        GPIO.output(14, GPIO.HIGH)
        time.sleep(1)
        GPIO.output(14, GPIO.LOW)
        time.sleep(1)