Welcome to Raspberry Pi 4B repository
4.Read pin level
!Note:
The maximum continuous input and output voltage of the Raspberry Pi's GPIO pin is 3.3V. Do not connect it directly with other electronic components, otherwise it will damage the Raspberry Pi.
Step 1:Create and open readgpio.py file
nano readgpio.py
Step 2: Writing code
import wiringpi
GPIO_Output_Pin = 0
GPIO_Intput_Pin = 1
OUTPUT = 1
INPUT = 0
HIGH = 1
LOW = 0
wiringpi.wiringPiSetup()
wiringpi.pinMode(GPIO_Output_Pin,OUTPUT)
wiringpi.pinMode(GPIO_Intput_Pin,INPUT)
while 1:
wiringpi.digitalWrite(GPIO_Output_Pin,HIGH)
print('GPIO_Output_Pin OUTPUT =>HIGH')
print('GPIO_Intput_Pin INPUT <=',wiringpi.digitalRead(GPIO_Intput_Pin))
wiringpi.delay(1000) #Delay 1000ms
print()
wiringpi.digitalWrite(GPIO_Output_Pin,LOW)
print('GPIO_Output_Pin OUTPUT =>LOW')
print('GPIO_Intput_Pin INPUT <=',wiringpi.digitalRead(GPIO_Intput_Pin))
wiringpi.delay(1000) #Delay 1000ms
print()
After writing, press Ctrl + X to exit this file.
The system will prompt you whether you need to save, press Y to save and exit.
Step 3: Connect GPIO0 and GPIO1 by Dupont line.
Step 5: Run this code
python3 readgpio.py
We can see that the InputPin pin changes with the output of OutputPin.
Appendix: GPIO pin reference diagram