Magic Button Switches | MindPlus Coding Kit for Arduino Started Tutorial E03

0 4900 Easy

Chapter 2 Magic Buttons

In the three projects following up, we will learn how to perform simple controls by input module

like a switch, and simulate some common life scenarios with buttons.

Project 1 Magic Button Switches

Button switches, also known as key switches, were called sensitive switches in the early days. They are widely applied in lights, socket master switches, doorbells, and car consoles, etc. Button switches add another protective layer to the usage of electricity. As they facilitate the controls of electrical appearance, they also further the protection of electrical components.

As we learn Arduino, we will be exposed to a variety of input devices. Among them, the button switch is the simplest and most widely used one. Here we will use Arduino to control the LED lights to make button pressing switch the light on and off every other time.


Task Navigation

1. Learn the basics of digital input and conditional statement structure

2. Turn on the LED with the button

Key Points Analysis

1. What is a digital input?

There are only two states, "high" or "low", which are called digital signals. Monitor the level "high" or "low" via digital pins.

Button pressed down means high, and button released it is low. At the same time, the button is a device that obtains external actions. We call it an input device.

2. What is a conditional statement structure?

The condition statement structure refers to a program structure that needs to decide if an operation to is be performed according to whether certain given conditions are met. Conditional statement is to check a condition and if that condition is met, execute.

Here we will learn the first structure of conditional judgment: if

Its semantics are: if the expression is true, then execute what follows; otherwise, the following statements are not executed. The process can be expressed as the following figure.


There are two types of modules related to conditional statement structure in Mind+: true conditional execution and true or false conditional execution.


Command List



1. Hardware connection

The pins that support digital input in UNO are 2 to 13 and A0 to A5 (In fact, P0 and P1 also support digital input. However, they are often occupied by serial communication and thus not usually used.).

1. Connect button module: insert the 3-pin interface of the button module directly to the Digital Pin 3 of Arduino Uno. You can also choose other digital pins, but do remember changing the digital pins in the code to the corresponding pin numbers.

2. Connect the LED module: plug the 3-pin interface of the LED module directly to the Digital Pin 10 of Arduino Uno.


Hardware connection (LED-10, button-3)

Please match the colors when plugging

2. Programming

On the basis of controlling LEDs directly with UNO, we introduce a button to control the LED, forming a relationship of “input-control-output”, as shown below:


In theory, the input device sends a signal to the control device; the control device receives the signal and processes it, and then controls the output device to perform the corresponding output work.

In button-controlled LED, the button module sends a signal (high / low level) to UNO; UNO receives the signal and processes the signal according to the command set by the code, and finally controls the LED to turn on / off. At this moment, we need to introduce conditional statement structure as a bridge between the button and LED control.

1. Write the program


Pay attention to changing “0” to the corresponding pin number. In the example, the button is connected to Pin 3.

2. Reference program


3. Program effect

Button pressed down, the LED is on; button released, the LED is off.

4. Program analysis

Conditional statement structure needs to check all input signals from input devices. We do we only have to set two conditions? The reason is that button input has only two cases, 1 and 0.


When the button is pressed, Pin 3 inputs high level and Pin 10 outputs high level accordingly;

When the key is released, Pin 3 inputs low level, and Pin 10 outputs low level at this time.

Press Input-> Master-> Output Analysis

Button (input): button pressed down, Pin 3 connected to the button inputs high level; button released, Pin 3 inputs the low level.

UNO (Control): Digital Pin 3 reads high or low level, and Digital Pin 10 outputs high or low level accordingly.

LED (output): receives the high or low levels of UNO output; high means on and low means off.

Further Exercise

Now we know how to control LED with button switch, can we control a small speaker in the same way? What about a small fan?

icon Program 2-1.rar 165KB Download(4)
All Rights