Control LED Brightness Using Potentiometer and Arduino with Python and PyFirmata - Beginner Guide

Control LED Brightness Using Potentiometer and Arduino with Python and PyFirmata - Beginner Guide

Objective

This guide demonstrates how to control LED brightness using a potentiometer and Arduino with the PyFirmata library. You will learn to read the potentiometer's value and adjust the brightness of an LED accordingly.

Project Goals

  • Establish a connection between Python and Arduino using PyFirmata.
  • Read the value from a potentiometer connected to the Arduino.
  • Control the brightness of an LED based on the potentiometer's value.

Required Components

ComponentDescriptionLink
Arduino UnoMain microcontroller for reading the potentiometer and controlling the LEDAmazon Link
LEDLight Emitting Diode for brightness controlAmazon Link
220Ω ResistorLimits current through the LEDAmazon Link
PotentiometerVariable resistor for controlling brightnessAmazon Link
Jumper WiresConnects components to ArduinoAmazon Link

Understanding the Circuit Basics

This project uses a potentiometer to adjust the brightness of an LED connected to the Arduino. Below is a description of the connections:

ConnectionDescription
Arduino Pin 9Connected to the anode of the LED through a resistor
GNDConnected to the cathode of the LED
PotentiometerConnected to an analog input pin (A0)

Arduino Circuit Connection

Use the following connections to set up the Arduino with the LED and potentiometer:

Arduino PinComponent PinConnection Description
Digital Pin 9Anode of the LEDControls LED brightness
GNDCathode of the LED (via resistor)Completes the circuit
A0Middle pin of the potentiometerReads analog value for brightness control
GNDOne outer pin of the potentiometerConnects to ground
5VOther outer pin of the potentiometerConnects to 5V for power

How This Arduino-Python Project Works

The Arduino reads the value from the potentiometer connected to A0. The value is then used to adjust the brightness of the LED connected to digital pin 9. The code reads the potentiometer value continuously and updates the LED brightness in real time.

Safety Tips for Beginners

  • Always use a current-limiting resistor with LEDs to prevent damage.
  • Ensure all connections are secure before powering on the Arduino.

Python Programming Section

Key Python Functions Used

  • Arduino('COM3') – Connects Python to the Arduino on the specified COM port.
  • led_pin.write(value) – Sets the PWM value for the LED (0.0 to 1.0 scale).
  • potentiometer_pin.read() – Reads the current value from the potentiometer.
  • time.sleep(seconds) – Pauses the program for a specified duration.

Setting Up the Python Environment

  • Install the PyFirmata library in Python by running: pip install pyfirmata.
  • Connect your Arduino to the computer and note the COM port it is connected to (e.g., COM3).

Python Code for Controlling LED Brightness Using Potentiometer


import pyfirmata
import time

# Change the port to the one your Arduino is connected to
board = pyfirmata.Arduino('COM3')

# Define the pin numbers
led_pin = board.get_pin('d:9:p')  # 'd' for digital, 9 is the pin number, 'p' for PWM
potentiometer_pin = board.get_pin('a:0:i')  # 'a' for analog, 0 is the pin number, 'i' for input

# Start an iterator thread so that the board doesn't overflow with data
it = pyfirmata.util.Iterator(board)
it.start()

try:
    while True:
        # Read the value from the potentiometer (0 to 1.0)
        pot_value = potentiometer_pin.read()

        if pot_value is not None:
            # Map the potentiometer value (0-1) to a PWM value (0-255)
            brightness = pot_value * 255
            led_pin.write(brightness / 255)  # Write PWM value to LED

except KeyboardInterrupt:
    # Handle user interruption (Ctrl+C) gracefully
    print("Program interrupted by user.")

finally:
    # Safely exit the program and close the connection to the Arduino
    board.exit()

How to Run the Python Code

  • Connect your Arduino to your computer and note the COM port (replace 'COM3' in the code with your port).
  • Install the PyFirmata library in Python by running pip install pyfirmata in the terminal.
  • Save the code to a Python file and run it using python your_script_name.py.

Expected Output

  • The LED brightness will change according to the potentiometer position.
  • Press Ctrl+C to stop the program.

Troubleshooting Tips

  • If the LED does not light up, ensure the connections are correct and the resistor is used.
  • Check that the correct COM port is specified in the code.

Suggestions for Beginners

If you're new to Arduino and Python, consider starting with simpler projects, such as LED on/off control or button presses. Understanding basic concepts will help in tackling more complex projects like this one.

Recommended Book for Learning Arduino

Arduino Programming for Absolute Beginners – This book offers clear, step-by-step Arduino projects that are ideal for building a solid foundation in Arduino programming and electronics.

For more free tutorials and Arduino projects, visit MechatronicsLab.net for resources on Arduino, ESP32, and more.

No comments

Theme images by Dizzo. Powered by Blogger.