Raspberry Pi LESSON 8: Using a Button as a Toggle Switch for LED Control

Поділитися
Вставка
  • Опубліковано 10 січ 2025

КОМЕНТАРІ • 77

  • @ubuntukali2997
    @ubuntukali2997 2 роки тому +3

    Thank you Mr. Paul; As you are really connected to the source, your teaching is powerful as usual;

  • @charlotteswift
    @charlotteswift 2 роки тому +12

    I've felt so sorry for all the people who want to follow along with these tutorials but who can't source/afford a Pi 3/4/400.
    As Picos are cheap and readily available, I wondered if it would be possible to use one instead. And, yes, it is!! So far...
    I've put the code in the description for each video and I've created a playlist (with some lame blurb that probably wouldn't help anyone) on my channel in case anyone is interested.
    This is the link for the playlist: ua-cam.com/play/PLWJsuyqCiVXOlL2FMW89ASuS-4BdNNDTj.html

    • @Wythaneye
      @Wythaneye 2 роки тому +1

      Thanks for going through this, Charlotte. Will you please show your code as well? I'm somewhat interested in the Pico, and would love to see how you're accomplishing this.

    • @WilliamBurlingame
      @WilliamBurlingame 2 роки тому +1

      GPIO pins ore different, no WiFi and Micro Python isn't 100% the same as python 3.

    • @floridian080
      @floridian080 2 роки тому

      That is a great idea, particularly as you succeeded in making it work. I bought a Pico about a year ago and had trouble with its onboard memory. I'm sure that's because I misunderstood the directions.

    • @89StarquestTSi
      @89StarquestTSi 2 роки тому

      Pico has more in common with Arduino nano than RPi4. I program my unos and Pico's USING my RPi4. Gpio is different, the RPI 4 has the ability to do a headless VNC/ssh boot (when I don't have a head to display to I can boot my RPI for and then connect via VNC from my Android phone and control the desktop GUI or the command line interface), micropython (pico/Arduino) is not the same as Python 3 or c/c++, etc.... There's a lot of differences between them

    • @charlotteswift
      @charlotteswift 2 роки тому +1

      @@Wythaneye All my code is in the comments on the videos. I'm not as clever as most of the people doing these lessons but trying to do things on the Pico gave me the incentive to actually take it out of its bag for the first time. Next thing is my soldering iron but that is REALLY scary!

  • @soteros1983
    @soteros1983 2 роки тому +4

    I love the way Mr. McWhorter explains everything! I really wish someday He teaches Raspberry Pi Pico with Micropython!! :)

  • @keyboardlearning7552
    @keyboardlearning7552 2 роки тому

    I am Uber LEGEND - Many thanks for this lesson. I did the homework after Lesson 7, using Nested If Statements, and it went fine.

  • @notdecraw
    @notdecraw Рік тому +2

    A little late to the party, but: I am UBER legend. The trick was realizing I had to test the button state and see if it had been released between the last iteration and this one. Paul's big clue was to hold the button down and show us that the LED's state changed upon releasing the button, not upon pressing it. Fun project!

  • @kamilkopyra4893
    @kamilkopyra4893 Місяць тому

    Hey Paul, your course is amazing. Im sending my solution below:
    from time import sleep
    import RPi.GPIO as GPIO
    delay = 0.1
    inPin = 40
    outPin = 38
    currentState = 0
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(outPin, GPIO.OUT)
    GPIO.setup(inPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    try:
    GPIO.output(outPin, currentState)
    while True:
    if GPIO.input(inPin) == 0:
    currentState += 1
    GPIO.output(outPin, currentState % 2)
    while GPIO.input(inPin) == 0:
    sleep(delay)
    sleep(delay)
    except KeyboardInterrupt:
    GPIO.cleanup()
    print('Program has been interrupted')

  • @lorisrobots
    @lorisrobots 2 роки тому +2

    Excellent lesson! Thanks for the great content.

  • @KurtVSJ
    @KurtVSJ Рік тому

    Paul have you tackled using one button for two functions somewhere? (Short press / long press) It would be grand if you could help with this!! Many thanks

  • @farshidowrang2762
    @farshidowrang2762 2 роки тому +1

    Takk!

  • @reubenthomas1033
    @reubenthomas1033 2 роки тому +1

    Hey Paul! Do consider teaching us ROS programming for the next series. That would be sweet!

  • @RobVollmar
    @RobVollmar 2 роки тому

    Paul,
    I was a little confused by the 'not LEDState' line. I understand how it works and the function of the 'if' statements you proposed could replace it. We define LEDState as a variable up top but we could have as easily assigned it a number like 8 or 514. How does that operation know that we are thinking of LEDState as specifically as a 1 or 'not a one' -ie 0?

  • @sinaghadimi1804
    @sinaghadimi1804 Рік тому

    Hi, I used counter method, as if it was devidable by 2, it would turn on and if no turn off, this way, I could make it blink by holding the button too.

  • @fredzerbe5819
    @fredzerbe5819 2 роки тому

    Paul,
    Enjoyed the lesson. Thank you.
    Question: can the pull up or down resistor be put in the LED circuit as well?
    Thanks
    Fred

  • @pralaymajumdar1206
    @pralaymajumdar1206 2 роки тому

    Outstanding lesson

  • @dooner9446
    @dooner9446 7 місяців тому

    Hi, managed to solve this slightly differently, one problem I had to solve was not knowing the not function, I tried to do what Paul said
    (if LEDstate==0:
    LEDstate=1
    “Do somthing”
    If LEDstate==1:
    LEDstate=0
    “Do somthing else”)
    But doing that will always end up with LEDstate as 0 (as the first if sees a state of 0 so makes it 1, the second then sees the 1 and makes it a 0 again)
    Using an else instead of a second if was my solution, and of course there is Paul’s.
    Just thought I’d post in case anyone was having similar issues and couldn’t work out why it didn’t work :)

  • @jessekloberdanz246
    @jessekloberdanz246 2 роки тому

    Thanks for another great video. I do 1-2 of your lessons each night. I have a piJuice Hat on my RPi 4. Have you seen those? They are cool, battery HAT with a solar controller for panels. Works really well on standard pi linux distribution. Thought with your prepper interest I would mention it encase you had not seen it.

  • @JUNWEIONG-r2b
    @JUNWEIONG-r2b Місяць тому

    May I know does the button has specific name?

  • @pbs570
    @pbs570 2 роки тому

    Hi. I really like your video. They're super helpful. I learned arduino uno with your videos. I really appreciate all the efforts you're putting in.
    I've got a favor to ask. Have you got any plans for teaching us a bit about arm programming? Specificly Stm series which are used almost everywhere. Or at least could you help us to get started? I don't know where to start.
    Thank a lot

  • @ettienemare
    @ettienemare 3 місяці тому

    Pro tip: use a needle or pin to re-open the hole for the strip that contains the resistors, It will slide in easy,

  • @aniketagarwadekar6188
    @aniketagarwadekar6188 2 роки тому

    Hey paul..can you please help me in my project.?..i need a arduino code to drive Hub BLDC motor using joystick to go forward backward left and right ,using stp75nf75 igbt and ir2103 IC as the driver circuit.please help

  • @sgodsellify
    @sgodsellify 2 роки тому +1

    could you add a 3.3 volt, 1watt, 350 milli amp, led bead light (100 Lumen) to your Raspberry Pi?

    • @lensman192
      @lensman192 2 роки тому

      Not directly. You should only draw 3 milli amps per output pin, but many sources say a maximum of 16 ma per pin. The maximum total current draw for all pins used is 50 ma. You can use an output to drive a transistor or a relay (or one of several devices) to increase your output current, but use a different power source than your Raspberry Pi to supply the current.

    • @89StarquestTSi
      @89StarquestTSi 2 роки тому

      Theoretically yes, but the proper way would be through a power transistor or mosfet having the base or gate switched by the gpio of the RPI4

  • @VeryUsMumblings
    @VeryUsMumblings 2 роки тому +1

    This is a really good video, but you got some extra views and re-views from me because I spent too long wondering what I did wrong, only to find out I didn't capitalize the State in buttonState. I will forever remember that States are supposed to be capitalized. 😀

  • @MrProvocer
    @MrProvocer Рік тому

    hey Guys, I'm a real noob here. Didn't manage to do the homework, as I'm still wrapping my head around Python. I copied Paul's code, but I don't get the link between the setup line (buttonStateOld=1) and the 2nd part of the if line (...and buttonStateOld==0:
    If we recorded for the program the status as 1, how would it know that when the button was pressed the buttonOldState was 0, especially that 3 lines later we tell it that the buttonStateOld needs to be 1 (buttonStateOld=buttonState), as the code keep running dry before we actually press anything?

  • @damianbutterworth2434
    @damianbutterworth2434 10 місяців тому

    i`m using a pull down resistor on the switch just in case. Might be 10k ohms.

  • @ion4497
    @ion4497 3 місяці тому

    great script thanks !!

  • @danielniels22
    @danielniels22 2 роки тому +1

    2:13 I made it. No googling, nothing.
    I AM UBER LEGEND

  • @RobVollmar
    @RobVollmar 2 роки тому +3

    I folded like a cheap lawn chair.

  • @tkingstl
    @tkingstl 2 роки тому

    21:27 The ant marches through the LED and makes a run for the kill switch

  • @captinkrazy6968
    @captinkrazy6968 Рік тому

    i dont get how buttonState goes to buttonStateOld. thats where i get confused.

    • @captinkrazy6968
      @captinkrazy6968 Рік тому

      everything makes sense except for the programming to me.

  • @jonathanlanders9406
    @jonathanlanders9406 Рік тому

    The way I did this in the "New Arduino tutorials" is how I did this assignment here. The key part of this code is to use an if statement that is "watching" for the button to be pressed followed directly by a while loop that continues so long as the button is held down:
    if tog0V == 0:
    while tog0V == 0:
    tog0V = GPIO.input(tog0)
    1. At the start of the program, tog0V (my variable for the pushbutton voltage value) equals 1, so the program skips the if statement and the code under it. Nothing changes.
    2. After the button is pressed, tog0V == 0, so the program "enters" the if-statement.
    3. Directly following the if-statement, their is the while loop that continues so long as tog0V ==0. Since the button was pressed down, tog0V is at 0. It will remain at 0 until it is released where it switches to one and exits the while-loop.
    4. Upon exiting the while-loop, the program runs through a set of if/elif statements that turn the LED on or off and reset the LED state.
    This reduces the number of variables as you do not need to assign a old and new toggle value. You can just use one variable.

    • @paulmcwhorter
      @paulmcwhorter  Рік тому

      Nice approach

    • @jonathanlanders9406
      @jonathanlanders9406 Рік тому

      @@paulmcwhorter I think I had this idea from your lesson in Arduino about using the serial port; when Serial.Available(), "do nothing".
      This method is not very easy to use when using more than one button (like in the next lesson), as the while loop freezes the code if you hold down a button.

  • @edwingrajo
    @edwingrajo 2 роки тому +1

    I did it. I am successful

  • @bozinoski
    @bozinoski Рік тому

    Got it working :). This was my solution:
    import RPi.GPIO as GPIO
    inPin = 40
    outPin = 38
    powerOn = True
    released = True
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(inPin,GPIO.IN,pull_up_down=GPIO.PUD_UP)
    GPIO.setup(outPin,GPIO.OUT)
    try:
    while True:
    readVal = GPIO.input(inPin)
    if readVal == 1:
    released = True
    if readVal == 0:
    if powerOn == True and released == True:
    powerOn = False
    released = False
    GPIO.output(outPin,0)
    if powerOn == False and released == True:
    powerOn = True
    released = False
    GPIO.output(outPin,1)
    except KeyboardInterrupt:
    GPIO.cleanup()
    print("GPIO Ready to Go")

  • @tompaquette7138
    @tompaquette7138 2 роки тому

    I want to see a solution to the opposite! A toggle switch that operates like a button.

  • @liamoliverdarroch6482
    @liamoliverdarroch6482 Рік тому

    I am uber legend!!!!!!!!!!!

  • @FushigiMigi
    @FushigiMigi 2 роки тому

    I am uber legend.

  • @tkklein
    @tkklein 2 роки тому

    I will not be able to join because I have a license test.

    • @paulmcwhorter
      @paulmcwhorter  2 роки тому +1

      What kind of License?

    • @tkklein
      @tkklein 2 роки тому

      @@paulmcwhorter 😂 Drivers. Sorry

  • @peterbush3972
    @peterbush3972 Рік тому

    yes solved 16 lines

  • @akshaysubash3176
    @akshaysubash3176 11 місяців тому

    I'm an uber legend

  • @anwar6971
    @anwar6971 2 роки тому

    You could have used if else. Then using NOT

  • @johnhechtlinger9465
    @johnhechtlinger9465 Місяць тому

    I folded like a cheap walmart lawnchair, though not for lack of trying.

  • @dillonhamer4135
    @dillonhamer4135 6 місяців тому

    I am Uber Legend!

  • @genepierson1728
    @genepierson1728 2 роки тому

    I went to this video and saw that you used two variable instead of three. Easier for me to understand (the old way). Why use three variables this time? ua-cam.com/video/9X5z2lxe8Vs/v-deo.html

  • @chrischon1
    @chrischon1 2 дні тому

    I am an Uber Walmart lawn chair.

  • @Codetech50
    @Codetech50 5 місяців тому

    i am a cheap walmart lawn chair

  • @ettienemare
    @ettienemare 3 місяці тому

    import RPi.GPIO as GPIO
    import time
    led_pin = 4
    input_pin = 17
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(led_pin, GPIO.OUT)
    GPIO.setup(input_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    is_on = False
    input = True
    try:
    while True:
    time.sleep(0.1)
    if input == GPIO.input(input_pin):
    continue
    if GPIO.input(input_pin) == GPIO.HIGH:
    is_on = not is_on
    GPIO.output(led_pin, is_on)
    input = not input
    except KeyboardInterrupt:
    GPIO.output(led_pin, GPIO.LOW)
    GPIO.cleanup()

  • @larrypaige3092
    @larrypaige3092 Рік тому

    folded up like a cheap walmart lawn chair

  • @tkklein
    @tkklein 2 роки тому

    😂

  • @davidjmcclelland
    @davidjmcclelland Рік тому

    I am UberLegend

  • @MensahJ
    @MensahJ 7 місяців тому

    I AM UBER LEGEND

  • @89StarquestTSi
    @89StarquestTSi 2 роки тому

    Definitely had fun with this one. Setting the min and max window isn't the same as c/c++/Arduino.
    I used : integer = max(0, min(integer, 100)) to limit the duty cycle value to 0.0-100.0. if you don't then the program gets finicky with pwm.ChangeDutyCycle(value)
    ua-cam.com/video/CMk9q_fk9bw/v-deo.html
    Edit: just realized I shared this on lesson 8 and not 9 🤦 🤣