Complete Guide Raspberry Pi LED Dimmer Switch

Circuit to control the LED brightness with a potentiometer

In today’s project, we will see how to control the brightness of an LED (raspberry pi LED dimmer switch) using a potentiometer. At the same time, we will learn how to convert the analog signal to Raspberry Pi and how to generate PWM i.e. pulse with the modulation signal. This project will help you to create many more projects in the future. 

Necessary equipment

You have to need a flowing component for the raspberry pi LED dimmer switch project

  1. Raspberry Pi
  2. Breadboard
  3. 10 kΩ potent iometer
  4. MCP 3008 chip
  5. 5 mm LED
  6. 330 Ω resistor
  7. Jumper wires

Introduction to Potentiometer:

 Various applications used in your daily life such as – to control the volume of the radio, controlling the brightness of any light, determining the fan speed, and much more. Keep using the potentiometer.

The potentiometer you will use in this project is shown in Figure 1.

A 10 kΩ potentiometer
Figure 1 A 10 kΩ potentiometer

 The potentiometer is also referred to as POT. This is a manually adjustable variable register.

You can change the resistance according to your needs by turning the knob of the potentiometer.

The potentiometer shown in Figure 2 has three pins. The two external pins displayed in blue and red are attached to a resistive element. The third is associated with a conductive adjustable wiper displayed in black.

How a potentiometer works
Figure 2 How a potentiometer works

The value of the circuit resistance is changed by changing the value of the adjustable wiper. In this project, you will be able to control the value of the resistance of the circuit to make the LED light brighter or brighter.

Analog Signal Measurement 

Raspberry Pi: Raspberry Pi GPIO pins can only read digital signals. This means they can read between high (3.3 v) or low (0 v), but also no other signal can read raspberry pi. 

However, the potentiometer provides an analog input voltage, and rotating the knob of the potentiometer changes the output voltage from 0 V to 3.3 V.

But we want raspberry Pi to be able to read different quality voltages. Such as 1, 1.1, 2 or 2.2 volts, etc. and we can provide such voltage i.e. variable voltage input at Raspberry Pi. Only then will we be able to control the brightness of our desired LED. 

For that you can convert analog to digital and digital to the analog signal, such an IC is required. With this IC you will be able to accomplish your desired task by converting the analog signal to digital signal and providing it in the input of Raspberry Pi.

Analog-to-Digital Converter

As shown in Figure 3, the analog-to-digital converter (ADC) chip (MCP 3008) converts the analog signal of a potentiometer to a digital signal.  

MCP3008 IC
Figure 3 MCP3008 IC

This chip has 16 pins, including 8 analog inputs. Which you can connect to analog devices. The other 8 raspberry Pis need to be attached to the GPIO pin. The pinout of IC is shown in Figure 4 below.

MCP3008 IC Pin Out
Figure 4 MCP3008 IC Pin Out

The following table gives the function for each pin.

PINSYMBOLDESCRIPTION
1CH0Analog input (channel 0)
2CH1Analog input (channel 1)
3CH2Analog input (channel 2)
4CH3Analog input (channel 3)
5CH4Analog input (channel 4)
6CH5Analog input (channel 5) )
7CH6Analog input (channel 6)
8CH7Analog input (channel 7)
9DGNDDigital ground
10CS / SHDNChip select / shutdown input
11DINSerial data in
12DOUTSerial data out
13CLKSerial clock
14AGNDAnalog ground
15VREFReference voltage input
16VDD+2.7 V to 5.5 V power supply

Pulse-Width Modulation (PWM) 

We already know that the GPIO pins of Raspberry Pi can only be set to High or Low. Due to which no other voltage i.e. analog voltage can give output. Interestingly, you can easily increase or decrease the brightness of your LED using pulse-width modulation (PWM). 

When a high-frequency LED is quickly turned on / off, which we cannot see with the naked eye. Only gradations of brightness can be seen. This is how PWM usually works. As long as an LED stays high, it is called PWM’s duty cycle.

The following figure shows how PWM works.

FIGURE 3-5: How PWM works

50% of a duty cycle means 50% brightness of the LED i.e. a duty cycle 0 means the LED is completely off and a bicycle 100 means the LED is completely on. So the level of brightness of the LED can be increased or decreased by changing the value of the duty cycle. 

