xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Raspberry Pi GPIO 针脚图解教程 All In One

Raspberry Pi GPIO 针脚图解教程 All In One

Raspberry Pi & Raspberry Pi Pico

Raspberry Pi GPIO

图解 GPIO

image

image

https://www.raspberrypi.com/documentation/computers/os.html#gpio-and-the-40-pin-header

$ pinout

https://pinout.xyz

GPIO (General Purpose IO)
SPI (Serial Peripheral Interface)
I2C (Inter-integrated Circuit)
UART (Universal Asyncronous Receiver/Transmitter)
PCM (Pulse Code Modulation)
Ground
5v (Power)
3.3v (Power)

GPIO(通用IO
SPI(串行外设接口
I2C(内部集成电路
UART(通用异步接收器/发送器
PCM(脉冲编码调制
接地
5v(电源)
3.3v(电源)

针脚类型

image

  1. GPIO

gpiozero

https://gpiozero.readthedocs.io/en/stable/

  1. BCM

  2. PWM (pulse-width modulation)

脉冲宽度调制

  1. I2C

  2. SPI

  3. UART

  4. PCM

  5. Serial

  6. ADC

Analog Digita Converter / 模拟信号数字信号转换器

模拟信号 vs 数字信号

#!/usr/bin/env python3
# coding: utf8

# from machine import Pin, Timer
from machine import ADC
from utime import sleep
# import utime

# ADC 28 针脚
sensor_temp = ADC(28)

while True:
  # 读取 sensor 数据
  reading = sensor_temp.read_u16()
  print("reading =", reading)
  sleep(0.2)
  # utime.sleep(0.2)

# https://youtu.be/zlKJ5hvfs6s?t=541

https://electronoobs.com/eng_arduino_tut137.php

MicroPython

MPY: soft reboot
MicroPython v1.19.1-994-ga4672149b on 2023-03-29; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> help()
Welcome to MicroPython!

For online help please visit https://micropython.org/help/.

For access to the hardware use the 'machine' module.  RP2 specific commands
are in the 'rp2' module.

Quick overview of some objects:
  machine.Pin(pin) -- get a pin, eg machine.Pin(0)
  machine.Pin(pin, m, [p]) -- get a pin and configure it for IO mode m, pull mode p
    methods: init(..), value([v]), high(), low(), irq(handler)
  machine.ADC(pin) -- make an analog object from a pin # 从针脚制作一个模拟对象 ✅
    methods: read_u16()
  machine.PWM(pin) -- make a PWM object from a pin
    methods: deinit(), freq([f]), duty_u16([d]), duty_ns([d])
  machine.I2C(id) -- create an I2C object (id=0,1)
    methods: readfrom(addr, buf, stop=True), writeto(addr, buf, stop=True)
             readfrom_mem(addr, memaddr, arg), writeto_mem(addr, memaddr, arg)
  machine.SPI(id, baudrate=1000000) -- create an SPI object (id=0,1)
    methods: read(nbytes, write=0x00), write(buf), write_readinto(wr_buf, rd_buf)
  machine.Timer(freq, callback) -- create a software timer object
    eg: machine.Timer(freq=1, callback=lambda t:print(t))

Pins are numbered 0-29, and 26-29 have ADC capabilities
Pin IO modes are: Pin.IN, Pin.OUT, Pin.ALT
Pin pull modes are: Pin.PULL_UP, Pin.PULL_DOWN

Useful control commands:
  CTRL-C -- interrupt a running program
  CTRL-D -- on a blank line, do a soft reset of the board
  CTRL-E -- on a blank line, enter paste mode

For further help on a specific object, type help(obj)
For a list of available modules, type help('modules')
>>>
Meta-Z for help | 115200 8N1 | NOR | Minicom 2.8 | VT102 | Offline | cu.usbmodem14601

https://micropython.org/help/

https://www.cnblogs.com/xgqfrms/p/17275859.html

ADC

Analog-to-Digital Converter / 模数转换器

In electronics, an analog-to-digital converter (ADC, A/D, or A-to-D) is a system that converts an analog signal, such as a sound picked up by a microphone or light entering a digital camera, into a digital signal.

电子学中,模数转换器(ADC、A/D 或 A-to-D)是一种将模拟信号(例如麦克风拾取的声音或进入数码相机的)转换为数字信号

image

https://en.wikipedia.org/wiki/Analog-to-digital_converter

Analog-to-digital converters (ADCs)
Digital potentiometers (digipots)
Digital-to-analog converters (DACs)
Integrated & special-function data converters

模数转换器 (ADC)
数字电位器(digipots)
数模转换器 (DAC)
集成和特殊功能数据转换器

https://www.ti.com/data-converters/adc-circuit/overview.html

demos

GPIO 12 / BCM 12

Physical/Board pin 32
GPIO/BCM pin 12
Wiring Pi pin 26

物理/电路板引脚 32 ✅
GPIO/BCM 引脚 12 ✅
接线 Pi 引脚 26

image

https://pinout.xyz/pinout/pin32_gpio12

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

Raspberry Pi & LED

$ cat gpio.py
$ vim gpio.py
# 命令行传入参数
$ ./gpio.py 3

gpio.py

#!/usr/bin/env python3
# coding: utf8

import RPi.GPIO as GPIO
from datetime import datetime
import time
import sys

arg1 = sys.argv[1]
print("arg1 =", arg1);

# Python 获取时间戳 ✅
now = datetime.now()
# 转换为指定的格式
currentTime = now.strftime("%Y-%m-%d %H:%M:%S")
print("⏰ current datetime =", currentTime);

# $ pinout 命令查看,或 https://pinout.xyz/
# 指定 BCM 模式下的 GPIO 针脚编号是 12
# 对应的物理针脚编号是 32
PIN = 12
# BCM 模式
GPIO.setmode(GPIO.BCM)

# 指定 GPIO 针脚为一个电流输出针脚
GPIO.setup(PIN, GPIO.OUT)
# 输出低电平
GPIO.output(PIN, GPIO.LOW)

# index
i = 0
# 类型转换,str => int
n = int(arg1)
print("n =", n)
print('开始闪烁⏳')

while (i < n):
    print("i =", i)
    # 高电平,LED 点亮
    GPIO.output(PIN, GPIO.HIGH)
    # 休眠 1 秒,防止 LED 长时间点亮烧坏了
    time.sleep(1.0)
    # 低电平,LED 熄灭
    GPIO.output(PIN, GPIO.LOW)
    # 休眠 1 秒
    time.sleep(1.0)
    i = i + 1

# 输出低电平,LED 关闭
# GPIO.output(PIN, GPIO.LOW)
# 清除,释放内存
GPIO.cleanup()

print('结束闪烁 👌🏻')

crontab

$ crontab -l
# Edit this file to introduce tasks to be run by cron.

# 自动化执行脚本 ✅
# 每隔 3 分钟执行一次
# 分钟 小时 天 月 周
#3 * * * * pi sh ./Desktop/gpio.py
# user python 参数
# 3 * * * *  python ./gpio.py 3
# 绝对路径 python ✅
# 二合一输出 log 🚀

# 每小时的第3 分钟执行一次
# >> 追加 log, > 覆盖 log
*/30 * * * * /usr/bin/python /home/pi/Desktop/gpio.py 3 >> /home/pi/Desktop/gpio.log.txt 2>&1

# Vixie Cron 不支持非标准 cron bug ❌

# 每个小时,从0 开始,每隔 5 分钟执行一次 ✅
# */5 * * * * /usr/bin/python /home/pi/Desktop/five.py >> /home/pi/Desktop/five.log.txt 2>&1
# */10 * * * * /usr/bin/python /home/pi/Desktop/five.py >> /home/pi/Desktop/ten.log.txt 2>&1

# 用户 crontab(⚠️ 没有用户字段 pi )

$  cat gpio.log.txt
arg1 = 3
⏰ current datetime = 2023-04-13 14:00:01
n = 3
开始闪烁⏳
i = 0
i = 1
i = 2
结束闪烁 👌🏻
arg1 = 3
⏰ current datetime = 2023-04-13 14:30:01
n = 3
开始闪烁⏳
i = 0
i = 1
i = 2
结束闪烁 👌🏻
arg1 = 3
⏰ current datetime = 2023-04-13 15:00:01
n = 3
开始闪烁⏳
i = 0
i = 1
i = 2
结束闪烁 👌🏻
arg1 = 3
⏰ current datetime = 2023-04-13 15:30:01
n = 3
开始闪烁⏳
i = 0
i = 1
i = 2
结束闪烁 👌🏻
arg1 = 3
⏰ current datetime = 2023-04-13 16:00:01
n = 3
开始闪烁⏳
i = 0
i = 1
i = 2
结束闪烁 👌🏻
arg1 = 3
⏰ current datetime = 2023-04-13 16:30:01
n = 3
开始闪烁⏳
i = 0
i = 1
i = 2
结束闪烁 👌🏻
arg1 = 3
⏰ current datetime = 2023-04-13 17:00:01
n = 3
开始闪烁⏳
i = 0
i = 1
i = 2
结束闪烁 👌🏻
arg1 = 3
⏰ current datetime = 2023-04-13 17:30:01
n = 3
开始闪烁⏳
i = 0
i = 1
i = 2
结束闪烁 👌🏻
arg1 = 3
⏰ current datetime = 2023-04-13 18:00:01
n = 3
开始闪烁⏳
i = 0
i = 1
i = 2
结束闪烁 👌🏻

Raspberry Pi Pico GPIO

https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html

refs

Raspberry Pi Pico

https://www.cnblogs.com/xgqfrms/p/17274289.html



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-04-11 13:25  xgqfrms  阅读(220)  评论(2编辑  收藏  举报