Led Pattern Button Push Button to Turn a Led on and Stay on Until Pressed Again

A great starter hardware project using the Raspberry Pi is hooking upwardly a simple tactile switch to the Raspberry GPIO and detecting button presses in Python.

This guide will guide you through setting upwards the circuit with the Raspberry Pi and reading the land of the push button in Python. Integrating the button with Python allows you lot endless possibilities of actions to execute on a button press. Yous could send a tweet or plough on a LED.

What y'all'll demand for this guide

We will need the following tools to complete the project:

  • Raspberry Pi 3 (or newer) or Raspberry Pi Cipher setup with monitor and USB Mouse & Keyboard
    • Checkout this guide if you need aid
  • Solderless breadboard
  • Jumper wires
  • Resistor pack
  • Tactile switch / push
  • Multimeter (optional)

Setting up the circuit

Connecting the Raspberry Pi's full general purpose input output ports (GPIO) to a momentary tactile button button switch is a adequately simple circuit.

We connect one side of the switch to an input pin on the Raspberry Pi, in this case we use pin 10. The other side of the switch nosotros connect to 3.3V on pin 1 using a resistor. The resistor is used as a current limiting resistor to protect our input pin past limiting the amount of current that tin can flow.

The circuit looks as follows:

The idea is that the input pin will be low (0V) when the push is not pushed. When the button is pushed it will connect the pin to 3.3V and modify the land to loftier (3.3V).

When you hook up the excursion yous can apply the post-obit reference to diagram to find the right pin numbers.


Writing the Python program to read the GPIO pin

With the excursion created we need to write the Python script that actually reads the country of the button and executes lawmaking based on the country.

Earlier we start writing the software we first need to install the Raspberry Pi GPIO Python module. This is a library that allows the states to access the GPIO port directly from Python.

To install the Python library open up a terminal and execute the post-obit

$ sudo apt-get install python-rpi.gpio python3-rpi.gpio

With the library installed at present open your favorite Python IDE (I recommend Thonny Python IDE more data about using it here).

Our initial script will initialize the GPIO port and then continuously read the status of the pin until nosotros leave the programme.

First we import the GPIO library and and then setup the library to utilize board numbering. We then initialize pin ten as an input pin, and instruct the Raspberry Pi to pull the pin low using the pull_up_down parameters.

The initialization code looks equally follows:

import RPi.GPIO equally GPIO # Import Raspberry Pi GPIO library  GPIO.setwarnings(False) # Ignore warning for now GPIO.setmode(GPIO.Board) # Use physical pin numbering GPIO.setup(ten, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set up pin 10 to be an input pivot and set initial value to exist pulled depression (off)

The pull_up_down parameter in the GPIO.setup call tells the Raspberry Pi which state the pin should be in when there is nothing connected to the pin. This is important since nosotros want our programme to read a low country when the button is non pushed and a high state when the button is pushed.

With the port initialized nosotros can write the code that continuously reads the port and outputs a message when the button is pressed. We utilize the GPIO.input office to read the land of the port.

while True: # Run forever     if GPIO.input(10) == GPIO.High:         print("Button was pushed!")

We can at present execute this plan past saving information technology as push_button.py and running it either in Thonny or in the panel as follows:

$ python3 push_button.py

Y'all'll observe that when you push the button the script outputs "Push button was pushed!" many times. This is because we are continuously reading the state of the push. To go effectually this effect we tin employ a GPIO outcome. We will talk through how to practice this in the next section.

If the plan does not piece of work, or continuously outputs "Push button was pushed!" without the button existence pressed down, endeavor rotating the push button 90 degrees.

Event based GPIO input in Python

We desire to rewrite our program to output a unmarried message whenever the button is pressed rather than continuously outputting a bulletin. To do this we need to use GPIO events.

A GPIO consequence in the Raspberry Pi Python GPIO library works by calling a Python part whenever an effect is triggered. Such a role is called a callback function.

An event can exist an input pin being low or high, but it could also exist when the pin changes from low to high – called rise – or when the pin changes from high to low – chosen falling.

In our instance nosotros desire to detect when the button is being pressed, that is going from depression to high as well called the ascension border.

Before nosotros setup the event we must however kickoff write the callback function to be executed when the outcome is detected. The callback function is a regular Python function and as such tin can contain whatever Python code, information technology could ship out a tweet or as we do in our example simply print "Button was pushed!".

At the summit of our program nosotros import the GPIO library and ascertain the function equally follows:

import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library  def button_callback(channel):     impress("Button was pushed!")

Next the program will initialize the input pin every bit follows:

GPIO.setwarnings(False) # Ignore warning for now GPIO.setmode(GPIO.BOARD) # Use physical pin numbering GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Ready pin 10 to be an input pin and set initial value to be pulled depression (off)

With the pin defined as a input pin nosotros can adhere the event to the pivot.

GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback) # Setup result on pivot 10 ascension edge

Notice how we provide the role name every bit the callback parameter such that the library knows which function to call when the outcome is triggered.

Finally we instruct python to look for keyboard input and when someone presses enter cleanup the GPIO input library resources and finish the program.

message = input("Press enter to quit\north\n") # Run until someone presses enter GPIO.cleanup() # Clean upwards

The combined code looks as follows:

import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library  def button_callback(channel):     print("Button was pushed!")  GPIO.setwarnings(False) # Ignore alert for now GPIO.setmode(GPIO.Board) # Use physical pin numbering GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pivot 10 to exist an input pin and ready initial value to be pulled low (off)  GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback) # Setup event on pivot 10 rising edge  message = input("Printing enter to quit\due north\n") # Run until someone presses enter  GPIO.cleanup() # Clean upwardly

We tin now save the file and execute the program either in Thonny or on the control line with:

$ python3 push_button.py

Yous now see that the program just outputs one "Button was pushed!" everytime the button is pushed.

This guide showed you how to read the state of a push switch using the Raspberry Pi and the Python programming linguistic communication. For more tips and guides on using electronics with the Raspberry Pi checkout the electronics section and join our newsletter!

scottmanne1937.blogspot.com

Source: https://raspberrypihq.com/use-a-push-button-with-raspberry-pi-gpio/

0 Response to "Led Pattern Button Push Button to Turn a Led on and Stay on Until Pressed Again"

Отправить комментарий

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel