How To Connect

diagram
  1. Connect 3.3 V pin of Raspberry Pi to VCC pin of Relay
  2. Connect GND pin of Raspberry Pi to GND of Relay
  3. Connect GPIO 21 of Raspberry Pi to Input pin of Relay
  4. Connect COM (Common pin) of Relay to +ve Voltage source of Battery
  5. Connect NO pin of Relay to +ve terminal of DC motor
  6. Connect -ve of Voltage source of Battery to -ve terminal of DC motor

Connections Logs

    Raspberry Pi
    Relay
    DC Motor
    9v battery

    CONNECTOR INFO
            import RPi.GPIO as GPIO
            import time
            # Set up GPIO
            GPIO.setmode(GPIO.BCM)
            GPIO.setup(relay_pin, GPIO.OUT)
            # Function to turn on the motor
            def turn_on_motor():
                GPIO.output(relay_pin, GPIO.HIGH)
                print("Motor ON")
            # Function to turn off the motor
            def turn_off_motor():
                GPIO.output(relay_pin, GPIO.LOW)
                print("Motor OFF")
            try:
            # Set the GPIO pin for the relay
                relay_pin = 17
                # Main loop
                while True:
                    turn_on_motor()
                    time.sleep(5)  # Run the motor for 5 seconds
                    turn_off_motor()
                    time.sleep(2)  # Wait for 2 seconds before turning on again
            except KeyboardInterrupt:
                # Clean up GPIO on keyboard interrupt
                GPIO.cleanup()