LED dimmer switch circuit wiring: 

In this circuit, you need to attach an LED, MCP3008 chip, and a potentiometer to the Raspberry Pi. Follow the instructions below to create the circuit using Figure 6 as a reference.

  1. Attach the ground of the Raspberry Pi to the breadboard. 
  2. Connect the 3.3 voltage of the Raspberry Pi to the breadboard.
  3. The LED must be connected to a 330-ohm resistor series from the positive edge of the LED by placing it on the breadboard and connecting it to the GPIO 25 pin of the Raspberry Pi. 
  4. Place the MCP3008 chip in the breadboard and attach it to the Raspberry Pi as shown in the table below.
MCP3008RASPBERRY PI
1Potentiometer middle lead
9GND
10GPIO 8
11GPIO 10
12GPIO 9
13GPIO 11
14GND
153.3 V
163.3 V
  1. Connect one of the outer legs of the potentiometer to GND and the other to 3.3 V. Connect the middle leg of the potentiometer with the MCP3008 chip pin 1. 
Circuit to control the LED brightness with a potentiometer

    Figure 6: Circuit to control the LED brightness with a potentiometer

Script Writing:

MCP3008 receives input from this IC using Raspberry Pi SPI communication, so you must first enable Raspberry Pi’s SPI communication option. Which is shown in the figure below.

 taskbar main menu, select Preferences ▸ Raspberry Pi Configuration. 

 Enabling SPI communication
Figure 7: Enabling SPI communication

Following the steps below, we will create an LED dimmer switch script.

Raspberry Pi MCP3008 will read the analog input from the potentiometer via IC.

Using PWM will control the brightness of the LED. The duty cycle will change based on the input value of the potentiometer. 

Now take out the python IDE of Raspberry Pi and write the following program there and when you finish writing it you have to save it as brightness_controller.py. 

#import necessary libraries
  from gpiozero import PWMLED, MCP3008
  from time import sleep

  #create an object called pot that refers to MCP3008 channel 0

➊ pot = MCP3008 (0)
#create a PWMLED object called led that refers to GPIO 17

➋ led = PWMLED ( 17)

➌ while True:

      # pot.value accesses the current pot reading

    ➍ if (pot.value <0.001):

          #if the pot value is too small, the led is turned off

        ➎ led.value = 0

    ➏ else:

          #change led brightness according to the pot value

          led.value = pot.value

      #print the pot value

      print (pot.value)

      #pause for 0.1 seconds

      sleep (0.1)

Script Analysis

 from gpiozero import PWMLED, MCP3008

from time import sleep

LED’s PWM Imported PWMLED from gpiozero library for control and MCP3008 for ADC control. At the same time, sleep has been imported from the time library to control the time. 

➊ pot = MCP3008 (0)

➋ led = PWMLED (17)

An object named pot for MCP3008 and an object named led for PWMLED have been created.

➌ while True:

Loop is used in programming to repeat specific code blocks. If the value of the while loop is True, the code inside the while loop is executed and this process continues until the value of the loop is false (0).

 ➍ if (pot.value <0.001):

          #if the pot value is too small, the led is turned off

        ➎ led.value = 0

    ➏ else:

          #change led brightness according to the pot value

          led.value = pot.value

here If-else condition has been used. If the value of pot.value is less than 0.001, then led.value = 0 which means LED will be off. And if the condition is else, then led.value = pot.value. That means the brightness of the LED will change based on the value of the pot.

print (pot.value)

sleep (0.1)

Here the value of pot will be displayed on the serial monitor. Then after 0.1 time the loop

will be restarted.

Raspberry Pi LED Dimmer Switch switch program

run: To run, first you have to save or save the above program, then you have to run Run ▸ Run current script or press F5 from the keyboard then the program will run. To close the running program, press CTRL + F2.

Homework:

If you know how to read analog values ​​and how to control you can create a myriad of projects. Below are some of his ideas:

  1. Control several LEDs with the same potentiometer.
  2. Build a bar graph of LEDs that you can control with a potentiometer.
  3. Control the blinking speed of an LED using a potentiometer.

RASPBERRY PI TUTORIALS FOR BEGINNERS

 25 total views,  3 views today