我们使用一些必要的Cookie来确保网站正常运行。

我们使用可选的 Cookie,具体详情请参阅我们的Cookie政策,用于记住您的设置并了解您如何使用我们的网站。

打造互联网收音机

本教程收录于Raspberry Pi Press出版的书籍《Simple electronics with GPIO Zero》。本书针对最新Raspberry Pi设备进行了更新,包含了使用Raspberry Pi GPIO引脚创建电子项目所需的所有信息。项目采用Python语言和GPIO Zero库编写,包括LED灯、运动感应报警器、测距仪、激光绊线和Raspberry Pi机器人。

在Raspberry Pi Official Magazine#167,我们曾利用电容的一个小技巧来获取模拟传感器的读数。Raspberry Pi检测模拟输入的最佳且最可靠的方法是使用模数转换器(ADC)芯片,例如MCP3008,它提供八个输入通道,可连接传感器和其他模拟输入。这不仅省去了电容,还能让您在传感器输出的整个范围内获得更精确、更即时的读数。

在本教程中,我们将把一个电位器连接到MCP3008,通过旋转旋钮来控制LED的亮度。然后,我们将添加第二个电位器并制作一个互联网收音机,使用这两个电位器来切换电台和调节音量。

包含两个电位器的最终电路

启用SPI

ADC芯片的模拟值将通过SPI协议传输到Raspberry Pi。虽然这在GPIO Zero中开箱即用,但如果您启用完整的SPI支持,可能会获得更好的效果。首先,确保Pythonspidev package is installed (it should be by default). Open a terminal window and enter:

$ sudo 合适 安装python3-spidev

接下来,点击Raspberry Pi菜单,选择“Preferences”,打开“Control Centre”,然后在“Interfaces”部分启用SPI。点击“OK”并重启Raspberry Pi。您也可以通过运行以下命令来使用命令行工具sudo raspi-config from the command prompt, going to Interface Options, enabling SPI, and rebooting your Raspberry Pi.

连接ADC

与往常一样,在搭建电路时需要关闭Raspberry Pi。正如您从图1中看到的,将MCP3008 ADC连接到Raspberry Pi的GPIO引脚需要相当多的接线。

首先,将MCP3008放置在面包板中间,跨接在中央凹槽上。现在按照图示连接跳线。两根连接到“+”电源轨,并连接至3V3引脚;另外两根通过“-”电源轨连接至GND引脚。ADC的四个中间引脚分别连接到GPIO 8 (CE0)、10 (MOSI)、9 (MISO)和11 (SCLK)。

图1:在面包板上连接ADC

读取数值

将ADC连接到Raspberry Pi后,您可以将设备连接到其八个输入通道(编号0至7)。这里,我们将连接第一个(最左侧)电位器,它是一个可变电阻:当您旋转其旋钮时,Raspberry Pi会读取电压(从0V到3.3V)。我们可以用它来控制其他组件,例如LED。如图1所示,将电位器的一个外侧引脚(左下)连接到“+”电源轨,另一侧连接到“-”接地轨,中间引脚连接到MCP3008的第一个输入:通道0。

现在我们可以用Python读取电位器的值了。创建一个新文件,然后将以下代码另存为test_pot.py,然后运行它。

from gpiozero import MCP3008

pot = MCP3008(channel=0)

while True:

    print(pot.value)

在顶部我们导入了MCP3008 class from GPIO Zero, then set the pot variable to the ADC’s channel 0. A while True: loop then continuously displays the potentiometer’s value (from 0 to 1) on the screen; try turning it as the code runs to see the number change. Press CTRL+C来退出。

点亮LED

接下来,我们将按照图2中的方法,在电路中添加一个LED,将其较长的(正极)引脚连接到GPIO 21,较短的引脚通过电阻连接到‘–’接地轨。创建一个新文件,输入以下代码,并将程序另存为source_values.py.

from gpiozero import MCP3008, PWMLED

from signal import pause

pot = MCP3008(0)

led = PWMLED(21)

led.source = pot.values

pause()

该代码导入了MCP3008 and PWMLED classes, as well as the signal module’s pause function. The MCP3008 class enables us to control the brightness of an LED using pulse-width modulation (PWM). We create a PWMLED object on GPIO 21, assigning it to the led variable. We assign our potentiometer to channel 0, as before. Finally, we use GPIO Zero’s clever source and values system to pair the potentiometer with the LED, to continuously set the latter’s brightness level to the former’s value. Run the code and turn the knob to adjust the LED’s brightness. Press CTRL+C来退出程序。

