目录

树莓派4

树莓派

1 树莓派4

1.1 树莓派4模块图

1.2 树莓派4尺寸图

1.3 树莓派 4 GPIO引脚图

-w641

-w483

1.4 引脚说明

Vcc 电源正极
GND 电源负极
TX  发送数据
RX  接收数据

1.5 GPIO Version

pi-pi1 ➜  ~ sudo gpio readall
Oops - unable to determine board type... model: 17
pi-pi1  ➜  ~ gpio -v
gpio version: 2.50
Copyright (c) 2012-2018 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Raspberry Pi Details:
  Type: Unknown17, Revision: 01, Memory: 0MB, Maker: Sony
  * Device tree is enabled.
  *--> Raspberry Pi 4 Model B Rev 1.1
  * This Raspberry Pi supports user-level GPIO access.

【解决方案】
wget https://project-downloads.drogon.net/wiringpi-latest.deb

sudo dpkg -i wiringpi-latest.deb

sudo gpio readall

 +-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 | ALT0 | 1 |  3 || 4  |   |      | 5v      |     |     |
 |   3 |   9 |   SCL.1 | ALT0 | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | IN   | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | IN   | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 1 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |
 |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
 |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
 |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi 4B--+---+------+---------+-----+-----+

-w716

2 sensor

2.1 超声波

-w583

  • 接线方法
VCC[2] 电源正极,连接5V针脚
GND[6] 电源负极,连接任意板上任意GND针脚
Trig[11] 引脚用来接收树莓派的控制信号。接任意 GPIO 口。
Echo[12] 引脚用来向树莓派返回测距信息。接任意 GPIO 口。
#py code
import RPi.GPIO as gpio
import time
import numpy as np

def get_distance(setmode=gpio.BOARD,TRIG=35,ECHO=37,max_wait_time=5):
   '''
   calculate the distance between the obstacle and ultrasonic sensor (US-015)
   :param setmode: could be GPIO.BOARD or GPIO.BCM
   :param TRIG: trigger pin number for raspberry pie
   :param ECHO: echo pin number for raspberry pie
   :param max_wait_time: max wait time for receiving the signal
   :return:
   '''
   gpio.setmode(setmode)

   gpio.setup(TRIG, gpio.OUT)
   gpio.setup(ECHO, gpio.IN)
   gpio.output(TRIG,True)
   time.sleep(0.001)
   gpio.output(TRIG,False)
   wait_start = time.time()
   try:
      while gpio.input(ECHO) == False:
        ## exit if the wait is longer than max_wait_time
        if time.time()> wait_start + max_wait_time:
            return np.nan
        else:
            pass
      start = time.time()
      while gpio.input(ECHO) == True:
        end = time.time()

      ##clear the channel
      gpio.cleanup()

      sig_time = end - start
      # cm:
      distance = sig_time / 0.000058
      return distance

   except:

       gpio.cleanup()
       return np.nan



if __name__ == "__main__":
    TRIG = 38
    ECHO = 36
    setmode=gpio.BOARD
    #for i in range(20):
    while True:
        start = time.time()
        dist=get_distance(setmode,TRIG=TRIG,ECHO=ECHO)
        print("disitance: {} CM".format(dist))
        end = time.time()
        #print(end-start)
        time.sleep(1)

-w283