• Light an LED when a MotionSensor detects motion:


  • GPIO gpio pir-motion-sensor
     
    from gpiozero import MotionSensor, LED
    from signal import pause
    
    pir = MotionSensor(4)
    led = LED(16)
    
    pir.when_motion = led.on
    pir.when_no_motion = led.off
    
    pause()
    
    
  • GPIO gpio light sensor


  • Have a LightSensor detect light and dark:


  •  
    from gpiozero import LightSensor
    
    sensor = LightSensor(18)
    
    while True:
        sensor.wait_for_light()
        print("It's light! :)")
        sensor.wait_for_dark()
        print("It's dark :(")
     
    
  • Run a function when the light changes:


  •  
    from gpiozero import LightSensor, LED
    from signal import pause
    
    sensor = LightSensor(18)
    led = LED(16)
    
    sensor.when_dark = led.on
    sensor.when_light = led.off
    
    pause()
    
    
  • Or make a PWMLED change brightness according to the detected light level:


  •  
        
    from gpiozero import LightSensor, PWMLED
    from signal import pause
    
    sensor = LightSensor(18)
    led = PWMLED(16)
    
    led.source = sensor
    
    pause()
    
    
  • GPIO gpio distance sensor


  • Note In the diagram above, the wires leading from the sensor to the breadboard can be omitted; simply plug the sensor directly into the breadboard facing the edge (unfortunately this is difficult to illustrate in the diagram without the sensor’s diagram obscuring most of the breadboard!)


  • Have a DistanceSensor detect the distance to the nearest object:


  •  
    from gpiozero import DistanceSensor
    from time import sleep
    
    sensor = DistanceSensor(23, 24)
    
    while True:
        print('Distance to nearest object is', sensor.distance, 'm')
        sleep(1)
         
    
  • Run a function when something gets near the sensor:


  •  
    from gpiozero import DistanceSensor, LED
    from signal import pause
    
    sensor = DistanceSensor(23, 24, max_distance=1, threshold_distance=0.2)
    led = LED(16)
    
    sensor.when_in_range = led.on
    sensor.when_out_of_range = led.off
    
    pause()
    
    
  • Control a servo between its minimum, mid-point and maximum positions in sequence:


  •  
    from gpiozero import Servo
    from time import sleep
    
    servo = Servo(17)
    
    while True:
        servo.min()
        sleep(2)
        servo.mid()
        sleep(2)
        servo.max()
        sleep(2)
         
    
  • Use a button to control the servo between its minimum and maximum positions:


  •  
    from gpiozero import Servo, Button
    
    servo = Servo(17)
    btn = Button(14)
    
    while True:
        servo.min()
        btn.wait_for_press()
        servo.max()
        btn.wait_for_press()
        
       
    
  • Automate the servo to continuously slowly sweep:


  •    
    from gpiozero import Servo
    from gpiozero.tools import sin_values
    
    servo = Servo(17)
    
    servo.source = sin_values()
    servo.source_delay = 0.1
    
     
    
  • Use AngularServo so you can specify an angle:


  •     
    from gpiozero import AngularServo
    from time import sleep
    
    servo = AngularServo(17, min_angle=-90, max_angle=90)
    
    while True:
        servo.angle = -90
        sleep(2)
        servo.angle = -45
        sleep(2)
        servo.angle = 0
        sleep(2)
        servo.angle = 45
        sleep(2)
        servo.angle = 90
        sleep(2)
        
    
  • GPIO gpio motor


  • Spin a Motor around forwards and backwards:


  •  
    from gpiozero import Motor
    from time import sleep
    
    motor = Motor(forward=4, backward=14)
    
    while True:
        motor.forward()
        sleep(5)
        motor.backward()
        sleep(5)
      
    
  • GPIO gpio robot


  • Make a Robot drive around in (roughly) a square:


  •  
    from gpiozero import Robot
    from time import sleep
    
    robot = Robot(left=(4, 14), right=(17, 18))
    
    for i in range(4):
        robot.forward()
        sleep(10)
        robot.right()
        sleep(1)
      
    
  • Make a robot with a distance sensor that runs away when things get within 20cm of it:


  •  
        
    from gpiozero import Robot, DistanceSensor
    from signal import pause
    
    sensor = DistanceSensor(23, 24, max_distance=1, threshold_distance=0.2)
    robot = Robot(left=(4, 14), right=(17, 18))
    
    sensor.when_in_range = robot.backward
    sensor.when_out_of_range = robot.stop
    pause()