In this lesson, we will show how to turn on/off an LED by using an I/O port and a button. The “I/O port” refers to the INPUT and OUTPUT port. Here the INPUT port of the Osoyoo Uno board is used to read the output of an external device. Since the board itself has an LED (connected to Pin 13), so you can use this LED to do this experiment for convenience.



About the button

Buttons are a common component used to control electronic devices. They are usually used as switches to connect or disconnect circuits. Although buttons come in a variety of sizes and shapes, the one used here is a 12mm medium-size button as shown in the following pictures.

The little button that are used in this lesson have four connections, which can be a little confusing.

Actually, there are only really two electrical connections, as inside the switch package pins B and C are connected together, as are A and D.


Although the bodies of the switches are square, the pins protrude from opposite sides of the switch. This means that the pins will only be far enough apart when they are the correct way around on the breadboard.Build the circuit as below:


Generally, the button is directly connected in an LED circuit in order to turn on or off the LED. This connection is relatively simple. However, sometimes the LED will light up automatically without pressing the button, which is caused by various interferences. In order to avoid these external interferences, a pull-down resistor is used, that is, to connect a 1K–10KΩ resistor between the button port and GND. It is used to consume external interferences while connected to GND for as long as the button switch is turned off.

Code Program

After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled PWR) should go on.

We have learnt how to make an LED blink in last lesson. So in this one we will add a button to controlthe LED on/off.First, we need to declare a variable to show the Buttons state. Drag out a Declare block in Variablescat.

Name this variable to Buttonstate, and select the type to int.

To assign 0 to Buttonstate initially, drag out the first block in Math, and add it behind the Buttonstate. The number in the slot can be changed, here we set it as 0.

Click Variables again, and you can see there are the corresponding input and output blocks whichcan be used in later part.

To read the state of buttonstate, drat an input block in Variables

Drag out a digital Read block in the In/Out category and add it behind the buttonstate(input), toead the pin’s state value and assign it to buttonstate Here the button is connectedwere reading the state of pin 12, meaning using the variable buttonstate to represent the pins state.

Add an if do block in Control under button State. If a value is true. thent block of thestatement. Otherwise. run the second block.

If we need an else sub-block, just click the setting icon beside if:

Drag the else into the right if block, you can see it become if_do_else block,

Click the icon again to fold the pop-up

Next, we should add the condition of if, that is buttonstate==High- find the block in Logic.

Drag out the Buttonstate(output) in Variables, and drop it to the first slot.

Drag out the first block in In/Out, and placele second slot. Now, the if statement is completed.

If the condition is met, set the LED at pin 13 to high, namely to light up the LED.

Otherwise, set pin 13 to low level.

The whole program is as above. You can check the code on the right bar.

Click Save after all the programming is done. Select the board type and serial port before uploading.

Finally, the staus will change to ‘Upload success!’.

Runnig Result

Now, press the button, and the LED on the OSOYOO Uno board will light up. When the switch is released, the LED goes out.

The Expansion Example

Here’s a simpler way to program: Complete the connection according to the above operation, , set the corresponding board type and port type in the software, then upload the following code.

A few seconds after the upload finishes,when the button switch is pressed, LED light up; when the switch is released, the LED goes out.

Scientists, one of the most valuable quality is always maintained in this world childlike curiosity, to explore the vision, constantly pioneering and enterprising city