Controlling LED with Raspberry Pi PART-2
In previous article we discuss about “How to connect led to raspberry, Switching LED on/off from Raspberry Pi console, Python program for blinking LED and Interfacing an LED an Switch with Raspberry Pi and Python program for sending an email on switch press” So please read this previous article Controlling LED with Raspberry Pi. let’s begin Controlling LED with Raspberry Pi PART-2.
LEDs are a very useful, cheap, and efficient way of producing light, but you do have to be careful how you are use them. If you are connected directly to a voltage source (such as GPIO output) that is greater than about 1.7V, they will draw a very large current. This can often be enough to destroy the LED or whatever is providing the current – which is not good if your RPi is providing the current.
You should always use a series resistor with LED because the series resistor is placed between the LED and voltage source, which limits the amount of current flowing through the LED to a level that is safe for both the LED and the GPIO pin driving it.
Selecting series resistors for LEDs and a 3.3V GPIO pin
LED Type Resistor Current(mA) Red 470Ω 3.5 Red 1KΩ 1.5 Orange, Yellow, green 470Ω 2 Orange, Yellow, green 1KΩ 1 Blue, White 100Ω 3 Blue, White 270Ω 1
Leaving the GPIO Pins in a Safe State
You want all the GPIO pins to be set to inputs whenever your program exists so that there is less of a chance of a chance of an accidental short on the GPIO header, which could damage your raspberry Pi.
So use try: finally: construction and the GPIO.cleanup method.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
try:
while (True)
GPIO.output(18, True)
time.sleep(0.5)
GPIO.output(18, False)
time.sleep(0.5)
finally:
print("Cleaning up")
GPIO.cleanup()
Now, when you Ctrl+C the program to close it, GPIO.cleanup will be called before the program exists.
You may like also: How To Use Raspberry pi in a truely headless mode
Controlling the brightness of an LED
If you wants to vary the brightness of an LED from a python program. The RPi.GPIO library has a pulse-width modulation (PWM) feature that allows you to control power to an LED and its brightness.
import RPi.GPIO as GPIO
led_pin = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(led_pin, GPIO.OUT)
pwm_led = GPIO.PWM(led_pin, 500)
pwm_led.start(100)
while True:
duty_s = raw_input("Enter brightness (0 to 100)")
duty = int(duty_s)
pwn_led.ChangeDutyCycle(duty)
if you are using python 3 rather than python 2, change the command raw_input to input.
So PWM is a clever technique where you vary the length of pulse while keeping the overall number of pulse per second (the frequency in Hz) constant.
You can change the PWM frequency by modifying this line:
pwm_led = GPIO.PWM(led_pin,500)
You may like also:
- How To Create Secure MQTT Broker
- Simple Raspberry Pi Home Security System
- Using Mq135 Sensor with InfluxDB
- Best OS for Raspberry Pi
- ROCK Pi 4 : Overview | Installation
- Remote control your Raspberry Pi from your PC with VNC!
- How To Use Raspberry pi in a truely headless mode
Buy now : Raspberry PI 3 Model B+ Motherboard
Pingback: Controlling LED with Raspberry Pi PART-2 — IoTbyHVM – Explore TechBytes – hashstacks
Pingback: piCore (Tiny Core) Linux on Raspberry Pi - IoTbyHVM - Bits & Bytes of IoT
Pingback: Create a Docker Container for Raspberry Pi to Blink an LED
Pingback: How to Run Ubuntu 18.04 or 18.10 on Raspberry Pi
Pingback: Control LED with Raspberry Pi using Nodejs - IoTbyHVM - Bits & Bytes of IoT
Pingback: Paytm will launch SIM-card enabled ‘Soundbox’
Pingback: Web Controlled IoT Notice Board using Raspberry Pi
Pingback: Raspberry Pi — Introduction | Overview | Setup and Management | Tutorials - CompileIoT
Pingback: Raspberry Pi SPI | Setting up SPI on Raspberry Pi - CompileIoT
Pingback: Install sgminer-arm on Rock Pi 4 - IoTbyHVM - Bits & Bytes of IoT
Pingback: Raspberry Pi Introduction and Tutorials - OnionLinux