Ad Code

Python code to control LED using Raspberry Pi

 

Python code to control LED using Raspberry Pi

Using the Raspberry Pi to control an LED might seem like a basic or boring task. However, the same hardware and programming concepts used to control an LED can be used to control a wide variety of sensors and modules. Learning how to control an LED with the Raspberry Pi’s GPIO pins will open up a whole new variety of devices you can use with the Raspberry Pi.

Now here are the steps to build the circuit:

  • Connect one wire between one GND (ground) pin of the Raspberry Pi and the blue line of the breadboard.
  • Take the LED and check the 2 legs. You will see that one is shorter than the other. Plug the shorter leg to the blue line (now connected to GND), and the longer to any other connector. You can either directly connect the shorter leg to the blue line, or add an additional short male-to-male connector (like in the picture), the result is the same.
  • Plug one leg of the resistor to the same line as the longer leg of the LED, and the other leg of the resistor to a different line.
  • Finally, to close the circuit plug one wire between the same line as the other leg of the resistor, and the GPIO number 17 (more on Raspberry Pi pins and GPIOs). This is the 6th pin on the GPIO header, starting from the left, on the inside side.

import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(14,GPIO.OUT) # While loop while True: # set GPIO14 pin to HIGH GPIO.output(14,GPIO.HIGH) # show message to Terminal print "LED is ON" # pause for one second time.sleep(1) # set GPIO14 pin to HIGH GPIO.output(14,GPIO.LOW) # show message to Terminal print "LED is OFF" # pause for one second time.sleep(1)



Python Program:

Use below python code to control the LED.

import RPi.GPIO as GPIO
import time

PIN = 36

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(PIN, GPIO.OUT)         #LED output pin

while True:
    GPIO.output(PIN, 0)  #Turn OFF LED
    time.sleep(1)
    GPIO.output(PIN, 1)  #Turn ON LED
    time.sleep(1)

On your Raspberry Pi, open IDLE (Menu > Programming > Python 2 (IDLE)).

Open a new project go to File > New File. Then type (or copy and paste) the following code:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.output(17, True)
time.sleep(3)
GPIO.output(17, False)
time.sleep(1)
GPIO.output(18, True)
time.sleep(3)
GPIO.output(18, False)
time.sleep(1)
GPIO.output(22, True)
time.sleep(3)
GPIO.output(22, False)
time.sleep(1)
GPIO.output(23, True)
time.sleep(3)
GPIO.output(23, False) 


Save your project as multilights.py (File > Save As) in your Raspberry Pis Documents folder.

On your Raspberry Pi open Terminal (Menu > Accessories > Terminal) and navigate to your Documents folder by typing the following:

cd /home/pi/Documents

You can now run your new script by typing the following:

python multilights.py

The lights will take it, in turn, to switch on and off. The above script uses time. sleep command to create a pause between each step, making each light stay on for 3 seconds and to wait for 1 second before turning the next light on.

'Acharya Ramchandra Shukla' was born in 1884 in a village named Agona in Basti district, Uttar Pradesh, India. His father Pt. Chandrawali Shukla was a Sarayuparin Brahmin. He was a supervisor Kanungo and biased of Urdu. Shuklji had studied till the Intermediate. After this, he did the job. Then he left the job and became a teacher. He started writing in Hindi from his student life. Impressed by Shuklaji's ability, Nagari Pracharini Sabha, Kashi called him to work in the Hindi literature. Shuklaji was appointed Hindi teacher in Hindu University and later became the Head of Hindi Department. He died in 1941 AD. Following are the major compositions of Acharya Ramchandra Shukla- 'Charan Vinod', 'Radhakrishna Das', 'Chintamani Triveni', 'Surdas', 'Ras Mimamsa', 'History of Hindi literature' etc. He edited 'Bhramar Geetasar', 'Bharatendu Sahitya', 'Tulsi Granthavali' and 'Jayasi Granthavali'. The talent of Acharya Ramchandra Shukla Ji was multi-faceted. He was a great essayist, critic and thinker. He is considered the first basic critic of Hindi. His history of Hindi literature is considered to be superior in history. Acharya Ramchandra Shukla was the pride of Hindi. Full name of 'Dr. A.P.J. Abdul Kalam' was 'Dr. Avul Pakir Jainulabdeen Abdul Kalam'. He was born on October 15, 1931 at Dhanushkothi in the temple town Rameshwaram in Tamil Nadu. He was born in a poor family, but he was an exceptionally brilliant child. Kalam passed the B.Sc. examination from Saint Joseph College, Thiruchirapalli. He joined Madras Institute of Technology (MIT). His further knowledge in the field got upgraded when he joined Defense Research and Development Organization (DRDO) in 1958 and Indian Space Research Organization (ISRO) in 1963. He is known as the Missile Man of India. The various Indian Missiles of world order like Prithvi, Trishul, Akash, Agni, etc. are mainly the result of his efforts and caliber. Dr. A.P.J. Abdul Kalam became the 11th President of India. He served the country from 2002 to 2007. For his excellence and brilliance, he was awarded the prestigious Bharat Ratna in 1998; Padma Vibhushan in 1990; and Padma Bhushan in 1981. Dr Kalam expired on Monday 27 July 2015. He suddenly fell unconscious when he was delivering a lecture at the Indian Institute of Management at Shillong. On 30 July 2015, the former President was laid to rest at Rameswaram's Pei Karumbu Ground with full state honours. Over 350,000 people attended the last rites, including the Prime Minister, the governor of Tamil Nadu and the chief ministers of Karnataka, Kerala and Andhra Pradesh. Dr. A.P.J. Abdul Kalam was mainly interested in work. He was a bachelor. He was not interested in going abroad. He wanted to serve his motherland first. He said that he thinks his first and foremost duty is to serve his motherland. He was fond of music and the Koran and the Gita. Ever since becoming the head of the Indian State, he had been having interaction with children all over the country. He was by no means a miracle man. His advice to the youngster of the nation was to "dream dream and convert these into thoughts and later into actions".
Close Menu