Lesson 4- Button

Lesson 4- Button

Introduction

In this chapter, we will learn about the onboard buttons of BreadPi and learn how to program them in Scratch, Python and C.

Components Required

  1. Raspberry Pi
  2. BreadPi

What is Button?

Buttons are the commonly used electronics device and are required to connect or break any circuit. The button which we are using are mini-buttons of size 6mm are present on the BreadPi. One side of the button is connected to Ground and other to the GPIO. While the button is pressed it will show 0V which will help you in programming.

Buttons are the input device. The circuit used for the button is a pull-down resistor circuit which gives a high input when the switch is pressed.

   


S. No.

Component

ONBOARD

BCM

WiringPi

1.

L1

32

12

26

2.

L2

36

16

27

3.

L3

38

20

28

4.

L4

40

21

29

5.

Buzzer

37

26

25

6.

Button SW1

29

5

21

7.

Button SW2

31

6

22


Scratch

We want to program the buttons such that on pressing the Button SW1 LED 1 will glow for 2 seconds and while pressing Button SW2 the buzzer will beep for 2 seconds. In this, we are not going to use any external buttons or LEDs. We will be using LED 1 ( BCM - 12), BUZZER ( BCM - 26), Button SW1 ( BCM - 5) and Button SW2 ( BCM - 6).

The blocks which we are going to use are from Events, Control and Raspberry Pi GPIO blocks. The if() Then control block will be needed in this as we have to check conditions.

If the condition is true, the code written inside the C space will run and the script will run like this. Here we have used 2 if() Then blocks for 2 buttons and for two different conditions. The first if() block is for button SW2, if its high( button is pressed) then the blinking of LED for 2 seconds code will be activated. The other if() block is for SW1 button and the code inside the C space is for buzzer beep for 2 seconds which will only run once the condition is true. Place these blocks inside the Forever block. For starting the code place any Event Block to start the code.  

PYTHON

Import the GPIO pins and time. Define the pin numbers for Buzzer and LED. Set them to output. When the condition is true the Buzzer will be high for 2 seconds and the LED will blink for a second. 


C Programming

 

Include the wiringPi and input/ output libraries. Define the wiringpi pin numbers for LED and Buzzer. In the main first write the WiringPi Setup condition and in the while loop write the condition to make buzzer high and LED blink.  


Lesson 3- GPIO Pins