raspberry pi_使用Raspberry Pi和GPIO引脚控制外部LED

raspberry pi

by Shahbaz Ahmed

Shahbaz艾哈迈德(Shahbaz Ahmed)

使用Raspberry Pi和GPIO引脚控制外部LED (Controlling an External LED using a Raspberry Pi and GPIO pins)

In this post we’ll explore Raspberry Pi GPIO pins by creating a “Hello World” GPIO program that results in a blinking red LED. We’ll be using the Python programming language. I am using a headless Raspberry Pi Zero WH (wireless with soldered headers) with Raspbian Stretch Lite (Raspberry Pi operating system with a minimal image based on Debian Stretch).

在本文中,我们将通过创建“ Hello World” GPIO程序来探索Raspberry Pi GPIO引脚,该程序会导致红色LED闪烁。 我们将使用Python编程语言。 我在Raspbian Stretch Lite(Raspberry Pi操作系统,基于Debian Stretch的最小映像)上使用了无头Raspberry Pi零WH(无线和焊接头)。

I will talk to my headless Pi using ssh and transfer necessary files from my Mac to Pi using scp commands. I am assuming you have your Raspberry Pi up and running with Raspbian OS installed. If not, then there are lot of articles on the Internet describing how to setup your Pi and install Raspbian, including the official Raspberry Pi documentation.

我将使用ssh与无头Pi交谈,并使用scp命令将必要的文件从Mac传输到Pi。 我假设您已经安装了Raspberry Pi并运行了Raspbian OS。 如果没有,那么Internet上有很多文章介绍如何设置Pi和安装Raspbian,包括官方的Raspberry Pi文档

Things you’ll need:

您需要做的事情:

  • 1 x Raspberry Pi (I am using the Pi Zero WH model)

    1 x Raspberry Pi(我使用的是Pi Zero WH模型)
  • 1 x bread board

    1个面包板
  • 1 x red LED light

    1个红色LED灯
  • 1 x 330 ohm resistor

    1个330欧姆电阻
  • 2 x female to male jumper cable

    2 x母对公跳线

GPIO引脚配置 (GPIO pins configuration)

GPIO stands for General Purpose Input Output. With the help of GPIO pins, a Raspberry Pi can connect and interact with external electronic components. Recent Raspberry Pi models (Pi 3, Pi Zero, Pi W and Pi WH models, and so on) contain 40 GPIO pins. Each pin can turn on or off, or go HIGH or LOW in electronic terms. If the pin is HIGH it outputs 3.3 volts, if the pin is LOW it is off.

GPIO代表通用输入输出 。 借助GPIO引脚,Raspberry Pi可以连接外部电子组件并与之交互。 最新的Raspberry Pi型号(Pi 3,Pi Zero,Pi W和Pi WH模型,等等)包含40个GPIO引脚。 每个引脚可以打开或关闭,或者以电子形式变为HIGHLOW 。 如果引脚为HIGH则输出3.3伏,如果引脚为LOW ,则关断。

In our example, we’ll be using pin 6 (ground) and pin 25. To know more about the GPIO pins in Raspberry Pi, checkout pinout.xyz.

在我们的示例中,我们将使用pin 6 (接地)和pin 25 。 要了解有关Raspberry Pi中GPIO引脚的更多信息,请查看pinout.xyz

设置电路 (Setting up the circuit)

You should turn the Pi off while building the circuit. We’ll create a circuit as depicted in the diagram below:

搭建电路时,您应该关闭Pi。 我们将创建一个电路,如下图所示:

Note: The resistor in the image is of 220 Ohm, but I have used 330 Ohm in my circuit.

注意 :图像中的电阻为220欧姆,但是​​我在电路中使用的是330欧姆。

  1. Use a female to male jumper cable to connect pin 6 (Ground) (black cable in the image above) to the breadboard negative row.

    使用母对公跳线将pin 6 (接地)(上图中的黑色电缆)连接到面包板负极行。

  2. Use another female to male jumper to connect to connect GPIO pin 25 to point represented by row A and column 12 on the breadboard as shown above (blue cable in the image above).

    使用另一个母对公跳线连接以将GPIO pin 25连接到面包板上的A行和第12列所表示的点,如上图所示(上图中的蓝色电缆)。

  3. Connect one end of a 330 ohm resistor to the negative row (the row that is highlighted in green where the black cable above earlier connected) and connect the other end to the point represented by row C column 11 on the breadboard as shown above.

    将330欧姆电阻器的一端连接到负极行(该行以绿色突出显示,其中前面的黑色电缆已连接到该行),另一端连接到面包板上C行第11列所表示的点。

  4. The shorter end of the LED is the negative end and the longer is the positive end. The longer end should always be connected to the point in the circuit with higher voltage (that is, higher potential). The shorter end of the LED is connected to a GPIO pin 25 (which can output 3.3V) via the blue cable and the longer end is connected to the ground pin 6 (which is 0V and acts like the negative terminal of the battery) via the black cable with a resistor in between them.

    LED的较短端是负端,而较长端是正端。 较长的一端应始终连接到电路中具有较高电压(即较高电势)的点。 LED的较短端通过蓝色电缆连接至GPIO pin 25 (可输出3.3V),而较长端则通过以下方式连接至接地pin 6 (其为0V,其作用类似于电池的负极端子)黑色电缆之间带有电阻。

电阻器 (Resistor)

Keeping in mind that I had taken introductory classes on electrical and electronics engineering quite some time ago (4 to 5 years approx.), I had two questions that I needed answers for. Please bear with me for being naive in this context.

