Raspberry Pi - Potentiometer fade LED

In a previous tutorial, we discovered how a potentiometer can be used to trigger a LED. This tutorial instructs you how to use Raspberry Pi to adjust the brightness of the LED based on the output value of the potentiometer.

Hardware Preparation

1×Raspberry Pi 4 Model B
1×Potentiometer
1×ADS1115 ADC Module
1×LED
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of LED and Potentiometer

If you are unfamiliar with LED and potentiometer (including pinout, functionality, programming, etc.), the following tutorials can help:

Wiring Diagram

The wiring diagram between Raspberry Pi and Rotary Potentiometer LED

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Code

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Install the Adafruit_ADS1x15 library by running the following commands on your Raspberry Pi terminal:
sudo pip install Adafruit-ADS1x15
  • Create a Python script file potentiometer_fade_led.py and add the following code:
# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-potentiometer-fade-led import time import Adafruit_ADS1x15 import RPi.GPIO as GPIO # Create an ADS1115 ADC object adc = Adafruit_ADS1x15.ADS1115() # Set the gain to ±4.096V (adjust if needed) GAIN = 1 # Define the GPIO pin for the LED LED_PIN = 16 # Change this to the appropriate GPIO pin # Set up GPIO for the LED GPIO.setmode(GPIO.BCM) GPIO.setup(LED_PIN, GPIO.OUT) # Define the PWM parameters PWM_FREQUENCY = 1000 # PWM frequency in Hz PWM_DUTY_CYCLE = 0 # Initial duty cycle (0 to 100) # Create a PWM object pwm_led = GPIO.PWM(LED_PIN, PWM_FREQUENCY) # Start PWM with the initial duty cycle pwm_led.start(PWM_DUTY_CYCLE) # Main loop to read the analog value and control LED brightness try: while True: # Read the raw analog value from channel A3 raw_value = adc.read_adc(3, gain=GAIN) # Convert the raw value to voltage voltage = raw_value / 32767.0 * 4.096 # Assumes 4.096 V range for GAIN=1 # Print the results print("Raw Value: {} \t Voltage: {:.2f} V".format(raw_value, voltage)) # Map the analog value to a duty cycle percentage (0 to 100) duty_cycle = int((raw_value / 32767.0) * 100) # Update the LED brightness with PWM pwm_led.ChangeDutyCycle(duty_cycle) # Add a delay between readings (adjust as needed) time.sleep(0.1) except KeyboardInterrupt: print("\nExiting the program.") # Stop PWM and clean up GPIO on exit pwm_led.stop() GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 potentiometer_fade_led.py
  • Turn the potentiometer.
  • Check out the LED's brightness.
  • Check out the output on the Terminal.
PuTTY - Raspberry Pi
Raw Value: 19736 Voltage: 2.39 V Raw Value: 19075 Voltage: 2.31 V Raw Value: 18409 Voltage: 2.23 V Raw Value: 17747 Voltage: 2.15 V Raw Value: 17086 Voltage: 2.07 V

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!