Raspberry Pi Potentiometer Điều Khiển LED

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

Linh Kiện Cần Thiết

1×Raspberry Pi 5
1×Potentiometer (biến trở)
1×Alternatively, 10k Ohm Trimmer Potentiometer
1×(Thay thế) Potentiometer Kit
1×(Thay thế) Potentiometer Module with Knob
1×mô-đun ADC ADS1115
1×LED Kit
1×LED (red)
1×LED Module
1×Điện trở 220 ohm
1×breadboard (bo mạch thí nghiệm)
1×dây jumper (dây nối)
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 với LED và potentiometer (bao gồm pinout, chức năng, lập trình, v.v.), các hướng dẫn sau sẽ giúp bạn:

Sơ Đồ Đấu Nối

sơ đồ đấu nối Raspberry Pi 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 trên Raspberry Pi.
  • Đảm bảo Raspberry Pi của bạn được kết nối với cùng mạng cục bộ với PC.
  • Đảm bảo Raspberry Pi của bạn có kết nối internet nếu cần cài đặt thư viện.
  • Mới sử dụng Raspberry Pi? Hãy bắt đầu với hướng dẫn Raspberry Pi - Cài Đặt Phần Mềm để học các kiến thức cơ bản trước.
  • Kết nối PC với Raspberry Pi qua SSH sử dụng SSH client có sẵn 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:
sudo pip install Adafruit-ADS1x15
  • Tạo file script Python potentiometer_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-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 # Set up GPIO for the LED GPIO.setmode(GPIO.BCM) GPIO.setup(LED_PIN, GPIO.OUT) # Define the threshold value THRESHOLD = 1000 # Adjust this threshold value as needed # Main loop to read and display the analog value 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)) # Check if the analog value is above the threshold if raw_value > THRESHOLD: GPIO.output(LED_PIN, GPIO.HIGH) # Turn on the LED else: GPIO.output(LED_PIN, GPIO.LOW) # Turn off the LED # Add a delay between readings (adjust as needed) time.sleep(1) except KeyboardInterrupt: print("\nExiting the program.") # Clean up GPIO on exit GPIO.cleanup()
  • Lưu file và chạy script Python bằng cách thực thi lệnh sau trong terminal:
python3 potentiometer_led.py

Script sẽ chạy trong vòng lặp vô hạn cho đến khi bạn nhấn Ctrl + C trong terminal.

  • Xoay potentiometer
  • Quan sát sự thay đổi trạng thái của LED

Giải Thích Code

Hãy xem giải thích từng dòng trong phần comment của source code!

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.