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

5 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!

2 Likes

Zach, Hello and thank you for the comments.
Bryan

2 Likes

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

2 Likes

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

1 Like

Hi @dave50358,

Thanks for sharing that product.

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

1 Like

Bryan, that’s an impressive number of LEDs ! I think your board is a great idea, and you have obviously learnt a lot by making it …
… but if you had simply bought a pre-assembled starter board, plugged it in, and installed a program to display patterns with the LEDs … would you have learned so much ? And where to go from there ?

Could I suggest writing a tutorial for this project, showing a new user not just what you did, but why you did it that way … helping them build their own starter board and learning as you did.

Don’t worry that you are not an expert or know all the correct terminology - neither do other users new to Raspberry Pi ! Personally I find too many tutorials from experienced people tell you what they did but not why they chose this component or technique over that, and so when I come to making my own project i’m not much wiser.

4 Likes

Donald,

Hello. Thank you for your email. Some history on myself, I am not new to computing but new to both the Raspberry Pi and Python.

I have attached a presentation I did for Engineers Australia a few years ago. You may find it interesting? My first computer back in the 70’s was a MiniScamp (all machine code) based on National Semiconductors ā€˜SC MP’ 8 Bit Microprocessor.

As per your comments below unfortunately I am not into YOU TUBE presentations however I could do something with Power Point if that is suitable.

Let me know your thoughts.

Thank you.

Regards

Bryan Cheetham

(attachments)

30 Years of Industrial Computing - A Personal Experience.pdf (2.08 MB)

1 Like

Hi Bryan,

What a great overview of the evolution of computing. I’ve done a little assembly before and I very much respect those who didn’t have an alternative. It is also interesting to see how similar some things still are, notation wise the programming is much the same.

The circuit board at the end is also quite interesting. I don’t think I’ve often that many large ICs on boards so densely packed (outside of computers I suppose).

What a great insight, thanks for sharing!

1 Like

Hi Bryan, my own first exposure to computers was the Digital Equipment Corp (DEC) PDP-8f (with 4K of 12-bit RAM, and a teletype) at my secondary school. I couldn’t think of a career I was attracted to, so enrolled at the local NZ TAFE-equivalent in 1977 for a full-time programming course. I was a founding member of the NZ Microcomputer Club the next year … though I couldn’t afford to buy any computer myself until an IBM-compatible PC several years later.

Since getting my first Raspberry Pi (model 1, about 2012) I have had many challenges learning Raspberry Pi, Linux and Python … open source assumes that users are other developers with the same high level of technical expertise … resulting in learning ā€œcurvesā€ that require crampons. Several times I have given up and come back a couple of months later to find that something must have permeated my thick skull :wink:

I myself have documented some of my forays (initially for my own debugging) and posted them on forums where hopefully they can make the journey easier for others. Doesn’t have to be professional youtube or PowerPoint presentations to help someone else.

I am impressed by your Starter-HAT, and I believe it to be a worthwhile project. Even if you choose not to write it up, your post here can inspire others.
Thank you.

1 Like