I/O means INPUT interface and OUTPUT interface,so far the LED experiment we designed just use the I/O output function of Arduino. Do you remember the RGB project? Yes,that is an example of OUTPUT.And today,we use a push button and a led light to make an experiment use both input and output to let you know more about I/O.

The push button is a component that connects two points in a circuit when you press it. The example turns on an LED when you press the button.

Preparation

In this project,we need some components like these:

Arduino UNO R3 board *1

Push button * 1

Red 5mm LED *1

Breadboard *1

220Ω Resistor * 1

10KΩ Resistor * 1

Jumper wires * 1 Bundle

 

Connection

Next,we should connect these hardware.

We connect these components according to the circuit diagram.To make it more easier,we put the connection picture here.

We usually define the LED longest pin as anode and the shorter one as cathode.

We use a wire to connect the LED anode to UNO board pin 11.And a 220Ω Resistor connects from LED cathode to breadboard cathode.And use another wire connects the resistor cathode to UNO board GND pin.

 

And then,we continue to put the push button on the breadboard.Use a wire connects a pin of button to breadboard anode while using a 10KΩ resistor connect another pin of button,and another wire from one of resistor pin to breadboard cathode.

            

A wire from the other resistor pin to UNO board pin 7.

At last,we use a long wire connect anode of breadboard to UNO board 5V pin.

Till now, we have finished all the connection of hardware.

Software

Open your Arduino IDE and type the code like below,

int ledpin=11;//define digital interface #11
int inpin=7;// define digital interface #7
int val;//define variable val
void setup()
{
pinMode(ledpin,OUTPUT);//define led interface as output
pinMode(inpin,INPUT);//define button interface as input
}
void loop()
{
val=digitalRead(inpin);//read digital interface #7 and assign the level value to val
if(val==LOW)//test if the button been pushed,if did then light on
digitalWrite(ledpin,LOW);
else
digitalWrite(ledpin,HIGH);
}

This code means we define the pin11 as LED digital interface,and pin 7 as button digital interface.And define led interface as output and button interface as input.We introduce a val,it can read digital interface #7 and assign the level value to val.If the button is pushed,then the val = LOW,and the light will on otherwise the light will off.

Next we upload this code to Arduino UNO board and power it,you can see the light on when you push the button.

Animated GIF - Find & Share on GIPHY

 

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *