001 - Raspberry Pi Pico: GPIO | MicroPython

Introduction

In this article, I would like to share to you the results of my exploration with the basic GPIO of the Raspberry Pi Pico using the MicroPython language.

Bill Of Materials

  1. Raspberry Pi Pico dev board.
  2. Breadboard.
  3. 5 pieces of LEDs with its appropriate limiting resistor.
  4. 3 pieces of tactile switch.
  5. and some jumper wires.

Hardware Instruction

  1. First, attach the Raspberry Pi Pico development board to the breadboard and make sure to refer to the circuit diagram below.
  2. Next, setup all the LEDs so that its anode pin(long leg) is connected to the limiting resistor in order to prevent damaging the LEDs from over current while its cathode pin to the ground.
  3. Next, connect the first LED (red LED in my case) to GPIO 2 (pin 4) of the RPi Pico.
  4. Next, connect the second LED (green LED in my case) to GPIO 3 (pin 5) of the RPi Pico.
  5. Next, connect the third LED (blue LED in my case) to GPIO 4 (pin 6) of the RPi Pico.
  6. Next, connect the fourth LED (yellow LED in my case) to GPIO 5 (pin 7) of the RPi Pico.
  7. Next, connect the fifth LED (white LED in my case) to GPIO 6 (pin 9) of the RPi Pico.
  8. Next, setup all the tactile switch so that one side of the switch is connected to the ground.
  9. Next, connect the first button to GPIO 11 (pin15) of the RPi Pico.
  10. Next, connect the second button to GPIO 12 (pin 16) of the RPi Pico.
  11. Next, connect the third button to GPIO 13 (pin 17) of the RPi Pico.
  12. Lastly, make sure to connect the ground pin of RPi Pico to the ground rail. Our demo circuit should now be ready.

Video Demonstration

Call To Action

For any concern, write your message in the comment section.

You might also like to support my journey on Youtube by Subscribing. Click this to Subscribe to TechToTinker.

Thank you and have a good days ahead.

See you,

– George Bantique | tech.to.tinker@gmail.com

Source Code

1. Binary Coded Decimal (BCD) using LEDs:

This code aims to display a Binary Coded Decimal (BCD) using LEDs. BCD value can be decremented using the first button (swA) or incremented using the third button (swC). The BCD value can be reset using the middle button (swB).

 1# More details can be found in TechToTinker.blogspot.com   
 2 # George Bantique | tech.to.tinker@gmail.com  
 3 from machine import Pin  
 4 from time import sleep_ms  
 5 
 6 # LEDs:  
 7 r = Pin(2, Pin.OUT)  
 8 g = Pin(3, Pin.OUT)  
 9 b = Pin(4, Pin.OUT)  
10 y = Pin(5, Pin.OUT)  
11 w = Pin(6, Pin.OUT)  
12 
13 # Buttons:  
14 swA = Pin(11, Pin.IN, Pin.PULL_UP)  
15 swB = Pin(12, Pin.IN, Pin.PULL_UP)  
16 swC = Pin(13, Pin.IN, Pin.PULL_UP) 
17 
18 # Converts an integer value to equivalent BCD  
19 # For more details, visit TechToTinker.blogspot.com  
20 def convert_to_bcd(bits, integer):  
21   global bcd  
22   for i in range(bits-1, -1, -1):  
23     # check if the specific bit should be set or clear  
24     div = integer//2**i  
25     # save the binary value to specific bit in the BCD  
26     bcd[i] = div  
27     # get the remainder of the integer value  
28     # which will be use for the next loop iteration  
29     integer = integer%2**i  
30     # DEBUG: print the current bit of BCD  
31     #    and the current remainder.  
32     print(div, integer)  
33     
34 # constants and global variables  
35 NUM_OF_LED = 5  
36 led = [w, y, b, g, r]  
37 bcd = [0, 0, 0, 0, 0]  
38 counter = 0  
39 prev_cnt = 0  
40 
41 while True:  
42   if swA.value()==0:  
43     # Subtract 1 from counter  
44     if counter>0:  
45       counter -= 1  
46   if swB.value()==0:  
47     # Reset counter to 0  
48     counter = 0  
49   if swC.value()==0:  
50     # Add 1 to counter  
51     if counter < 31:  
52       counter += 1  
53       
54   # Check if the counter is change  
55   if prev_cnt != counter:  
56     # convert the counter into BCD  
57     convert_to_bcd(NUM_OF_LED, counter)  
58     # display the current bcd value  
59     for i in range(NUM_OF_LED):  
60       led[i].value(bcd[i])  
61     # DEBUG: print the current counter  
62     print(counter)  
63     # store the current counter value  
64     prev_cnt = counter 
65     
66   # soft debounce  
67   sleep_ms(250)  

2. Knight Rider v1:

 1from machine import Pin
 2from time import sleep_ms
 3
 4r = Pin(2, Pin.OUT)
 5g = Pin(3, Pin.OUT)
 6b = Pin(4, Pin.OUT)
 7y = Pin(5, Pin.OUT)
 8w = Pin(6, Pin.OUT)
 9led = [r, g, b, y, w]
10
11swA = Pin(11, Pin.IN, Pin.PULL_UP)
12swB = Pin(12, Pin.IN, Pin.PULL_UP)
13swC = Pin(13, Pin.IN, Pin.PULL_UP)
14
15delay = 50
16
17while True:
18    for i in range(0, 4, 1):
19        led[i].value(1)
20        sleep_ms(delay)
21        led[i].value(0)
22        sleep_ms(delay)
23    for i in range(4, 0, -1):
24        led[i].value(1)
25        sleep_ms(delay)
26        led[i].value(0)
27        sleep_ms(delay)

3. Knight Rider v2:

This code creates smoother transition.

 1from machine import Pin
 2from time import sleep_ms
 3
 4r = Pin(2, Pin.OUT)
 5g = Pin(3, Pin.OUT)
 6b = Pin(4, Pin.OUT)
 7y = Pin(5, Pin.OUT)
 8w = Pin(6, Pin.OUT)
 9led = [r, g, b, y, w]
10
11swA = Pin(11, Pin.IN, Pin.PULL_UP)
12swB = Pin(12, Pin.IN, Pin.PULL_UP)
13swC = Pin(13, Pin.IN, Pin.PULL_UP)
14
15delay = 50
16
17while True:
18    for i in range(0, 4, 1):
19        led[i].value(1)
20        sleep_ms(delay)
21        led[i+1].value(1)
22        sleep_ms(delay)
23        led[i].value(0)
24        sleep_ms(delay*2)
25    for i in range(4, 0, -1):
26        led[i].value(1)
27        sleep_ms(delay)
28        led[i-1].value(1)
29        sleep_ms(delay)
30        led[i].value(0)
31        sleep_ms(delay*2)

References And Credits

None so far.



Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.