请记住,很早以前(大约4至5年)我参加了电气和电子工程入门课程,我有两个问题需要解答。 请您忍受我在这种情况下的幼稚。

  1. Why do we need a resistor in our circuit?

    为什么在电路中需要电阻?
  2. How do we determine how many Ohms (the measure of electrical resistance) the resistor should be?

    我们如何确定电阻应为多少欧姆(电阻的量度)?

A resistor is required to dissipate the extra electrical energy (voltage) from the Raspberry Pi. Raspberry Pi is rated to supply 50mA at 3.3V. Let’s say our red LED can have a forward voltage (forward voltage is the “negative voltage,” used by the LED when it’s on) of around 2V and consumes 4mA current. So the remaining 1.3V should be dissipated by the resistor.

需要电阻来消散Raspberry Pi的多余电能(电压)。 Raspberry Pi的额定电压为3.3V,可提供50mA的电流。 假设我们的红色LED可以具有大约2V的正向电压(正向电压是指LED开启时使用的“负电压”),并消耗4mA电流。 因此,剩余的1.3V应该通过电阻来耗散。

Using Ohm’s law, V = IR, R = (3.3V - 2V) / (4/1000) which comes to around 325 ohms — so I recommend using a 330 ohm resistor.

根据欧姆定律, V = IRR = (3.3V - 2V) / (4/1000) ,约为325 ohms -因此,我建议使用330欧姆电阻

I discovered this from a Raspberry Pi forum discussion.

我是从Raspberry Pi论坛的讨论中发现的

Now that we have a complete circuit, the next part is to program the GPIO ports to make the magic happen: to get the LED to blink. We will be using the output of GPIO pin 25 to make the LED blink.

现在我们已经有了完整的电路,接下来的部分是对GPIO端口进行编程以使魔术发生:使LED闪烁。 我们将使用GPIO pin 25的输出来使LED闪烁。

Start your Pi and connect to it using ssh. In the terminal, use the following command to install the Python library gpiozero. The gpiozero library makes working with GPIO pins and connected external components very simple.

启动您的Pi并使用ssh连接到它。 在终端中,使用以下命令安装Python库gpiozerogpiozero库使使用GPIO引脚和连接的外部组件的工作非常简单。

To install the Python library, type sudo apt-get install python3-gpiozero.

要安装Python库,请输入sudo apt-get install python3-gpiozero

Now we will run some Python code. Save the code below onto your your Pi file system in a file named blink1.py. The script basically turns on the LED connected to pin 25, sleeps for 1 second, then turns off the LED, and again sleeps for 1 second. And this is done continuously in a loop until the program is terminated (pressing ctrl + c).

现在,我们将运行一些Python代码。 将下面的代码保存到您的Pi文件系统中,命名为blink1.py 。 脚本基本上会打开连接到pin 25的LED,Hibernate1秒钟,然后关闭LED,然后再次Hibernate1秒钟。 并且这将连续不断地进行直到程序终止(按ctrl + c )。

Now from terminal, go the the directory where the script is saved and run it using the command: python3 blink1.py.

现在,从终端转到保存脚本的目录,然后使用以下命令运行该脚本: python3 blink1.py

You will see the red LED blinking like this:

您将看到红色LED闪烁如下:

We can build lots of fun stuff using gpiozero using a similar setup. Check out the documentation for gpiozero which demonstrates some interesting examples. Try building a traffic light system.

我们可以使用gpiozero使用类似的设置来构建很多有趣的东西。 请查看gpiozero 的文档 ,其中展示了一些有趣的示例。 尝试构建交通信号灯系统。

Originally published at shahbaz.co on April 7, 2018.

最初于2018年4月7日发布在shahbaz.co

翻译自: https://www.freecodecamp.org/news/hello-gpio-blinking-led-using-raspberry-pi-zero-wh-65af81718c14/

raspberry pi

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Raspberry Pi Pico进行I2C通信时,可以使用C语言中的smbus库函数来控制I2C总线。具体操作步骤如下: 1. 导入smbus库头文件。 ``` #include "pico/stdlib.h" #include "hardware/i2c.h" ``` 2. 初始化I2C总线。 ``` i2c_init(i2c_port, speed); i2c_set_slave_mode(i2c_port, true, i2c_slave_address); ``` 其中,i2c_port表示要使用的I2C总线端口号,speed表示I2C总线的速度,i2c_slave_address表示I2C设备的地址。 3. 向I2C设备写入数据。 ``` uint8_t buf[] = {data}; i2c_write_blocking(i2c_port, i2c_slave_address, buf, sizeof(buf)); ``` 其中,buf表示要写入的数据,sizeof(buf)表示要写入的数据长度。 4. 从I2C设备读取数据。 ``` uint8_t buf[8]; i2c_read_blocking(i2c_port, i2c_slave_address, buf, sizeof(buf), false); ``` 其中,buf表示读取的数据缓冲区,sizeof(buf)表示要读取的数据长度,false表示不使用重复启动条件。 5. 向指定寄存器写入数据。 ``` uint8_t buf[] = {reg, data}; i2c_write_blocking(i2c_port, i2c_slave_address, buf, sizeof(buf)); ``` 其中,reg表示要写入数据的寄存器地址,data表示要写入的数据。 6. 从指定寄存器读取数据。 ``` uint8_t buf1[] = {reg}; uint8_t buf2[8]; i2c_write_blocking(i2c_port, i2c_slave_address, buf1, sizeof(buf1)); i2c_read_blocking(i2c_port, i2c_slave_address, buf2, sizeof(buf2), false); ``` 其中,buf1表示要写入的寄存器地址,buf2表示读取的数据缓冲区,sizeof(buf2)表示要读取的数据长度,false表示不使用重复启动条件。 需要注意的是,在使用Pico进行I2C通信时,需要先配置GPIO的功能,并且需要调整时钟频率和I2C设备的地址等参数。具体操作可以参考Pico的官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值