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.
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!
best beginners' lesson !!! Paul you made everything clear and simple. Can you give another lesson on the hardware fundamentals of this build-in resistor?
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.
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.
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")
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....
@@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.
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
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.
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
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
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);
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
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.
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 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.
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
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")
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.
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")
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
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.
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!
best beginners' lesson !!! Paul you made everything clear and simple. Can you give another lesson on the hardware fundamentals of this build-in resistor?
I wish I could have paid attendion. The ant crawling around kept distracting me. 😂
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.
Nice job!
Groovy...I really want a raspberry pi garden and a cool robot
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.
We missed you in the chat!
@@paulmcwhorter Thanks, Paul. It's a pain getting old!
@@opalprestonshirley1700 It's better than the alternative!
you are a truly blessed man brother in Christ!!
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")
Nice Job.
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....
@@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.
@@Lehibob It is solid on mine. Did you put a delay in your loop?
@@Lehibob If you held the switch, you might be experiencing "switch bounce."
Like always worth of million dollars lesson.
Amazing! Thanks Paul
I am uber legend!!!!!!!!!!!!!!!!
And I only use 7 more lines of code than you said you did to do it :p
Thank you. Top content as always.
Much appreciated!
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
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.
Hi sir. do you have a 24hour timer lesson to activate an output? can i know what lesson is that?
What exactly is the delay for and why is it necessary??
Its just a variable used for sleep time. delay = 1; sleep(delay)
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
#RESISTTHEMETAVERSE
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
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();
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
Great work. Glad you remembered what you learned from the arduino lessons!
Good job!
Nice work and Great explanation!
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.
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')
Interesting! Always fun to see how others code a problem.
@@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.
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
LEGEND!
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")
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.
if math.fmod(x, y) = 0 then it's time
Takk!
17:40
I AM ÜBER LEGEND
I am uber legend with only 18 lines of code.
LEGEND!
Homework for Lesson 7 using pulldown in place of pullup -- th-cam.com/video/H60wg5wHYf0/w-d-xo.html
Well done.
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")
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
Thanks Paul. Here is a short video of the circuit working. th-cam.com/video/XcxU_X6MVds/w-d-xo.html
LEGEND!
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
th-cam.com/video/Ychpcl2hHRI/w-d-xo.html
haha. There has to be a better way, but it works.
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
LEGEND!
Homework published :- th-cam.com/video/48n8qSLF3cY/w-d-xo.html
Nice!
Super description! Thank You!