找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 778|回复: 0
打印 上一主题 下一主题
收起左侧

树莓派3b控制5v八路继电器遇到的问题

[复制链接]
跳转到指定楼层
楼主
我用的是树莓派3b,想控制一个5v八路继电器,代码逻辑是让八个继电器依次切换为高电平,以顺序循环。
但是我接好线后,继电器的In1 In2两个小灯长亮。
运行脚本,只听得到另外六个继电器的打开和关闭的哒哒声,而且只能控制一轮,之后就会失效,也无法控制后续电路工作。

排查一天了,不知道问题出在哪里....希望有人能发现问题

##################################代码如下###################################
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, 下载次数: 39)

连线图

连线图

20211207_145808.jpg (3.4 MB, 下载次数: 37)

未运行脚本两个小灯常亮

未运行脚本两个小灯常亮
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶1 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表