图2:添加到面包板上的LED和电位器

添加第二个电位器

如果还没有,请按照图2中的方法,在电路中添加第二个电位器,将其中间引脚连接到MCP3008的通道1。现在我们将使用两个电位器来控制LED的闪烁频率。在Thonny中,创建一个新文件,输入以下代码并将其另存为two_pots.py.

from gpiozero import MCP3008, PWMLED

pot1 = MCP3008(0)
pot2 = MCP3008(1)
led = PWMLED(21)
while True:
    print(pot1.value, pot2.value)
    led.blink(on_time=pot1.value)
off_time=pot2.value, n=1, background=False)

在这里,我们创建了两个独立的pot1 and pot2 variables, assigned to the ADC’s channels 0 and 1 respectively. In a while True: loop, we then print the two values on the screen and make the LED blink, with its on and off times affected by our two potentiometers. Run the code and twist both knobs to see how it changes.

安装VLC

我们将使用相同的电路制作一个简单的网络收音机,用一个电位器切换电台,另一个调节音量。如果默认未安装,您需要安装VLC媒体播放器才能播放M3U网络收音机流。打开终端窗口并输入:

$ sudo 合适 安装vlc

制作收音机

创建一个新文件,输入radio_new.py上方列表中所示的代码,并以该名称保存。

首先,我们导入MCP3008 class, along with Popen,  run, and time; Popen will enable us to start and stop VLC. We create variables for the station and volume dials, on ADC channels 0 and 1 respectively. We then assign variables to two radio stream URLs (we’ve used SomaFM Groove Salad and Indie Pop in this example).

接下来,我们创建几个函数。第一个,set_volume, uses wpctl (the command-line control tool for WirePlumber, the PipeWire session manager) to control the volume. The second,  change_station, includes an if condition so it only triggers when the station set by the first potentiometer position is different from the currently selected one (current_station). If so, it stops the current stream and starts playing the new one, before reassigning the current_station variable to it.

最后,在一个while True: loop, inside a try … except block, we read each potentiometer in turn and use if conditional statements to call the related functions, set_volume and change_station, when required.

运行代码,尝试旋转两个电位器来切换电台和调节音量。为了保持简单,本示例中我们只使用了两个电台,但您可以轻松添加更多,只需调整station.dial.value thresholds accordingly.

得益于try … except block, pressing CTRL+C在退出程序前停止收音机流媒体。

下载完整代码.

《Simple electronics with GPIO Zero》

本教程收录于Raspberry Pi Press出版的书籍《Simple electronics with GPIO Zero》。本书针对最新Raspberry Pi设备进行了更新,包含了使用Raspberry Pi GPIO引脚创建电子项目所需的所有信息。项目采用Python语言和GPIO Zero库编写,包括LED灯、运动感应报警器、测距仪、激光绊线和Raspberry Pi机器人。

7条评论
跳转到评论表单

Andrew Beasley avatar

两个电位器的代码不正确——它只是第一个电位器读取代码的重复。

回复 Andrew Beasley

Ashley avatar

哎呀,出错了。现在已经修复了。(应该是吧)。谢谢 Andrew!

回复 Ashley

Andrew Beasley avatar

谢谢——这样合理多了 :-)

回复 Andrew Beasley

Brian avatar

如何让 VLC 在开机时自动启动?

回复 Brian

DistroEx avatar

“创建一个新文件,输入上方 radio_new.py 列表中所示的代码……”上方并没有 radio_new.py 列表。如果想查看,必须前往“下载完整代码”链接下给出的 Github 网站,并导航至 ch09。

回复 DistroEx

DeeJay avatar

“新建一个文件,输入上面 radio_new.py 代码清单中所示的代码”

上面并没有 ‘radio_new.py’ 的代码清单。整篇文章中也没有。

‘two_pots.py’ 代码清单看起来有‘问题’。off_time 赋值在 while 循环之外,因此永远不会被执行,而且括号不匹配。

回复 DeeJay

darmco avatar

当需要从 Raspberry Pi 获取可靠的模拟读数时,MCP3008 是一个极佳的选择。使用 SPI 还使得该设置在实际项目中更具实用性,尤其是需要同时监控多个模拟输入的场景。

回复 darmco

发表评论