{"product_id":"accessories-t-type-gpio-expansion-board-40p-cable-for-raspberry-pi-3b-4b","title":"Accessories T Type GPIO Expansion Board 40P Cable for Raspberry Pi 3B+\/4B","description":"\u003cdiv\u003e\n\u003cdiv\u003e\u003cdiv\u003e\n\u003cp\u003e\u003cspan\u003eRaspberry Pi B+ special accessories T-shaped GPIO expansion board development board\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eYou can import the RPi.GPIO module with the following code.\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eimportRPi.GPIOasGPIO\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eOnce introduced, you can use functions from the GPIO module. If you want to check whether the module was introduced successfully, you can also write:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003etry:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eimportRPi.GPIOasGPIO\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eexceptRuntimeError:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eprint (\" Introduce error \")\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003ePin number\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eIn RPi.GPIO, both GPIO pin numbers on Raspberry PI are supported. The first number is the BOARD number, which corresponds to the physical pin number on the Raspberry PI board. The advantage of using this numbering is that your hardware will always be usable and you won't have to worry about Raspberry PI versions. So you don't need to rewrite the connectors or code after the board is upgraded.\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eThe second type of numbering, the BCM rule, is a more low-level way of working that corresponds to the channel numbering in Broadcom's system-on-chip. When using a pin, you need to look for the corresponding rules between the channel number and the physical pin number. The script file may not be universal for different versions of Raspberry PI.\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eYou can specify a numbering rule with the following code (mandatory) :\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.setmode (GPIO.BOARD)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003e#or\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.setmode (GPIO.BCM)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eThe following code returns the numbering rule that was set\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003emode=GPIO.getmode ()\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003ewarning\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eIf RPi.GRIO detects that a pin has been set to a non-default value, you will see a warning message. You can disable warnings with the following code:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.setwarnings (False)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003ePin setting\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eBefore using a pin, you need to set the pins as input or output. The code to configure a pin is as follows:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003e# Set the pin to input mode\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.setup (channel, GPIO.IN)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003e# Set the pin to output mode\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.setup (channel, gpio.out)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003e# sets the default value for the pins of the output\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.setup (channel, GPIO.OUT, initial=GPIO.HIGH)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003erelease\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGenerally speaking, the application needs to release resources at the end of the application. This good habit can avoid accidentally damaging the Raspberry PI. Release the pins used in the script:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.cleanup ()\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eNote that gpio.cleanup () releases only the GPIO pins used in the script and clears the set pin numbering rules.\u003c\/span\u003e\u003c\/p\u003e\n\u003cdiv\u003e\u003cbr\u003e\u003c\/div\u003e\n\u003cp\u003e\u003cspan\u003eTo set the port to the output state:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eIf you want to light an LED light or drive a device, you need to give the current and voltage to them. This step is very simple, just set the output state of the pin. The code is as follows:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.output (channel, state)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eState can be set to 0 \/ GPIO. LOW\/False \/ 1 \/ GPIO. HIGH\/True. If the encoding rule is GPIO.BOARD, then channel is the number corresponding to the pin.\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eIf you want to set more than one pin at a time, use the following code:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003echan_list= [11,12]\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.output (chan_list, GPIO.LOW)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.output (chan_list, (GPIO.HIGH, GPIO.LOW))\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eYou can also use the Input () function to read the state of an output pin and use it as an output value, for example:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.output (12, notGPIO.input (12))\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eread\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eWe often need to read the input state of the pin to obtain the following code:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.input (channel)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eReturn 0\/GPIO.LOW\/False for low levels and 1\/GPIO.HIGH\/True for high levels.\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eIf the input pin is suspended, the pin value will float. In other words, the value read is unknown because it is not connected to any signal until a button or switch is pressed. The input value may change repeatedly due to interference. Use the following code to solve the problem:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.setup (channel, GPIO.IN, pull_up_down=GPIO.PUD_UP)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003e#or\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.setup (channel, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eIt is important to note that the reading code above just gets the pin input signal for the current instant.\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eIf you need to monitor pin status changes in real time, there are two methods. The simplest primitive way is to check the input signal value at regular intervals, which is called polling. If your program reads at the wrong time, it is likely to lose the input signal. Polling is done in a loop, which takes up processor resources. Another way to respond to GPIO input is to use interrupts (edge detection), where the edge refers to the transformation of the signal from high to low (falling edge) or from low to high (rising edge).\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003ePolling mode\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003ewhileGPIO.input (channel) ==GPIO.LOW: Whilegpio. input (channel) == gpio. low:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003e# wait10mstogiveCPUchancetodootherthings time. Sleep (0.01)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eEdge detection\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eAn edge is a change in signal state, from low to high (rising edge) or high to low (falling edge). In general, we care more about the edge of the input state than the value of the input signal. The side of this state is called an event. Let's start with two functions:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003ewait_for_edge () function. wait_for_edge () is used to prevent the program from continuing until an edge is detected. That is, the example of waiting for the button to be pressed above could be rewritten as: channel=GPIO.wait_for_edge (channel, GPIO_RISING, timeout=5000)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eifchannelisNone:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eprint (' Timeoutoccurred ')\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eelse:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eprint (' Edgedetectedonchannel ', channel)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eadd_event_detect () function This function listens for a pin and returns true when the input state of the pin changes, as shown in the following code: GPIO. Add_event_detect (channel, GPIO. RISING) # addrisingedgedetectiononachannel\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003edo_something ()\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003e\/\/ The following code is executed in a thread loop.\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eifGPIO.event_detected (channel) :\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eprint (' Buttonpressed ')\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eThe above code requires a new thread of its own to loop through the value of event_detected (), which is a bit of a hassle.\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eHowever, another way to easily detect state is to pass in a callback function directly:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003edefmy_callback (channel) :\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003ePrint (' Thisisaedgeeventcallbackfunction! ')\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eprint (' Edgedetectedonchannel%s' %channel)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003ePrint (' Thisisruninadifferentthreadtoyourmainprogram)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.add_event_detect (channel, GPIO.RISING, callback=my_callback)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eIf you want to set more than one callback function, you can do this:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003edefmy_callback_one (channel) :\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eprint (' Callbackone ')\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003edefmy_callback_two (channel) :\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eprint (' Callbacktwo ')\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.add_event_detect (channel, GPIO.RISING)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.add_event_callback (channel, my_callback_one)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.add_event_callback (channel, my_callback_two)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eNote: When a callback is triggered, the callback functions are not executed at the same time; instead, they are called in the order they were set.\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGeneral example: Light up LED lights\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cbr\u003e\u003cspan\u003eThis explains a whole bunch of libraries, so it's time for a simple experiment. The experiment is simple. Light up an LED light.\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eBefore you can write the code, first you need to connect the led light pin to the Raspberry PI pin via the dupont wire, for example, you can connect to pin 11.\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eCreate a new main.py file and write the following code:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eimportRPi.GPIOasGPIO\/\/ Introduces importrpi. gpioasgpio \/\/\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eimporttime\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eRPi.GPIO.setmode (GPIO.BOARD) \/\/ Set pin numbering rules\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eRPi.GPIO.setup (11, RPi.GPIO.OUT) \/\/ Set pin 11 to output mode\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003ewhileTrue\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.output (channel, 1) \/\/ Set the status of the pin to high level, then the LED lights up\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003etime.sleep (1) \/\/ The program sleeps for 1 second and the LED is on for 1 second\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.output (channel, 0) \/\/ Set the pin status to low and the LED is off\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003etime.sleep (1) \/\/ Program sleep 1 second, let LED off 1 second\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.cleanup () \/\/ Don't forget to clear all resources at the end of the program\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eSave and exit the file. To see the effect, run python3main.py. Ctrl+C can close the program.\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eIn addition, try other functions to enhance the impression.\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eUse PWM\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eThe python library also supports output in PWM mode, which we can use to create the breath light effect. See the code for details:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eimporttime\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eimportRPi.GPIOasGPIO\/\/ Importrpi. gpioasgpio \/\/\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.setmode (GPIO.BOARD) \/\/ Set the numbering mode\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.setup (12, GPIO.OUT) \/\/ Set pin 12 to output mode\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003ep=GPIO.PWM (12,50) \/\/ Initializes pin 12 to a PWM instance at a frequency of 50Hz\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003ep.start (0) \/\/ Start pulse width modulation, parameter range: (0.0 =dc =100.0)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003etry:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003ewhile1:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003efordcinrange (0,101,5) :\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003ep.ChangeDutyCycle (dc) \/\/ Modify the parameter range of duty cycle: (0.0 =dc =100.0)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003etime.sleep (0.1)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003efordcinrange (100, -1, -5) :\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003ep.ChangeDutyCycle (dc)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003etime.sleep (0.1)\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eexceptKeyboardInterrupt:\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003epass\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eP.top () \/\/ Stop output PWM wave\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003eGPIO.cleanup () \/\/ Cleanup\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003econclusion\u003c\/span\u003e\u003cbr\u003e\u003cspan\u003eIn this article, I have explained the concept of GPIO and how to operate it using python. However, if you want to fully understand Raspberry PI gpio programming, you need to do hands-on operation and analysis and summary.\u003c\/span\u003e\u003c\/p\u003e\n\u003cdiv\u003e\u003cbr\u003e\u003c\/div\u003e\n\u003cp\u003e\u003cspan\u003eThe following is the test procedure\u003c\/span\u003e\u003cbr\u003e\u003cspan\u003e# -*- coding: utf-8 -*- # Declaration allows writing Chinese in the program\u003c\/span\u003e\u003cbr\u003e\u003cspan\u003eimport RPi.GPIO as GPIO # Import RPi.GPIO library function named GPIO\u003c\/span\u003e\u003cbr\u003e\u003cspan\u003eimport time # Introduces the timing time function\u003c\/span\u003e\u003cbr\u003e\u003cspan\u003e# BOARD numbering mode based on socket pin number\u003c\/span\u003e\u003cbr\u003e\u003cspan\u003eGpio.setmode (gpio.board) # Set the GPIO programming mode to BOARD mode\u003c\/span\u003e\u003cbr\u003e\u003cspan\u003e# Output mode\u003c\/span\u003e\u003cbr\u003e\u003cspan\u003eGpio.setup (11, gpio.out) # Set GPIO pin 11 as the output pin\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003ewhile True: # The condition is true, the following program continues to loop\u003c\/span\u003e\u003cbr\u003e\u003cspan\u003eGPIO.output(11, GPIO.HIGH) # Set the voltage of pin 11 to high and light the LED\u003c\/span\u003e\u003cbr\u003e\u003cspan\u003etime.\u003c\/span\u003e\u003c\/p\u003e\n\u003cdiv\u003e\n\u003cbr\u003e\u003cbr\u003e\n\u003c\/div\u003e\n\u003c\/div\u003e\u003c\/div\u003e\n\u003cp\u003e\u003cbr\u003e\u003c\/p\u003e\n\u003c\/div\u003e","brand":"mosti1one","offers":[{"title":"Default Title","offer_id":55866292306306,"sku":"c5ce57cb-e29a-4330-bb71-b0295789e0f6","price":15.77,"currency_code":"EUR","in_stock":true}],"thumbnail_url":"\/\/cdn.shopify.com\/s\/files\/1\/0928\/3354\/6626\/files\/cc9c49e00ba63bc26ddd3168543c8972.jpg?v=1761299270","url":"https:\/\/www.mosti1one.de\/products\/accessories-t-type-gpio-expansion-board-40p-cable-for-raspberry-pi-3b-4b","provider":"mosti1one","version":"1.0","type":"link"}