Section outline

  • Section 8: Working with Sensors and Actuators

    Raspberry Pi becomes far more powerful when combined with sensors and actuators. This section focuses on connecting and programming basic input and output hardware components such as IR sensors, ultrasonic sensors, buzzers, and relays using the GPIO pins and Python scripts.

    • 1. Understanding GPIO Input and Output Modes

      • GPIO pins can be used either for reading inputs (e.g., from sensors) or sending outputs (e.g., to motors or LEDs).
      • Each GPIO pin can be configured using Python via the RPi.GPIO or GPIO Zero library.
      • Set a pin as input to receive signals from a sensor, and as output to send signals to actuators.
      import RPi.GPIO as GPIO
      
      GPIO.setmode(GPIO.BCM)
      GPIO.setup(17, GPIO.IN)   # Input pin (e.g., IR sensor)
      GPIO.setup(27, GPIO.OUT)  # Output pin (e.g., buzzer)

      2. Connecting and Using IR and Ultrasonic Sensors

      • IR sensors are used to detect objects based on infrared light reflection. Connect their output pin to a GPIO input and read logic high or low.
      • Ultrasonic sensors like HC-SR04 use echo and trigger pins to calculate distance based on time of reflected sound waves.
      import time
      
      TRIG = 23
      ECHO = 24
      GPIO.setup(TRIG, GPIO.OUT)
      GPIO.setup(ECHO, GPIO.IN)
      
      GPIO.output(TRIG, False)
      time.sleep(2)
      GPIO.output(TRIG, True)
      time.sleep(0.00001)
      GPIO.output(TRIG, False)
      
      while GPIO.input(ECHO) == 0:
          pulse_start = time.time()
      while GPIO.input(ECHO) == 1:
          pulse_end = time.time()
      
      distance = (pulse_end - pulse_start) * 17150
      print("Distance:", round(distance, 2), "cm")

      3. Controlling Buzzers, LEDs, and Relays

      • Buzzers can be controlled by sending HIGH or LOW signals via GPIO output.
      • Relays allow you to control higher voltage devices (like lights or pumps) using the Pi's GPIO.
      • Always use a transistor and diode to protect the Pi when working with relays.
      GPIO.output(27, GPIO.HIGH)  # Turn on buzzer or relay
      time.sleep(1)
      GPIO.output(27, GPIO.LOW)

      4. Using Pull-Up and Pull-Down Resistors

      • When using switches or sensors, GPIO inputs may float and give unstable readings.
      • Pull-up resistors keep the input at a known state (HIGH) when the button is not pressed.
      • Pull-down resistors do the opposite, keeping the input LOW by default.
      • Raspberry Pi allows software pull-ups/pull-downs during GPIO.setup().
      GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

      5. Real-Time Input-Output Control

      • Use loops and event detection to monitor sensor inputs and trigger outputs like buzzers or LEDs instantly.
      • GPIO.add_event_detect() can be used to trigger a callback when the pin value changes.
      def motion_detected(channel):
          print("Obstacle detected!")
          GPIO.output(27, GPIO.HIGH)
      
      GPIO.add_event_detect(17, GPIO.RISING, callback=motion_detected)

      This section gives you the foundational skills to build interactive Raspberry Pi projects where hardware reacts to real-world inputs, which is essential for robotics, home automation, and safety systems.