Raspberry Pi Potentiometer Relay

Hướng dẫn này sẽ hướng dẫn bạn cách sử dụng Raspberry Pi và potentiometer để điều khiển relay. Chi tiết:

Bằng cách kết nối relay với bóng đèn, dải LED, motor hoặc thiết bị chấp hành, chúng ta có thể sử dụng Raspberry Pi và potentiometer để điều khiển bóng đèn, dải LED, motor hoặc thiết bị chấp hành.

Phần cứng cần chuẩn bị

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 with Knob
1×mô-đun ADC ADS1115
1×Relay
1×(Tùy chọn) Solenoid Lock
1×12V Power Adapter
1×DC Power Jack
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)

Giới thiệu về Relay và Potentiometer

Nếu bạn chưa quen thuộc với relay và potentiometer (bao gồm pinout, hoạt độ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

Raspberry Pi chiết áp rơ le sơ đồ đấu dây

This image is created using Fritzing. Click to enlarge image

Code Raspberry Pi

Các Bước Thực Hiện

  • Đảm bảo bạn đã cài đặt Raspbian hoặc hệ điều hành tương thích với Raspberry Pi khác trên Pi của bạn.
  • Đả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 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, 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 của bạn 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 nó 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 Python script potentiometer_relay.py và thêm đoạn 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-relay 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 relay RELAY_PIN = 20 # Set up GPIO for the relay GPIO.setmode(GPIO.BCM) GPIO.setup(RELAY_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(RELAY_PIN, GPIO.HIGH) # Turn on the relay else: GPIO.output(RELAY_PIN, GPIO.LOW) # Turn off the relay # 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 Python script bằng cách thực hiện lệnh sau trong terminal:
python3 potentiometer_relay.py

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.

  • Xoay potentiometer
  • Kiểm tra sự thay đổi trong trạng thái của relay

Giải Thích Code

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

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.