---
# System prepended metadata

title: RPI.GPIO example

---

``` python
import RPi.GPIO as GPIO
import time

# Set the GPIO mode to BCM numbering
GPIO.setmode(GPIO.BCM)

# Define the pin number
PIN = 4

# Set up the GPIO pin as an input
GPIO.setup(PIN, GPIO.IN)

try:
    while True:
        # Read the state of the pin
        pin_state = GPIO.input(PIN)
        
        if pin_state:
            print("Pin 4 is HIGH")
        else:
            print("Pin 4 is LOW")
        
        # Wait for a second before reading again
        time.sleep(1)

except KeyboardInterrupt:
    print("Program terminated by user")

finally:
    # Clean up the GPIO settings
    GPIO.cleanup()
```