Raspberry Pi Potentiometer điều khiển độ sáng LED

Trong Raspberry Pi - Potentiometer Điều Khiển LED, chúng ta đã khám phá cách sử dụng potentiometer để điều khiển LED. Hướng dẫn này sẽ chỉ bạn cách sử dụng Raspberry Pi để điều chỉnh độ sáng của LED dựa trên giá trị đầu ra của potentiometer.

Linh Kiện Cần Thiết

1×Raspberry Pi 5
1×Chiết áp
1×Alternatively, 10k Ohm Trimmer Potentiometer
1×Mô-đun chiết áp có núm xoay
1×(Thay thế) Potentiometer Kit
1×(Thay thế) Potentiometer Module với Knob
1×mô-đun ADC ADS1115
1×LED Kit
1×LED (red)
1×LED Module
1×Điện trở 220 ohm
1×breadboard
1×Dây Jumper
1×(Khuyến nghị) Screw Terminal Block Shield for Raspberry Pi
1×(Khuyến nghị) Raspberry Pi Prototyping Base Plate & Breadboard Kit
1×(Khuyến nghị) HDMI Touch Screen Monitor for Raspberry Pi

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về LED và Potentiometer

Nếu bạn chưa quen thuộc với LED và potentiometer (bao gồm sơ đồ chân, chức năng, lập trình, v.v.), các hướng dẫn sau có thể giúp bạn:

Sơ Đồ Kết Nối

sơ đồ kết nối Raspberry Pi rotary chiết áp LED

This image is created using Fritzing. Click to enlarge image

Code Raspberry Pi

Các Bước Nhanh

  • Đảm bảo bạn đã cài đặt Raspbian hoặc hệ điều hành tương thích khác cho Raspberry Pi.
  • Đảm bảo Raspberry Pi của bạn được kết nối với cùng một mạng cục bộ với PC của bạn.
  • Đảm bảo Raspberry Pi của bạn được kết nối internet nếu bạn cần cài đặt một số thư viện.
  • Nếu đây là lần đầu tiên bạn sử dụng Raspberry Pi, hãy xem Raspberry Pi - Cài Đặt Phần Mềm
  • Kết nối PC của bạn với Raspberry Pi qua SSH sử dụng SSH client tích hợp trên Linux và macOS hoặc PuTTY trên Windows. Xem cách kết nối PC với Raspberry Pi qua SSH.
  • Đảm bảo bạn đã cài đặt thư viện RPi.GPIO. Nếu chưa, hãy cài đặt bằng lệnh sau:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Cài đặt thư viện Adafruit_ADS1x15 bằng cách chạy các lệnh sau trên terminal Raspberry Pi của bạn:
sudo pip install Adafruit-ADS1x15
  • Tạo file script Python potentiometer_fade_led.py và thêm code sau:
# Mã Raspberry Pi này được phát triển bởi newbiely.vn # Mã Raspberry Pi này được cung cấp để sử dụng công khai, không có ràng buộc. # Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: # https://newbiely.vn/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()
  • Lưu file và chạy script Python bằng cách thực hiện lệnh sau trong terminal:
python3 potentiometer_fade_led.py
  • Xoay potentiometer.
  • Quan sát độ sáng của LED.
  • Kiểm tra kết quả trên 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

Script chạy trong vòng lặp vô hạn liên tục cho đến khi bạn nhấn Ctrl + C trong terminal.

Video Tutorial

Việc sản xuất video tốn rất nhiều thời gian. Nếu video hướng dẫn hữu ích cho việc học của bạn, hãy đăng ký kênh YouTube để ủng hộ. Nếu nhu cầu đủ cao, chúng tôi sẽ cố gắng làm thêm nhiều video.