How to PWM an LED bulb using a Raspberry Pi board

How to PWM an LED bulb using a Raspberry Pi board

Hello and welcome back. In this tutorial, we will learn how to PWM an LED bulb using a Raspberry Pi board. We have presented step-by-step how to do this with Arduino and Raspberry Pi Pico in previous tutorials, and you can also visit them if you want. Also, I used a potentiometer for controlling the LED bulb. Then, we can control the light of the LED bulb as we want. And we need an Analog to Digital converter for this project. Because the Raspberry Pi board doesn’t have analog input pins to get the analog values. But, these pins are included in the Raspberry Pi Pico boards. For this reason, I used the Analog to Digital Converter (ADC) known as ADS7830. You can see it below.

It’s included eight analog pins and two digital pins. For this, a 3.3VDC power supply should be provided and it operates through I2C communication. Therefore, we can use it using four wires (VCC, GND, SDA, SCL). These analog pins have separate I2c addresses and they are as follows. Use the I2C address corresponding to the pin you are using. Don’t worry, there are described in the program.

  • A0 – 0x84
  • A1 – 0xc4
  • A2 – 0x94
  • A3 – 0xd4
  • A4 – 0xa4
  • A5 – 0xe4
  • A6 – 0xb4
  • A7 – 0xf4

PWM an LED bulb with the Arduino – Click me

PWM an LED bulb with the Raspberry Pi Pico – Click me

Also, I use the Raspberry PI 2 Model B board for this tutorial. You can use any other Raspberry Pi board.

OK, let’s do it step by step. The required components are given below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Step 2

Secondly, place the ADS7830 converter on the breadboard and connect it to the Raspberry Pi board. For that, use the Circuit diagram below.

How to PWM an LED bulb using a Raspberry Pi board

Step 3

Thirdly, place the potentiometer and connect it to the ADS7830 converter. Please use the circuit diagram above.

Step 4

And then, place the LED bulb and 180-ohm resistor on the breadboard. Next, connect it to the Raspberry Pi board.

Step 5

Now, connect the mouse, keyboard, monitor, and SD card to the Raspberry Pi board.

Step 6

Finally, connect the power source to the Raspberry Pi board.

Step 7

OK, now let’s create the program for this project. For that, follow the steps below.

  • First, enable I2C communication. And then, you can check it using the terminal. Use the following images for that.

  • And then, copy and paste the program to the Thonny IDE. It’s as follows.
  • Full details of this project — Download
#Include the library files
from smbus import SMBus
from time import sleep
from gpiozero import PWMLED

bus = SMBus(1)
# Include the LED pin

#Get the analog input values
def analogInput():
    bus.write_byte(0x4b,0x84)# ADC module address, analog pin address
    value = bus.read_byte(0x4b)
    return int(value)

while True:
    value = analogInput()# 0.0 to 1.0
    LED.value = value/255 # Write the values on the LED
    sleep(0.05) #Delay time
  • Now, save this code. Finally, click the run button and enjoy this project.

The full video guide is below. So, see you in the next project or tutorial.

How to PWM an LED bulb using a Raspberry Pi board

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *