2. Chapter Button and leds

Usually, there are three essential parts in a complete automatic control device: INPUT, OUTPUT, and CONTROL. In last section, the LED module was the output part and RPI was the control part. In practical applications, we not only make LEDs flash, but also make a device sense the surrounding environment, receive instructions and then take the appropriate action such as turn on LEDs, make a buzzer beep and so on.

../../../_images/button-led-flow-chart.png

Next, we will build a simple control system to control an LED through a push button switch.

2.1. Project Push Button Switch & LED

In the project, we will control the LED state through a Push Button Switch. When the button is pressed, our LED will turn ON, and when it is released, the LED will turn OFF. This describes a Momentary Switch.

2.1.1. Component List

  1. Raspberry Pi (with 40 GPIO) x1

  2. GPIO Extension Board & Ribbon Cable x1

  3. Breadboard x1

LED x1

red-led

Resistor 220Ω x1

res-220R

Push Button Switch x1

button-small

Resistor 10KΩ x2

res-10k

Jumper Wire

jumper-wire

Please Note: In the code “button” represents switch action.

2.1.2. Component knowledge

2.1.2.1. Push Button Switch

This type of Push Button Switch has 4 pins (2 Pole Switch). Two pins on the left are connected, and both left and right sides are the same per the illustration:

../../../_images/button-small-img-sch.jpg

When the button on the switch is pressed, the circuit is completed (your project is Powered ON).

2.1.3. Circuit

  1. Schematic diagram

    ../../../_images/button-led-sch1.jpg

    Note

    R3 is used to limit current to protect GPIO 18, if you set it to output HIGH level by mistake.

  2. Hardware connection

../../../_images/button-led-hdc1.jpg

Note

There are two kinds of push button switch in this kit.
The smaller push button switches are contained in a plastic bag.

Hint

If you need any support, please feel free to contact us via: support@freenove.com

  1. This is how it works. When button switch is released:

../../../_images/button-led-released.png
  1. This is how it works. When button switch is released:

../../../_images/button-led-pressed.png

2.1.4. Code

This project is designed for learning how to use Push Button Switch to control an LED. We first need to read the state of switch, and then determine whether to turn the LED ON in accordance to the state of the switch.

2.1.4.1. Electronic_Button_LED

Hint

If you have any concerns, please contact us via: support@freenove.com

Load the code to scratch3.

Freenove_Kit/Code/Scratch3/02.0_Electronic_Button_LED.sb3

Click the green flag. Then when you press button swtich, the LED will be turned on. Otherwise, the LED will be turned off.

../../../_images/scratch_button.png ../../../_images/scratch_input.png

is to set a GPIO to input mode, which is used to detect button swtich condition.

2.1.4.2. Floating_Ballon

The circuit is same as previous section.

Load the code to scratch3.

Freenove_Kit/Code/Scratch3/02.1_Floating_Ballon.sb3

Click the green flag. Then when you press button switch, the ballon will rise. Otherwise, the ballon will fall. When the ballon is too high or too low, game is over. The falling speed will increase over time.

../../../_images/scratch_float_ballon.png

If we select varianle of left, they will be shown on right.

../../../_images/scratch_right.png

Operators are used in this code.

../../../_images/scratch_operators.png