0
0
Raspberry Piprogramming~5 mins

Digital output (GPIO.output) in Raspberry Pi

Choose your learning style9 modes available
Introduction

We use digital output to control devices like lights or motors by turning them on or off using the Raspberry Pi's pins.

Turning an LED light on or off.
Controlling a small motor to start or stop.
Activating a buzzer to make sound.
Switching a relay to control a bigger device.
Signaling with a digital flag or indicator.
Syntax
Raspberry Pi
GPIO.output(pin_number, GPIO.HIGH) or GPIO.output(pin_number, GPIO.LOW)

pin_number is the number of the pin you want to control.

GPIO.HIGH turns the pin on (power on), GPIO.LOW turns it off.

Examples
This turns on the device connected to pin 18.
Raspberry Pi
GPIO.output(18, GPIO.HIGH)
This turns off the device connected to pin 23.
Raspberry Pi
GPIO.output(23, GPIO.LOW)
Sample Program

This program turns an LED connected to pin 18 on for 2 seconds, then turns it off. It uses BCM pin numbering and cleans up the pins at the end.

Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)  # Use BCM pin numbering
GPIO.setup(18, GPIO.OUT)  # Set pin 18 as output

print('Turning LED on')
GPIO.output(18, GPIO.HIGH)  # Turn LED on

time.sleep(2)  # Wait for 2 seconds

print('Turning LED off')
GPIO.output(18, GPIO.LOW)  # Turn LED off

GPIO.cleanup()  # Reset pins
OutputSuccess
Important Notes

Always call GPIO.cleanup() at the end to reset pins safely.

Use GPIO.setmode(GPIO.BCM) or GPIO.setmode(GPIO.BOARD) to choose pin numbering style before setup.

Summary

Use GPIO.output(pin, GPIO.HIGH) to turn a pin on.

Use GPIO.output(pin, GPIO.LOW) to turn a pin off.

Remember to set the pin as output first with GPIO.setup().