Friday, 8 February 2019

Dimable LED Using Raspberry Pi


Dimable LED Using Raspberry Pi:

In this project I'm going to tell you about how we can control the brightness of two leds using two push buttons with raspberry pi.

Components Used:
   1)    Raspberry pi 3 :
In this we have inbuilt Bluetooth and wi-fi, unlike previously we have to use Wi-Fi dongle in one of its usb port. There are total 40 pins in RPI3. Of the 40 pins, 26 are GPIO pins and the others are power or ground pins (plus two ID EEPROM pins.)

There are 4 USB Port and 1 Ethernet slot, one HDMI port, 1 audio output port and 1 micro usb port and also many other things you can see the diagram on right side. And also we have one micro sd card slot wherein we have to installed the recommended Operating system on micro sd card.

There are two ways to interact with your raspberry pi. Either you can interact directly through HDMI port by connecting HDMI to VGA cable, and keyboard and mouse or else you can interact from any system through SSH(Secure Shell).



  2)   Two LEDs



  3)   Two Push Buttons



Project Description:
In this project basically we will go through about 4 things. And those are GPIO Inputs, pullup resistor, GPIO outputs and PWM. So whenever we will press one button the intensity of leds will increase, and if we will press another button the intensity of leds will decrease. Two buttons will work as GPIO Inputs and two leds will work as GPIO outputs. Here we are using PWM concepts. As we need to give pulses to leds pins and we will start PWM at 0 percent duty cycle and will go to 100 percent duty cycle.

Code of Project:
from time import sleep  # Library will let us put in delays
import RPi.GPIO as GPIO # Import the RPi Library for GPIO pin control
GPIO.setmode(GPIO.BCM)# We want to use the physical pin number scheme
button1=23              # Give intuitive names to our pins
button2=18
LED1=25
LED2=24
GPIO.setup(button1,GPIO.IN,pull_up_down=GPIO.PUD_UP)  # Button 1 is an input, and activate pullup
GPIO.setup(button2,GPIO.IN,pull_up_down=GPIO.PUD_UP)  # Button 2 is an input, and activate pullup
GPIO.setup(LED1,GPIO.OUT) # LED1 will be an output pin
GPIO.setup(LED2,GPIO.OUT) # LED2 will be an output pin
pwm1=GPIO.PWM(LED1,1000)  # We need to activate PWM on LED1 so we can dim, use 1000 Hz
pwm2=GPIO.PWM(LED2,1000)  # We need to activate PWM on LED2 so we can dim, use 1000 Hz
pwm1.start(0)              # Start PWM at 0% duty cycle (off)            
pwm2.start(0)              # Start PWM at 0% duty cycle (off)
bright=1                   # Set initial brightness to 1%
while(1):                  # Loop Forever
        if GPIO.input(button1)==0:             #If left button is pressed
                print "Button 1 was Pressed"   # Notify User
                bright=bright/2.               # Set brightness to half
                pwm1.ChangeDutyCycle(bright)   # Apply new brightness
                pwm2.ChangeDutyCycle(bright)   # Apply new brightness
                sleep(.25)                     # Briefly Pause
                print "New Brightness is: ",bright # Notify User of Brightness
        if GPIO.input(button2)==0:             # If button 2 is pressed
                print "Button 2 was Pressed"   # Notify User
                bright=bright*2                # Double Brightness
                if bright>100:                 # Keep Brightness at or below 100%
                        bright=100
                        print "You are at Full Bright"
                pwm1.ChangeDutyCycle(bright)  # Apply new brightness
                pwm2.ChangeDutyCycle(bright)  # Apply new brightness
                sleep(.25)                    # Pause
                print "New Brightness is: ",bright #Notify User of Brightness





0 comments:

Post a Comment

if you have any doubt please let me know