Raspberry Pi A Скачать руководство пользователя страница 2

Controlling the GPIO in python 

 

 

You can simulate and test these commands without a RPi on 

create.withcode.uk

 

Import the GPIO module 

You’ll need to do this once at the start of your code so 
that you can use the GPIO module to access the inputs 
and outputs. 

Setup inputs and outputs 

 

All I/O pins are set to be inputs unless you write code to make them outputs. 

 
This example sets the RPi to use physical pin 
numbering then sets physical pin 3 to be an output. 

 

 

Change GPIO.BOARD to GPIO.BCM if you want BCM pin numbering instead of physical pin numbering 

 

Change GPIO.OUT to GPIO.IN if you want to set a pin back to being an input 

Reading inputs and setting outputs 

 

The time module is useful for adding delays to 
pause your program 

We’ve set up physical pin 3 (BCM2) as an 
output and physical pin 5 (BCM 3) as an input: 

 

 

 

GPIO.output sends a value to an output pin 

time.sleep(1) pauses the program for 1 
second 

 

 

GPIO.input reads a value from an input pin 

 

 

 

You can run this code online with a simulated Raspberry Pi here; 

https://create.withcode.uk/python/A3

  

Отзывы: