Following example sets PIN18 to High(3.3V).
In [ ]:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM) # use GPIO numbering, see output of gpio command
#GPIO.setmode(GPIO.BOARD) # use Physical pin numbering
GPIO.setup(18, GPIO.OUT) # PIN18 (Physical:Pin12) : Output
GPIO.output(18, GPIO.HIGH) # Ping18 -> High (3.3V)
In [ ]:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM) # use GPIO numbering, see output of gpio command
GPIO.setup(18, GPIO.IN) # PIN18: Input
if GPIO.input(18) == GPIO.HIGH:
print('PIN18: HIGH')
else:
print('PIN18: LOW')
In [ ]:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM) # use GPIO numbering, see output of gpio command
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO_PUP_DOWN) # PIN18: Input (Pull Down enabled)
if GPIO.input(18) == GPIO.HIGH:
print('PIN18: HIGH')
else:
print('PIN18: LOW')
PWM is a common method used to apply a proportional control signal to an external device using a digital output pin. For example, servo motors use the pulse width of an incoming PWM signal to determine their rotation angle. LCD displays adjust their brightness based on a PWM signal's average value.
Raspberry Pi has 2 hardware PWM, Arduino UNO has 6 hardware PWM.