Learning Raspberry Pi and GPIO

Core Electronics Team, Hello
If there is nothing already, I believe there is a need for such a thing as what I am calling a Starter-HAT.
I circuit board with preloaded LED and Buttons to allow a new to Raspberry PI user to get instantly hands onto coding without first fully understanding electronics.
The simple circuit board I have put together is shown in the short photo
below.

There is one LED for each of 23 GPIO and one Button.

The following Pythn3 code will cycle the LED in either GPIO sequence number, Clockwise or Anticlockwise. The Button, using edger detect interrupts the programme.

"""
#file gpio_trainer.py

The idea here is to Create a "Starter HAT" an easy access to GPIO for the Beginner.
The circuit board utilizes  23 of the available 28 GPIO as OUTPUTS and 1 as an INPUT.
The outputs drive one LED of a 16 DIL Bar Graph LED module. The are 4 modules in total.
The 1 Input is connected to GPIO 04
"""


import RPi.GPIO as GPIO
import time
import sys
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

#configure the Pi i/o

led_pin = 0
switch = 4
set_type = "0"
global hold_set
menu_set = 0

GPIO.setup(switch, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# GPIO.add_event_detect(switch, GPIO.RISING, callback=switch_callback)

gpio_clockwise = ([14, 15, 18, 23, 24, 25, 8, 7, 12, 16, 20, 21, 26, 19, 13, 6, 5, 11, 9, 10, 22, 27, 17, 50])
gpio_anticlock = ([17, 27, 22, 10, 9, 11, 5, 6, 13, 19, 26, 21, 20, 16, 12, 7, 8, 25, 24, 23, 18, 15, 14, 50])


# Callback called when switch is pressed.
def switch_callback(channel):
    print('Switch pressed, exiting.')
    GPIO.cleanup()
    sys.exit(0)

GPIO.add_event_detect(switch, GPIO.RISING, callback=switch_callback)

"""
# function to select INPUT or OUTPUT
Following is a loop function that sets the required GPIO's as OUTPUTS
"""

def set_out(led_pin): # set io as output
    GPIO.setup(led_pin, GPIO.OUT)

def on_off(led_pin, state): # set LED ON and OFF
    GPIO.output(led_pin, state)

def gpio_type(set_type): #set as INPUT or OUTPUT
    led_pin = 0
    while led_pin < 28:
        if led_pin == 0 or led_pin == 1 or led_pin == 2 or led_pin == 3 or led_pin == 4:
           #print(led_pin)
           led_pin += 1
        elif set_type == 1 or set_type == 2 or set_type == 3:
            set_out(led_pin)
            #print(led_pin, end=" ")
            led_pin += 1

"""
This function rotates the LED Outputs either Clockwise or Anti-Clockwise.
"""

def gpio_clock_anticlock(dir): # select clockwise or anti-clock
    i = 0
    if dir == 2:
       led_pin = gpio_clockwise[i]
    elif dir == 3:
        led_pin = gpio_anticlock[i]

    while led_pin != 50: # i < len(gpio_clockwise) + 1:
        #i = 0
        if dir == 2: # rotate LED Clockwise
            state = int(True)
            on_off(led_pin, state)  # GPIO.setup(led_pin, GPIO.OUT)
            time.sleep(0.025)
            state = int(False)
            on_off(led_pin, state)
            time.sleep(0.025)
            i += 1
            #print(led_pin)
            led_pin = gpio_clockwise[i]

        elif dir == 3: # rotate LED Anti-Clock
            state = int(True)
            on_off(led_pin, state)  # GPIO.setup(led_pin, GPIO.OUT)
            time.sleep(0.025)
            state = int(False)
            on_off(led_pin, state)
            time.sleep(0.025)
            i += 1
            #print(led_pin)
            led_pin = gpio_anticlock[i]

"""
This function rotates the GPIO Outputs in the Number Sequence of the GPIO.
"""

def gpio_set(): #set as True ON or False OFF
    led_pin = 0
    while led_pin < 28:
        if led_pin == 0 or led_pin == 1 or led_pin == 2 or led_pin == 3 or led_pin == 4:
           led_pin += 1
        else:
            state = int(True)
            on_off(led_pin, state)  # GPIO.setup(led_pin, GPIO.OUT)
            time.sleep(0.025)
            state = int(False)
            on_off(led_pin, state)
            time.sleep(0.025)
            led_pin += 1
            #print(led_pin)

"""
Main Loop to set the LED sequence.

Above the 1 edge-triggered Input when pressed interrupts the operation.
"""

try:
    while True:
        if menu_set == 0: #:set_type = 0
            menu_set = 11 #hold_set = (" ")
            print("Set GPIO as OUTPUT and turn LED ON/Off in BCM number sequence press 1 ")
            print("Set GPIO as OUTPUT and turn LED ON/OFF in Rotate Clockwise sequence press 2 ")
            print("Set GPIO as OUTPUT and turn LED ON/OFF in Rotate Anti-Clockwise sequence press 3 ")
            set_type = (input("Press 1, 2, or 3 ..   "))  #select GPIO type
        else:
    #gpio_type(set_type)

    #while True:
           #if menu_set == "0":
               #print("xxx")
               #GPIO.cleanup()
               #sys.exit(0)

            if set_type == "1":
               menu_set = 0
               hold_set = set_type
               set_type = int(set_type)
               gpio_type(set_type)
               time.sleep(0.01)
               gpio_set()
               print(hold_set, "xxx")
               set_type = hold_set
            elif set_type == "2" or set_type == "3":
                menu_set = 0
                hold_set = set_type
                set_type = int(set_type)
                gpio_type(set_type)
                time.sleep(0.01)
                dir = set_type
                gpio_clock_anticlock(dir)
                set_type = hold_set
            else:
                menu_set = 0
                print()
                print("input is not correct")
                print()
                print()
                print()

finally:
   print("Cleaning up")
   GPIO.cleanup()

My apologies for the code insertion.

Maybe Core as a Builder could put something together along these lines.

Bryan
2024-10-03T14:00:00Z

3 Likes

Hey @Bryan160034,

Awesome idea! We always love to hear product suggestions from our community.

I’m unaware of anything like this that currently exists, at least not this pre-assembled. Most products in this vein tend to be heavily geared towards “starter kits” and other fully disassembled collections to help beginners get the basics down.

I’ll tip this idea off to our production team, @Michael and @Liam. Thanks again!

1 Like

Zach, Hello and thank you for the comments.
Bryan

1 Like

Yes, there are such things.
It depends on your needs but I found this: https://www.desertcart.in/products/228966094-geeek-pi-raspberry-pi-4-gpio-screw-terminal-block-breakout-module-raspberry-pi-gpio-expansion-board-breakout-for-raspberry-pi-4-b-3-b-3-b-2-b-b-pi-zero-pi-zero-w
and this
Amazon.de

Dave

1 Like

Dave, hello. I had a look at the link, looks good, although complex.
Thanks
Bryan

Hi @dave50358,

Thanks for sharing that product.

It definitely looks like a good board for getting started with GPIO programming.