gpio_controlled_leds
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| gpio_controlled_leds [2026/06/17 16:23] – [Introduction] walkeradmin | gpio_controlled_leds [2026/06/30 21:01] (current) – walkeradmin | ||
|---|---|---|---|
| Line 13: | Line 13: | ||
| \\ | \\ | ||
| \\ | \\ | ||
| - | {{ : | + | {{ : |
| \\ | \\ | ||
| + | We have wired the LEDs to GPIO pins 2, 6 & 10. These are the GPIO Pin numbers, not the actual Pi Pico Pin numbers. | ||
| + | \\ | ||
| + | \\ | ||
| + | Below is the software being used to drive the LEDs | ||
| + | \\ | ||
| + | < | ||
| + | # Start by importing lib files " | ||
| + | # Imports the machine module, which provides access to hardware features such as GPIO pins, ADCs, PWM, etc. | ||
| + | import machine | ||
| + | # Imports the time module, which provides delay functions like sleep(). | ||
| + | import time | ||
| + | #we're initalizing pin 25 (which is the onboard LED) & GPIO pins 2, 6 and 10 as outputs (These are the GPIO numbers, not the actual pin numbers around the PCB). | ||
| + | # Creates a pin object called led1. | ||
| + | # Uses GPIO pin 25. | ||
| + | # Sets the pin mode to output (Pin.OUT). | ||
| + | # On a Raspberry Pi Pico, GPIO 25 is usually connected to the onboard LED. | ||
| + | led1 = machine.Pin(25, | ||
| + | # Creates an output pin object for GPIO 2 (GP2). | ||
| + | led2 = machine.Pin(2, | ||
| + | # Creates an output pin object for GPIO 6 (GP6). | ||
| + | led3 = machine.Pin(6, | ||
| + | # Creates an output pin object for GPIO 10 (GP10). | ||
| + | led4 = machine.Pin(10, | ||
| + | |||
| + | #Run a loop 5 times | ||
| + | # Starts a loop that repeats 5 times. | ||
| + | # The variable i takes values from 0 to 5. | ||
| + | for i in range(5): | ||
| + | # Sets GPIO 2 HIGH (3.3V). | ||
| + | # Turns LED 2 on (assuming the LED is wired active-high). | ||
| + | led2.value(1) | ||
| + | # Sets GPIO 6 & 10 LOW. | ||
| + | # Turns LED 3 and 4 off. | ||
| + | led3.value(0) | ||
| + | led4.value(0) | ||
| + | | ||
| + | #Use time to wait for 0.5 second | ||
| + | time.sleep(0.2) | ||
| + | |||
| + | # The rest of the code is a repetition of what has been done above. | ||
| + | #Turn the onbaord LED off | ||
| + | led1.value(0) | ||
| + | | ||
| + | #Turn external LED on pin 2 (only) on | ||
| + | # | ||
| + | led2.value(1) | ||
| + | led3.value(0) | ||
| + | led4.value(0) | ||
| + | #Use time to wait for 1 second | ||
| + | time.sleep(0.11) | ||
| + | #Turn external LED on pin 3 (only) on | ||
| + | # | ||
| + | led2.value(0) | ||
| + | led3.value(1) | ||
| + | led4.value(0) | ||
| + | #Use time to wait for 1 second | ||
| + | time.sleep(0.2) | ||
| + | #Turn external LED on pin 4 (only) on | ||
| + | # | ||
| + | led2.value(0) | ||
| + | led3.value(0) | ||
| + | led4.value(1) | ||
| + | #Use time to wait for 1 second | ||
| + | time.sleep(0.2) | ||
| + | | ||
| + | #Turn the external LED on pin 4 off | ||
| + | led4.value(0) | ||
| + | </ | ||
| + | |||
| + | The software works by performing the following tasks. | ||
| + | \\ | ||
| + | * Import Libraries (Machine and Time) | ||
| + | |||
| + | * * Set GPIO Pins to output | ||
| + | |||
| + | * Start a loop that will loop 5 times | ||
| + | |||
| + | * Turn on the first LED and turn off all other LEDs | ||
| + | |||
| + | * Turn on the second LED and turn off all other LEDs | ||
| + | |||
| + | * Turn on the third LED and turn off all other LEDs | ||
| + | |||
| + | That's it. Pretty simple. Remember, you can control other devices using the GPIO pins other than LEDs, but the current output is limited to around 20mA. If you try to pull more than this, you could damage that GPIO Pin. | ||
| + | \\ | ||
| + | \\ | ||
| + | ---- | ||
gpio_controlled_leds.1781713395.txt.gz · Last modified: by walkeradmin
