Raspberry PI introduction requirements

1. Hardware

  • Raspberry PI Motherboard (Raspberry PI 3B+ for this column)
  • Monitor (regular monitor will do)
  • TF card
  • Dupont line (several, male to male, male to female, female to female, 40P)
  • Electric iron kit (in demand later)
  • One LED lamp (project requirement)

2. Software

  • Raspberry PI System (usually included with raspberry PI)
  • Xshell (log in to Raspberry PI, or use the built-in terminal on a MAC or Linux operating system)
  • FileZilla (FTP, upload files to raspberry PI, or upload files using terminal)
  • A programming language, raspberry PI support C++,python,nodejs,golang and other languages, in terms of the ecology of C++ and python the most perfect ecology, this tutorial is chosen python language

Installation environment

Start the raspberry pie

Insert the memory card of the pre-installed system, connect the power supply and the display, and start up.

SSH login

Connect to wifi, open the terminal, and enter ifconfig to view the IP address. Log in to the COMPUTER using SSH username @IP address. Generally, the initial login name of raspberry PI is PI

Installation environment

To install the Python development environment, use the Linux system installation method. The package manager is apT-get.

GPIO introduction

Compared with traditional computers, the biggest difference of raspberry PI is that GPIO (General Purpose Input /output), which is a programmable pin, can control the high and low levels of input and output to realize the control of external devices. The following figure is a comparison of two coding methods and pins of raspberry PI:

As you can see in the figure, there are two encoding formats for pins of raspberry PI. In this article, BCM is generally used.

1. Import GPIO modules

import RPi.GPIO as GPIO

2. Set the encoding format

GPIO.setmode(GPIO.BCM)

3. This setting will have some warning messages, which can be set to hide

GPIO.setwarnings(False)

Now that the interface is initialized, it’s time to complete the first raspberry PI application.

Light up the LED

Prepare an LED. At present, it is only a short time to test the function of GPIO, so it can be done without resistance.

Connect the raspberry PI to the LED

Cut a dupont wire from the middle, and connect the LED anode and cathode respectively at the breaking point. The other end is connected to the GPIO interface of raspberry PI, as shown in the figure below:

Select interfaces 5 and 7 of the physical interface, namely the fourth and fifth pins on the left of the position shown in the figure. It can be seen in the figure that the fourth pin on the left is no. 4 I/O port encoded by BCM, which can output high and low levels and is connected with the anode of THE LED. The fifth port on the left is GND, which is connected with the cathode.

Code section

First connect the above initialization interface, and then set the output mode of BCM code port 4.

GPIO.setup(4, GPIO.OUT)
Copy the code

Secondly, set the output high level of port 4

while True:
    GPIO.output(4.True)
Copy the code

Save the code as a py file, upload it to the PI via FileZilla, and run.

You can see that the LED has been lit. In order to verify the control function of GPIO, set the LED lighting mode below

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
GPIO.setwarnings(False)
while True:
    GPIO.output(4.True)
    time.sleep(1)
    GPIO.output(4, false)
    time.sleep(1)
Copy the code

Upload the code, run it, and you can see that the LED has gone into blinking mode.