![]() You enable these internal pull-ups/pull-downs at the time of setting up the port for input or output, by adding an extra, optional, argument to the tup() function call. How do you do that? RPi.GPIO to the rescue. This means we can eliminate our pull-down resistors for the button – as long as we enable the internal ones. This was to demonstrate the idea.īut, in fact, the Raspberry Pi has built-in pull-up and pull-down resistors which can be enabled in software. In our button circuit, we used resistors to pull down the voltage. What’s all this about internal ones then? You can easily get it under control by using pull-up or pull-down resistors, so that’s what we do. Or Granny might be sent back upstairs when she wants to be downstairs. If that port is susceptible to random changes of state, the propellor might spin when it shouldn’t and hurt someone. So imagine the situation where you have a motor with a propellor on it, or one which controls Granny’s stairlift, which is controlled by a GPIO input port. Any wires attached to the GPIO ports act as antennae for this radiation (it’s mostly radio waves). It is susceptible to random electromagnetic radiation or static from you, from any devices near or far and from the environment. If you have no pull-up or pull-down resistors attached to an input port, its status is not clearly defined. I mentioned in day 5 that we’d cover this in more detail, so that’s what we’re doing now. This gives it a “default” state of 0V (0, LOW, False). In the button circuit, we’re using resistors to “pull-down” the port. Flashing: Reinstate the flashing, while keeping the “on – off” light switch mode.( You might need to investigate a little bit about “button debounce” if you run into difficulties) Light switch mode: Make it switch on the LED when the button is pressed and released once and leave it on until the button is pressed and released again.Fewer messages: Only display a message when the button status changes, rather than on every iteration of the loop.I can think of several improvements you could try to make. That’s the input/output (I/O) part sorted, now it’s over to you to improve the script. It keeps going until CTRL+C is pressed, then the ports are cleaned up before exit.otherwise, if not pressed (input port 25 = 0), button status is displayed and the LED is switched off (output port 24 is set to 0).if pressed (input port 25 = 1), button status is displayed and the LED is switched on (output port 24 is set to 1).GPIO.output(24, 0) # set port/pin value to 0/LOW/Falseįinally: # this block will run no matter how the try block exitsĮvery 0.1s, this program checks the button status… GPIO.output(24, 1) # set port/pin value to 1/HIGH/True While True: # this will carry on until you hit CTRL+C tup(24, GPIO.OUT) # set GPIO24 as an output (LED) tup(25, GPIO.IN) # set GPIO25 as input (button) tmode(GPIO.BCM) # set up BCM GPIO numbering This takes bits from each of the previous two programs.įrom time import sleep # this lets us have a time delay (see line 15) This is simply a combination of the circuits from the last two exercises.Ĭircuit for combining button input and LED output with external pull-down resistor Here’s a simple Python program I = GPIO.input(port_or_pin) # read status of pin/port and assign to variable i GPIO.output(port_or_pin, 0) # set an output port/pin value to 0/LOW/False GPIO.output(port_or_pin, 1) # set an output port/pin value to 1/HIGH/True tup(port_or_pin, GPIO.OUT) # set a port/pin as an output tup(port_or_pin, GPIO.IN) # set a port/pin as an input Import RPi.GPIO as GPIO # import RPi.GPIO module But before we do that… Here’s a quick recap of inputs and outputs with RPi.GPIO We’re going to make a simple new program which switches the LED on when the button is pressed and switches it off again when the button is released. We’re going to make a new program which takes parts from both the “read a button press” and the “flash an led every half second” programs. To make it super-easy, we’ll even stick to the same port numbers we used for the last two days. It’s simply a case of doing what we’ve already done in the last two days’ of GPIO basics, but combining them. It’s nothing scary and it’s not new either. Today, it’s time for us to combine inputs and outputs into the same script. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |