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
Component | Description | Link |
---|---|---|
Arduino Uno | Main microcontroller for reading the potentiometer and controlling the LED | Amazon Link |
LED | Light Emitting Diode for brightness control | Amazon Link |
220Ω Resistor | Limits current through the LED | Amazon Link |
Potentiometer | Variable resistor for controlling brightness | Amazon Link |
Jumper Wires | Connects components to Arduino | Amazon 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:
Connection | Description |
---|---|
Arduino Pin 9 | Connected to the anode of the LED through a resistor |
GND | Connected to the cathode of the LED |
Potentiometer | Connected to an analog input pin (A0) |
Arduino Circuit Connection
Use the following connections to set up the Arduino with the LED and potentiometer:
Arduino Pin | Component Pin | Connection Description |
---|---|---|
Digital Pin 9 | Anode of the LED | Controls LED brightness |
GND | Cathode of the LED (via resistor) | Completes the circuit |
A0 | Middle pin of the potentiometer | Reads analog value for brightness control |
GND | One outer pin of the potentiometer | Connects to ground |
5V | Other outer pin of the potentiometer | Connects 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