Raspberry Pi LESSON 7: GPIO Inputs from Button Switch

แชร์
ฝัง
  • เผยแพร่เมื่อ 18 ก.ย. 2024

ความคิดเห็น • 98

  • @W3ASR
    @W3ASR 8 หลายเดือนก่อน +1

    Thank you for taking the time to explain the pullup and pulldown resisters. The extra time was well spent and helped to clearly understand the concept.

  • @booknerdgirl4eva
    @booknerdgirl4eva หลายเดือนก่อน

    I AM UBER LEGEND!! I kind of just smiled when you issued this challenge, because I have to do this all the time for the robotics team I'm on. We have to make toggle buttons for a lot of functions on the robot controller. I used 22 lines of code! And I actually appreciate you taking the time to explain pull up and pull down resistors in the last lesson :) I'm here on a quest for understanding so I can apply what I learn here to my work at my internship. Thank you!

  • @yrtang3538
    @yrtang3538 ปีที่แล้ว +1

    best beginners' lesson !!! Paul you made everything clear and simple. Can you give another lesson on the hardware fundamentals of this build-in resistor?

  • @chadvoller
    @chadvoller ปีที่แล้ว +1

    I wish I could have paid attendion. The ant crawling around kept distracting me. 😂

  • @cbrombaugh
    @cbrombaugh 2 ปีที่แล้ว

    I AM LEGEND! It really helped to work out the logic on paper before typing the code. I had 28 lines of code in this app. This was another great lesson from the best teacher on the Internet.

  • @mattphorwich
    @mattphorwich 2 ปีที่แล้ว +2

    Groovy...I really want a raspberry pi garden and a cool robot

  • @opalprestonshirley1700
    @opalprestonshirley1700 2 ปีที่แล้ว

    Sorry about today, DR.'s appointment. Do love the problems and this one is no less fun. I remember the Adruino problem fun, fun, fun. Y'all have a great day and stay safe.

    • @paulmcwhorter
      @paulmcwhorter  2 ปีที่แล้ว

      We missed you in the chat!

    • @opalprestonshirley1700
      @opalprestonshirley1700 2 ปีที่แล้ว

      @@paulmcwhorter Thanks, Paul. It's a pain getting old!

    • @charlotteswift
      @charlotteswift 2 ปีที่แล้ว

      @@opalprestonshirley1700 It's better than the alternative!

  • @aga1nstall0dds
    @aga1nstall0dds ปีที่แล้ว

    you are a truly blessed man brother in Christ!!

  • @charlielowell4077
    @charlielowell4077 2 ปีที่แล้ว +2

    Got it done. Took a little longer because it took me a little while to realize I had the button read on the outside of the loop. Well, that and about 1,000 spelling and syntax boo-boos. The input read in the wrong place was a big oops. But, it works now! Great lesson! Thanks Paul!
    Here's the video link:
    th-cam.com/video/v9n6vEPm4Ws/w-d-xo.html
    Here's the code:
    from time import sleep
    import RPi.GPIO as GPIO
    rest = .5
    switchPin = 40
    ledPin = 11
    buttonVal=False
    ledState=False
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(ledPin, GPIO.OUT)
    GPIO.setup(switchPin, GPIO.IN,pull_up_down=GPIO.PUD_UP)
    GPIO.setwarnings(False)
    try:
    while(True):
    buttonVal=GPIO.input(switchPin)
    if buttonVal==False:
    if (ledState==False):
    GPIO.output(ledPin,True)
    print ("LED is on")
    sleep(rest)
    ledState=True
    else:
    GPIO.output(ledPin,False)
    print ("LED is off")
    sleep (rest)
    ledState=False
    except KeyboardInterrupt:
    GPIO.cleanup()
    print ("GPIO clear")

    • @paulmcwhorter
      @paulmcwhorter  2 ปีที่แล้ว

      Nice Job.

    • @Lehibob
      @Lehibob 2 ปีที่แล้ว

      I was curious when I tested this code... I noticed that if I held the button the LED flashed on and off. At other times, the led would start in the ON condition. Did you experience any of this? I"m wondering if my RPi is glitchy....

    • @keithlohmeyer
      @keithlohmeyer 2 ปีที่แล้ว

      @@Lehibob Yes you have to adjust the code a bit to only toggle the first time the state changes. You can look at my homework video to see how I did that.

    • @cbrombaugh
      @cbrombaugh 2 ปีที่แล้ว

      @@Lehibob It is solid on mine. Did you put a delay in your loop?

    • @charlielowell4077
      @charlielowell4077 2 ปีที่แล้ว

      @@Lehibob If you held the switch, you might be experiencing "switch bounce."

  • @anwarzebkhan
    @anwarzebkhan 2 ปีที่แล้ว

    Like always worth of million dollars lesson.

  • @tkklein
    @tkklein 2 ปีที่แล้ว +1

    Amazing! Thanks Paul

  • @liamoliverdarroch6482
    @liamoliverdarroch6482 10 หลายเดือนก่อน

    I am uber legend!!!!!!!!!!!!!!!!
    And I only use 7 more lines of code than you said you did to do it :p

  • @alanabela1900
    @alanabela1900 2 ปีที่แล้ว

    Thank you. Top content as always.

  • @soroushkhosrojerdi5184
    @soroushkhosrojerdi5184 2 ปีที่แล้ว +2

    Hi paul,
    I managed to have my led toggled but the curiosity caught the better of me and then I came up with an interesting command of add_event_detect() for detecting an edge in a program and i rewrote the program as follow:
    #------ Libraries ------
    import RPi.GPIO as GPIO
    #------ setmode() ------
    GPIO.setmode(GPIO.BOARD)
    #------ setup() ------
    GPIO.setup(3,GPIO.IN,pull_up_down = GPIO.PUD_UP)
    GPIO.setup(5,GPIO.OUT,initial = 0)
    #------ Functions ------
    def Button():
    if GPIO.input(3)==0:
    GPIO.output(5,1)
    else:
    GPIO.output(5,0)
    #*********************** Body ***********************
    try:
    GPIO.add_event_detect(3,GPIO.BOTH,callback = Button,bouncetime = 100)
    except KeyboardInterrupt:
    #-------Set all pins free--------
    GPIO.cleanup()
    by doing so I read somewhere that it can engage processor less than while loop and the program won't have overload.
    Best regards

    • @keithlohmeyer
      @keithlohmeyer 2 ปีที่แล้ว +1

      Yes. Great job! I did the same in the second half of my video.
      You can also read the state of the output and toggle it in one command. GPIO.output(13, not GPIO.input(13))
      Since you can do it in one line I just put a lambda inline with the event detect to shorten the code to 7 lines.

  • @addamsapple4925
    @addamsapple4925 ปีที่แล้ว

    Hi sir. do you have a 24hour timer lesson to activate an output? can i know what lesson is that?

  • @keneflucas937
    @keneflucas937 8 หลายเดือนก่อน

    What exactly is the delay for and why is it necessary??

    • @maxq4515
      @maxq4515 8 หลายเดือนก่อน

      Its just a variable used for sleep time. delay = 1; sleep(delay)

  • @michaelschweizer2115
    @michaelschweizer2115 2 ปีที่แล้ว +1

    Legends never die. Paul is a legend. I just wrote this little program.
    The state of the button is compared to its previous state.
    When pressed, a new event is detected and the state of the switch changes.
    The video can be found here: th-cam.com/video/jw0WTHfX5hw/w-d-xo.html
    #RESISTTHEMETAVERSE

  • @gary48500
    @gary48500 2 ปีที่แล้ว

    This is off topic sorry but have a video request, i have a Arduino and want to send data wireless form the PC to Arduino i have figured out how to send it to Arduino using visual studio GUI, but can't seem to send it back from Arduino to the PC and read the data i am using a temp sensor, thanks

  • @willgordon5737
    @willgordon5737 2 วันที่ผ่านมา

    Note: This is not the Homework Code
    Here is the code for the newer Raspberry pi models. anything starts with a # is comments(notes)
    import gpiod
    from time import sleep
    LED_PIN = 20;
    BUTTON_PIN = 21;
    RESPONSE_TIME = 0.1;
    # input output
    print("My second Program");
    chip = gpiod.Chip('gpiochip0');
    # GET LED LINE HERE
    ledLine = chip.get_line(LED_PIN); # GPIO 20 (BCM)
    ledLine.request(consumer='led_line', type=gpiod.LINE_REQ_DIR_OUT);
    line = chip.get_line(BUTTON_PIN); # GPIO 21 (BCM)
    line.request(consumer='input_test', type=gpiod.LINE_REQ_DIR_IN, flags=gpiod.LINE_REQ_FLAG_BIAS_PULL_UP); # Request the line as input
    try:
    while True:
    value = line.get_value();

    if value == 1: # if button is not pressed
    ledLine.set_value(0) # Turn LED off
    else: # if button is pressed
    ledLine.set_value(1) # Turn LED on

    print(f"
    Pin value: {value}", end='') # Update in place and not a new line ever time.
    sleep(RESPONSE_TIME);

    except KeyboardInterrupt:
    line.release();

  • @lorisrobots
    @lorisrobots 2 ปีที่แล้ว +2

    I am Uber Legend. I had many challenges with this homework. I understood the programming logic for this one because I had done a similar program for the Arduino in Paul's New Arduino course. I had issues with getting the "sleep" statements in the right places in my loop. Finally got it to work correctly. Here is my solution: th-cam.com/video/Ij4lJqkHvnk/w-d-xo.html

    • @paulmcwhorter
      @paulmcwhorter  2 ปีที่แล้ว +1

      Great work. Glad you remembered what you learned from the arduino lessons!

    • @keithlohmeyer
      @keithlohmeyer 2 ปีที่แล้ว +1

      Good job!

    • @vaughntaylor2855
      @vaughntaylor2855 2 ปีที่แล้ว

      Nice work and Great explanation!

  • @Spuzmax
    @Spuzmax ปีที่แล้ว

    Hello everyone!
    I am a bit late coming to the party, but I hope my efforts are well received.
    I decided to create a state value for the LED light. The program then looks for a press/release event on the button to trigger the flipping of the state value. I chose to keep the program simple without creating functions
    Cheers!
    import RPi.GPIO as GPIO
    import time
    LEDstate=0
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(37,GPIO.OUT)
    GPIO.setup(40,GPIO.IN,pull_up_down=GPIO.PUD_UP)
    try:
    while True:
    readVal=GPIO.input(40)
    if not readVal:
    readButtonVal=0
    while readButtonVal==0:
    readButtonVal=GPIO.input(40)
    if readButtonVal:
    LEDstate=(LEDstate+1) %2
    else:
    time.sleep(.01)
    else:
    GPIO.output(37,LEDstate)
    time.sleep(.1)
    except KeyboardInterrupt:
    GPIO.cleanup()
    Edit: I added the "else: time.sleep(.01)" to the "while readButtonVal==0:" loop because I realized that the CPU would be swamped waiting for the button release to occur.

  • @jimw9626
    @jimw9626 2 ปีที่แล้ว

    Did Paul post his solution? I haven't seen it. Here's mine:
    import RPi.GPIO as GPIO
    from time import sleep
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(40,GPIO.IN)
    GPIO.setup(37,GPIO.OUT)
    togg=0
    count=0
    delay=0.1
    readVal=0
    try:
    while True:

    while readVal==0:
    readVal=GPIO.input(40)
    sleep(delay)
    while readVal==1:
    readVal=GPIO.input(40)
    print(readVal)
    sleep(delay)
    count=count+1
    togg=count %2
    GPIO.output(37,togg)
    except KeyboardInterrupt:
    GPIO.cleanup()
    print('GPIO reset')

    • @keithlohmeyer
      @keithlohmeyer 2 ปีที่แล้ว

      Interesting! Always fun to see how others code a problem.

    • @jimw9626
      @jimw9626 2 ปีที่แล้ว +1

      @@keithlohmeyer Agreed. It makes me appreciate how coders can work in teams with everyone having a unique approach. BTW - I forget to mention my circuit puts a HIGH on the input pin when the button is pressed. I think my code will work the same in either case though.

  • @sbanerjee9311
    @sbanerjee9311 2 หลายเดือนก่อน +1

    Dear Mr. Paul, what a stupendous effort you've put in for this training series! This is absolute fun to watch and learn. I express my gratitude. Here is my toggle switch solution (I'm nowhere near a legend till I finish your entire training :) ): th-cam.com/video/txxFUU3ADmM/w-d-xo.html
    By the way, I use a power supply designed by myself, check it out if sounds interesting:
    th-cam.com/video/Bss2zhME33U/w-d-xo.html

  • @franciscotoledo8314
    @franciscotoledo8314 2 ปีที่แล้ว

    One friend told me that introducing delays is not a good practice in coding because it could cause a malfunction from the program in some cases.
    This is my proposal of solution for the homework. Below I am sharing it.
    from time import sleep, time
    import RPi.GPIO as GPIO
    interval=1000
    inPin=40
    outPin=38
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(outPin,GPIO.OUT)
    GPIO.setup(inPi,GPIO.IN,pull_up_down=GPIO.PUD_UP)
    state=0
    press=0
    prev=0
    last_time=0
    try:
    while True:
    if(time()*1000-last_time>interval):
    last_time=time()*1000
    readVal=GPIO.input(inPin)
    if not readVal:
    press=1
    else:
    press=0
    if (press==1 and press==prev):
    state=not state
    print('State = ',state)
    prev=not press
    if (state==1):
    GPIO.output(outPin,1)
    else:
    GPIO.output(outPin,0)
    except KeyboardInterrupt:
    GPIO.cleanup()
    print('GPIO Ready to Go")

    • @franciscotoledo8314
      @franciscotoledo8314 2 ปีที่แล้ว

      The part related to the delay could be omitted in this case cause the program is not expecting anything related to it. Nevertheless it could be used in the class exercise.

    • @elektron2kim666
      @elektron2kim666 ปีที่แล้ว

      if math.fmod(x, y) = 0 then it's time

  • @farshidowrang2762
    @farshidowrang2762 ปีที่แล้ว

    Takk!

  • @danielniels22
    @danielniels22 ปีที่แล้ว

    17:40

  • @willgordon5737
    @willgordon5737 2 วันที่ผ่านมา

    I AM ÜBER LEGEND

  • @brucemilyko8549
    @brucemilyko8549 ปีที่แล้ว

    I am uber legend with only 18 lines of code.

  • @RichPenn
    @RichPenn 2 ปีที่แล้ว

    Homework for Lesson 7 using pulldown in place of pullup -- th-cam.com/video/H60wg5wHYf0/w-d-xo.html

  • @WilliamBurlingame
    @WilliamBurlingame 2 ปีที่แล้ว

    My solution the Lesson 7 homework. I'm not sure how to handle the delays to keep the button working as a toggle.
    from time import sleep
    import RPi.GPIO as GPIO
    inPin = 40
    outPin = 38
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(outPin,GPIO.OUT)
    GPIO.setup(inPin,GPIO.IN,pull_up_down=GPIO.PUD_UP)
    delay = .1
    KeyBounceDelay = 1
    LED_Status = False
    try:
    GPIO.output(outPin,0)
    while True:
    readVal=GPIO.input(inPin)
    print(readVal)
    if readVal == 0:
    LED_Status = not(LED_Status)
    GPIO.output(outPin,LED_Status)
    sleep(KeyBounceDelay)
    sleep(delay)
    except KeyboardInterrupt:
    GPIO.output(outPin,0)
    GPIO.cleanup()
    print("GPIO Ready to Go")

    • @justinc2633
      @justinc2633 ปีที่แล้ว

      check for a change in button state from 1 to 0 and if true then change the value of a bool "toggled", KeyBounceDelay looks unnecessary to me

  • @JoRogan-l1v
    @JoRogan-l1v ปีที่แล้ว

    Thanks Paul. Here is a short video of the circuit working. th-cam.com/video/XcxU_X6MVds/w-d-xo.html

  • @robinedminson6461
    @robinedminson6461 ปีที่แล้ว

    Another superb session. I really do like your style. Struggled a bit with this homework (syntax & method of storing history) but succeeded after sleeping on it :) th-cam.com/video/q9f8z0b2XQc/w-d-xo.html

  • @FushigiMigi
    @FushigiMigi ปีที่แล้ว

    th-cam.com/video/Ychpcl2hHRI/w-d-xo.html
    haha. There has to be a better way, but it works.

  • @harrison4687
    @harrison4687 ปีที่แล้ว

    I remembered this from the Arduino series, so I looked back at my .ino file and used the concept.... th-cam.com/video/O-CrX86IJoU/w-d-xo.html

  • @itsmeintorrespain2714
    @itsmeintorrespain2714 2 ปีที่แล้ว

    Homework published :- th-cam.com/video/48n8qSLF3cY/w-d-xo.html