3. Using RaspberryPi
a. Calculate the distance using a distance sensor.
b. Basic LED functionality using switch.
3.
Basic LED functionalityusing switch.
Control an LED using a push button connected to the Raspberry Pi GPIO pins.
Components Required
Raspberry Pi 3B+
LED
220 Ω resistor
Push button (switch)
Breadboard
Jumper wires
6.
from gpiozero importLED, Button
from signal import pause
# LED connected to GPIO17
led = LED(17)
# Push button connected to GPIO2
button = Button(2)
# Turn LED ON when button is pressed
button.when_pressed = led.on
# Turn LED OFF when button is released
button.when_released = led.off
print("Press the button to control the LED")
pause()