如何通过BeagleBon上的GPIO管脚生成声音信号

2024-05-16 06:38:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找关于如何在BeagleBone上生成合成声音信号的指针/提示,类似于观察tone()函数将在Arduinos上返回。最后,我想连接一个压电或扬声器上的GPIO引脚和听到一个声波。有什么建议吗?在


Tags: 函数声音gpio信号建议beaglebone指针声波
3条回答

这就是我如何使用Python和PyBBIO在Beaglebone上解决这个问题的方法:

#!/usr/bin/python
# Circuit:
# * A Piezo is connected to pin 12 on header P8.        - GPIO1_12
# * A LED is connected to pin 14 on header P8.          - GPIO0_26
# * A button is connected to pin 45 on header P8.       - GPIO2_6
#   Use a pull-down resistor (around 10K ohms) between pin 45 and ground. 
#       3.3v for the other side of the button can be taken from pins 3 or 4 
#       on header P9. Warning: Do not allow 5V to go into the GPIO pins.
# * GND - pin 1 or 2, header P9.

def setup(): # this function will run once, on startup
    pinMode(PIEZO, OUTPUT) # set up pin 12 on header P8 as an output - Piezo
    pinMode(LED, OUTPUT) # set up pin 14 on header P8 as an output - LED
    pinMode(BUTTON, INPUT) # set up pin 45 on header P8 as an input - Button

def loop(): # this function will run repeatedly, until user hits CTRL+C
    if (digitalRead(BUTTON) == HIGH): 
        # was the button pressed? (is 3.3v making it HIGH?) then do:
            buzz()
    delay(10) # don't "peg" the processor checking pin

def delay(j):  #need to overwrite delay() otherwise, it's too slow
    for k in range(1,j):
            pass

def buzz():    #this is what makes the piezo buzz - a series of pulses
               # the shorter the delay between HIGH and LOW, the higher the pitch
    limit = 500     # change this value as needed; 
                    # consider using a potentiometer to set the value
    for j in range(1, limit):
            digitalWrite(PIEZO, HIGH)
            delay(j)
            digitalWrite(PIEZO, LOW)
            delay(j)
            if j==limit/2:
                    digitalWrite(LED, HIGH)
    digitalWrite(LED, LOW) # turn it off

run(setup, loop)

AM3359的GPIO引脚电压低,驱动器强度不足,无法直接驱动任何类型的传感器。你需要用运算放大器,晶体管或场效应晶体管来建立一个小电路。在

一旦完成了这项工作,您只需设置一个计时器循环,以所需频率更改GPIO线路的状态。在

到目前为止,最快和最简单的方式,从这个董事会音频接口是一个USB音频接口。在

签出this page。在userland(例如python)中,您可以通过写入/sys/class/gpio中的正确的sysfs文件,将pin设置为high或low。在

相关问题 更多 >