Input(Button Function)

This tutorial is the second part of Tutorial 7  with few more  example code to demonstrate how to setup and read an input from a button,

The following code example based on led_on_while.py from Tutorial 7. If you already have led_on_while.py saved , open it up with editor. In case you have not got the mentioned script already saved, go back to Tutorial 7  .

Once you have led_on_while.py opened in editor, remove/add and change the following few lines.

Before you start adding the new lines to the script, remove line 9:

 

Add the following lines to the script:

Line 6:

Line 11:

Line 13:

The LED will lit for 10 seconds
Line 14:

And finally change the first time.sleep(0.2)line to time.sleep(10)

Your final could should look like this:

Save the script under a differenet name.

Run the program, and follow the on screen instructions.

 

Input(Button Function, 2 buttons)

The following example will demonstrate how to use the buttons on EduBoard (RED tactile switch, Black tactile switch) to turn ON and OFF the LED.

The following code example based on button_1.py from Tutorial 7. If you already have button_1.py saved , open it up with editor. In case you have not got the mentioned script already saved, go back to Tutorial 7  .

Once you have button_1.py opened in editor, change the GPIO BCM pin numbering back to GPIO Board pin numbering. To do this you will need to change the 3 line of the code:


Now define your second button. Add the following line to your code, above this:
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) ## Enable pull-up resistor on pin 18

Line to add:

Also define the output pin for the LED. Add the following line to your code, right under:

GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) ## Enable pull-up resistor on pin 18

Line to add:

To being able to use both button inputs, we will need to declare the second button state in the script within the while loop. As we are going to use 2 buttons, we will call the inputs,  input1_state and input2_state. To create the second button state simply use the original input_state syntax as an example. See below.

And create 2 input state syntax within the loop.

This should look like this:

Now, rename the input states. The first input_state to, input1_state, the second input_state to input2_state.

Dont forget to change the GPIO pin numbering at input1_state, to 12.

For each button state, we will need to add the following lines to turn ON, or OFF the LED.

For input1_state, add the following line right under:

Line to add:

For input2_state, add the following line right under:

Line to add:

At the print lines, rename the button statement to Button 1, and to Button 2.

“Indentation”

“Python uses indentation to determine the grouping of statements. You can use one or more spaces or one or more tab, just that within the same statement always use one or the other. Is recommended to use spaces instead of tabs, but is not required. Read here”

 

Your final code should look like this:

Save the script under a different name. Run the program, and press the RED button, the LED should no lit. The black button will switch the LED off.

 

  Next Tutorial 9 (Usage of the SSD1306 OLED Display, I2C)

 

The code below only an example to demonstrate multiple function, and button state use.

Try it out..