Example 2: Raspberry Pi GPIO controls external LED light flashing

Example 2: Raspberry Pi GPIO controls external LED light flashing

Purpose

  1. Through background knowledge learning, learn about the micro control computer equipped on the quadruped robot mini pupper: Raspberry Pi.
  2. Through learning Raspberry Pi GPIO operations, you will become familiar with GPIO read and write control.
  3. By controlling the external LED lights on and off, you can become familiar with the Raspberry Pi's reading and output of external signals.

Experimental requirements

Using Python language programming, through the control of the GPIO on the Raspberry Pi, the external LED light is periodically changed on and off, causing it to continuously flash bright and dark with a period of one second.

experimental knowledge

1. What is Raspberry Pi?

Raspberry Pi is a low-cost micro single-board computer developed by the British Raspberry Pi Foundation. The hardware is equipped with an SoC (system on a chip), has a GPU and RAM, and supports wired and wireless connection solutions. It is commendable that the Raspberry Pi has complete community and software support, which greatly facilitates the entry-level learning of robot development beginners.
The Raspberry Pi has the basic functions of a computer while maintaining the size of a card, which means you can easily and conveniently build your own robot programming platform and deploy this microcomputer on the small and cute four-legged robot dog mini. Pupper on.
Please add image description
Picture 1: Raspberry Pi 4B
Reference link: Raspberry Pi

2. What is GPIO?

There are a wealth of interfaces on the Raspberry Pi, which allow the Raspberry Pi to easily communicate and control external devices, such as USB, CAMERA, GPIO, etc.
Among them, the GPIO interface is called General Purpose Input Output, which means a general-purpose input and output port. The GPIO interface on the Raspberry Pi can be used for input, output or other special functions.
Through GPIO, the Raspberry Pi can interact with different external module components, such as buttons, buzzers, servos, etc.

Insert image description herePicture 2: GPIO interface diagram

3. Raspberry Pi GPIO pin diagram

View the Raspberry Pi GPIO pin diagram from the command line.

pinout
#	如果你希望学习在命令行中对GPIO口进行更复杂的操作,可查看课程的进阶参考文档中实例2部分

You will see
Picture 3: The output result of the pinout command.
Reference link: Raspberry Pi 4B pin diagram

4. Basic usage of RPi.GPIO library

GPIO.setmode(GPIO.BOARD)	#	初始化GPIO引脚编码方式,需放在代码正式开始处
#	GPIO引脚有多种编码方式,比如BCM、wiringPi、BOARD等,方便起见,课程均采用BOARD编码模式
GPIO.setup(12, GPIO.OUT)	#	初始化GPIO引脚设置,需放在代码正式开始处
GPIO.setup(12, GPIO.IN)	#	12为引脚号 GPIO.IN或者GPIO.OU为输入输出模式
print(GPIO.input(12))	#	GPIO.input查看GPIO输入的电平信号
#	GPIO.HIGH为高电平 ,GPIO.LOW为低电平 也可用于条件判断作其他操作
#	GPIO.output向端口发送高低电平信号
GPIO.output(12, GPIO.HIGH)	#	此处以12号端口为例
GPIO.output(12, GPIO.LOW)
GPIO.cleanup()	#	在使用完GPIO后要作清理,避免后续引脚被占用

Reference link: Python RPi.GPIO library
Reference link: GPIO Input detailed explanation

5. mini pupper learning kit-use of LED module

The LED module in the mini pupper learning kit comes with on-board current limiting resistor, which can protect the LED module.
The wiring is as follows:

pin effect Raspberry party response
GND ground GND port
R red light positive PWM port
G Green light positive PWM port
B Blue light positive electrode PWM port

Experimental steps

1.Hardware connection

  1. Connect any positive pole (R/G/B/Y) of the light to the PWM port on the Raspberry Pi (for example, port 33). Only
    some of the GPIOs on the Raspberry Pi are PWM ports. You can check the GPIO pin diagram to confirm which port is the PWM port.

  2. Connect the GND terminal of the lamp to the GND port on the Raspberry Pi (for example, port 34).
    You can confirm which port is the GND terminal by looking at the pin diagram of the Raspberry Pi GPIO.

2. Write the Python program led_blink_GPIO.py

#!/usr/bin/python
# coding:utf-8
# led_blink_GPIO.py
# 使树莓派GPIO外接的LED灯的亮灭状态周期性地改变,发生周期为两秒的持续明暗闪烁。

import RPi.GPIO as GPIO
import time

# GPIO初始化
LED = 33
period = 2
GPIO.setmode(GPIO.BOARD)  # 初始化GPIO引脚编码方式
GPIO.setup(LED, GPIO.OUT)
# 如果你需要忽视引脚复用警告,请调用GPIO.setwarnings(False)
# GPIO.setwarnings(False)
print('预设置完成,开始闪烁,端口为%d,周期为%d秒。' % (LED, period))

while True:
    GPIO.output(LED, GPIO.HIGH)
    time.sleep(period/2)
    GPIO.output(LED, GPIO.LOW)
    time.sleep(period/2)

GPIO.cleanup()

3. Run the program and observe the effect

Execute the following commands in the directory of led_blink_GPIO.py:

sudo python led_blink_GPIO.py

At this time, you should observe that the on and off status of the Raspberry Pi status indicator light changes periodically, with a continuous light and dark flashing period of one second.

Picture 3: GIF picture LED flashing experimental effect
Insert image description here

Experiment summary

After studying and experimenting with this knowledge point, you should be able to reach the following levels:

Knowledge points content learn familiar master
raspberry pie The origin, purpose and basic hardware performance of Raspberry Pi
raspberry pie Raspberry Pi GPIO
GPIO Usage of RPi.GPIO library
hardware How to use the LED module of the mini pupper learning kit

Copyright information: The teaching materials are not yet complete. Copyright information processing methods are reserved here.
Mini pupper related content can be accessed: https://github.com/mangdangroboticsclub

Guess you like

Origin blog.csdn.net/m0_56661101/article/details/129114034