Raspberry Pi website Study record

Source: Internet
Author: User

Raspberry Pi official website learning record and Gpio zero together we're going to do

The row pin on the Raspberry Pi side is called the Universal input/Output pin (GPIO)

These pins allow the Raspberry Pi to control things in reality. You can connect the components to these pins: the output device is like an LED that can be switched on or off, or an input device like a button or sensor that can be used as a trigger event, such as when a button is pressed, a LED is lit.

By using the Gpio Zero Library, you can easily control the gpio pin of the Raspberry Pi.

We will learn the

By completing this resource you will learn:

    1. How to connect the LEDs and buttons to the Raspberry Pi Gpio
    2. How to control the output of the Gpio pin via Gpio Zero
    3. How to read the input of a gpio pin via Gpio Zero
We need it.

Hardware:

In addition to the Raspberry Pi and conventional peripherals with SD cards, a non-solder breadboard (solderless breadboard), two male to female DuPont lines, a button, an LED

Software:

In order to prepare this resource, you will need an updated SD card image:

First, update the list of system packages by typing the following command in Lxterminal or from the command line:

sudo apt-get update

Next, upgrade all installed packages to the latest version by using the command:

sudo apt-get upgrade

Usually, regular operations can keep your installation up-to-date so that it will be equivalent to the latest release image in Raspberrypi.org/downloads.

However, there are occasional changes to join the underlying Raspbian image that requires manual intervention, such as a recently proposed package. There is an upgrade that cannot be installed, and for this command only the packages that you have installed are upgraded.

Upgrading the kernel and framework

The kernel and framework are installed as a Debian package and will be upgraded through the steps above. These packages will often be upgraded after extensive testing.

Run space

When executing sudo apt-get upgrade, it will tell us how much data will be downloaded and how much space would occupy the SD card. It can be checked with df–h so that you have enough free space, when the unfortunate apt will not do this for you. Also note that the downloaded package file (. deb extension) is already in the/var/cache/apt/archives directory. You can use sudo apt-get clean to remove them to free up space.

You will also need to install the following additional software:

GPIO Zero

In order to install the software you need, execute the following command in Lxterminal:

sudo apt-get install Python3-pip python3-w1thermsensor python3-spidev

sudo pip-3.2 install Gpiozero

Begin

1.GPIO Pin

The Raspberry Pi has 40 pins, and these pins provide different functions.

If you have a raspio pin label, it can help you define what each pin is used to do. Make sure your copper hole is facing towards the USB port and facing outward.

If you do not have a raspio pin label, this guide will help you define the PIN number:

You will see the pins labeled 3v3, 5V, GND and GP2, GP3, etc.

3V3 ?

3.3 V ?

The thing that gets this pin can always get 3.3v Voltage

5V ?

3 0

The thing that gets this pin can always get 5v Voltage

GND ?

To

0v used to complete a circuit

GP2 ?

GPIO Pin 2

These pins are generic and can be configured as input or output

ID_SC/ID_SD /DNC

Special purpose pins

??? ?

??

2. Light up an LED

Try connecting an LED and a resistor in series to the Raspberry Pi 3v3 and GND pins

This led will be lit. It will be always bright because it is connected to a 3v3 pin.

Now try moving it from 3v3 to Gpio pin 17:

This led should not be lit now, but it is now on a gpio pin, so it can be controlled by the code.

3. Switch an LED

Gpio Zero is a new Python library that provides a simple interface to everyday gpio components.

(1). Open Python 3 from the main menu;

(2). You can switch an led by typing the command directly into the Python interpreter window (also called the Python shell). First let's import this Gpio Zero library. You also need to tell the Raspberry Pi which gpio pin you are using – in this case the pin 17 is used. Next is the chevrons type:

    1. From Gpiozero import LED
    2. LED = LED (17)

      ?

Press ENTER on the keyboard.

(3). Let the LED open, type the following statement and press ENTER:

    1. Led.on ()

?

(4). Let the LED off, you can type:

    1. Led.off ()

?

4. Complete an LED

Through the time library and a small loop, you can be led flashing.

(1). Create a new document by clicking File > New file

(2). Save a new file by clicking File > Save

(3). Save the file as gpio_led.py

(4). Type the following code to get started:

    1. From Gpiozero import LED
    2. From time import sleep
    3. ?
    4. LED = LED (17)
    5. ?
    6. while True:
    7. ???? Led.on ()
    8. ???? Sleep (1)
    9. ???? Led.off ()
    10. ???? Sleep (1)

      ?

(5). Use CTRL + S to save and execute code through F5

(6). The LED should start flashing, press CTRL + C on the keyboard to exit the project

5. Use the button to get the input

???? Now that you can control an output element-led, let's connect and control an INPUT element-the button

???? (1). Connect one button to another GND pin and GPIO pin 2 as

????

(2). Create a new document by clicking File > New file

(3). Save a new file by clicking File > Save

(4). Save the file as gpio_button.py

(5). This time we need the button class to tell the Raspberry Pi button on pin 2. Type the following code in your new file:

    1. From Gpiozero import Button
    2. button = button (2)

?

(6). Now you can get a project to do something when the button is pressed. Add the following code:

    1. Button.wait_for_press ()
    2. Print (' You pushed me ')

?

(7). Use CTRL + S to save and execute code through F5

(8). Press the button text to appear

6. Manual LED Control

???? You can now combine two items written to the current position to control the LEDs via a button.

(1). Create a new document by clicking File > New file

(2). Save a new file by clicking File > Save

(3). Save the file as gpio_control.py

(4). Type the following code:

    1. From Gpiozero import LED, Button
    2. From time import sleep
    3. ?
    4. LED = LED (17)
    5. button = button (2)
    6. ?
    7. Button.wait_for_press ()
    8. Led.on ()
    9. Sleep (3)
    10. Led.off ()

?

(5). Save and run your project. When you press the button the LED should light for 3 seconds

7. Make a switch

Make a switch, press and release the button to light the LED, and once again press and release the button will turn off the LED.

(1). Modify your code to make it look like this. You are using a flag called Active to record the status of the LEDs. Command line active = Not active toggles the flag between true and false:

  1. From Gpiozero import LED, Button
  2. From time import sleep
  3. ?
  4. LED = LED (17)
  5. button = button (2)
  6. Active = False
  7. ?
  8. while True:
  9. ???? if active = = False:
  10. ???????? Led.off ()
  11. ???? Else:
  12. ???????? Led.on ()
  13. ???? Button.wait_for_press ()
  14. ???? Button.wait_for_release ()
  15. ???? Active = Not active

?

It's great if you just want to turn on the LED when the button is pressed. With the Gpio Zero Library, this is simple.

(1). There are two methods in the button class called When_pressed and When_release. They do not block the engineering process, so if they are placed in a loop, the project will cycle in an indeterminate way.

(2). Modify your code like this:

    1. From Gpiozero import LED, Button
    2. From signal import Pause
    3. ?
    4. LED = LED (17)
    5. button = button (2)
    6. ?
    7. button.when_pressed = Led.on
    8. button.when_released = Led.off
    9. ?
    10. Pause ()

?

(3). Save and run the project. Now when you hold down the button, the LED will light up, and when you release the button it will go off.

X What's next?

There are a lot of other things to try in the Gpio Zero library. You can take a look at the documentation here:

https://gpiozero.readthedocs.org/en/v1.1.0/

Try to control other components such as buzzers, RGB LEDs, motors or robots.

Raspberry Pi website Study record

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.