找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 787|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

樹莓派3b控制5v八路繼電器遇到的問題

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:989308 發(fā)表于 2021-12-7 20:55 | 只看該作者 |只看大圖 回帖獎勵 |正序瀏覽 |閱讀模式
我用的是樹莓派3b,想控制一個5v八路繼電器,代碼邏輯是讓八個繼電器依次切換為高電平,以順序循環(huán)。
但是我接好線后,繼電器的In1 In2兩個小燈長亮。
運行腳本,只聽得到另外六個繼電器的打開和關(guān)閉的噠噠聲,而且只能控制一輪,之后就會失效,也無法控制后續(xù)電路工作。

排查一天了,不知道問題出在哪里....希望有人能發(fā)現(xiàn)問題

##################################代碼如下###################################
import RPi.GPIO as GPIO
import time
import numpy as np

# Set the BPM (speed) here
bpm = 120

# Each line represents a beat. Each 0 or 1 is a solenoid
sequence = [
  [1, 0, 0, 0, 0, 0, 0, 0],
  [0, 1, 0, 0, 0, 0, 0, 0],
  [0, 0, 1, 0, 0, 0, 0, 0],
  [0, 0, 0, 1, 0, 0, 0, 0],
  [0, 0, 0, 0, 1, 0, 0, 0],
  [0, 0, 0, 0, 0, 1, 0, 0],
  [0, 0, 0, 0, 0, 0, 1, 0],
  [0, 0, 0, 0, 0, 0, 0, 1],
  [1, 1, 1, 1, 1, 1, 1, 1],
  [1, 1, 1, 1, 1, 1, 1, 1],
  [1, 1, 1, 1, 1, 1, 1, 1],
  [1, 1, 1, 1, 1, 1, 1, 1],
]

gpio_map = [2, 3, 4, 17, 27, 22, 10, 9]

# The Lenth of time each solenoid should be activated
active_duration = 0.01

############################################# The main script#############################################

# Sets pin number to BCM mode
GPIO.setmode(GPIO.BCM)

# Calculate the time period between the solenoid being deactivated and the next beat starting
beat_gap = (float(60) / float(bpm)) - float(active_duration)


# Generator to infinitely loop around the sequence
def infinite_generator(n):
    i = 0
    while True:
        if i >= len(n):
            i = 0

        yield n[ i]
        i = i + 1

# Loop through each pin and set the mode and state to 'low'
for i in gpio_map:
    GPIO.setup(i, GPIO.OUT)
    GPIO.output(i, GPIO.HIGH)

# Run the infinite loop
try:
    for beat in infinite_generator(sequence):
        # Get active drum numbers
        active = np.where(beat)[0]
        # Get pin numbers for active drums
        pins = [gpio_map[ i] for i in active]

        print('Activating Pins ', pins)
        GPIO.output(pins, GPIO.LOW)
        time.sleep(active_duration)
        GPIO.output(pins, GPIO.HIGH)
        print('Sleep ', beat_gap)
        time.sleep(beat_gap)

# End
except KeyboardInterrupt:
    print ('Quit')
    # Reset GPIO settings
    GPIO.cleanup()

SZ6`7($WADIM[Z55)4ZG6JI.png (340.02 KB, 下載次數(shù): 39)

連線圖

連線圖

20211207_145808.jpg (3.4 MB, 下載次數(shù): 37)

未運行腳本兩個小燈常亮

未運行腳本兩個小燈常亮
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂1 踩
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表