Introduction
I/O port means interface for INPUT and OUTPUT. Up until now, we have only used its OUTPUT function.
In this experiment, we will try to use the input function, which is to read the output value of device connecting to it. We use 1 button and 1 LED using both input and output to give you a better understanding of the I/O function.
Button switches, familiar to most of us, are a switch value (digital value) component. When it's pressed,the circuit is in closed (conducting) state.
Hardware Required
- Arduino board *1
- Button switch*1
- Red M5 LED*1
- 220Ω resistor*1
- 10KΩ resistor*1
- Breadboard*1
- Breadboard jumper wire *6
Video Tutorial
Connection
Connection for arduino uno REV4:
Connection for arduino mege 2560 R3:
Sample Code
Now, let's begin the compiling. When the button is pressed, the LED will be on. Based on the previous study, the coding may be easy for you. In this program, we add a statement of judgment. Here, we use an if() statement.
Arduino IDE is based on C language, so statements of C language such as while, switch, etc. can certainly be used for Arduino program.
When we press the button, pin 7 will output high level. We can program pin 11 to output high level and turn on the LED. When pin 7 outputs low level, pin 11 also outputs low level and the LED remains off.
int ledpin=11;// initialize pin 11 int inpin=7;// initialize pin 7 int val;// define val void setup() { pinMode(ledpin,OUTPUT);// set LED pin as “output” pinMode(inpin,INPUT);// set button pin as “input” } void loop() { val=digitalRead(inpin);// read the level value of pin 7 and assign if to val if(val==LOW)// check if the button is pressed, if yes, turn on the LED { digitalWrite(ledpin,LOW);} else { digitalWrite(ledpin,HIGH);} }
Result
When the button is pressed, LED is on, otherwise, LED remains off. So the button controlled LED experiment is completed.
The simple principle of this experiment is widely used in a variety of circuit and electric appliances. You can easily come across it in your everyday life. One typical example is when you press a certain key of your phone, the backlight will be on.
All Tutorial
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 1: Hello World
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project Project 2: LED Blinking
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 3: PWM
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 4: Traffic Light
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 5: LED Chasing Effect
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 6: Button-controlled LED
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 7: Active Buzzer
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 8: Passive Buzzer
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 9: RGB LED
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 10: Photo Resistor
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 11: Flame Sensor
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 12: LM35 Temperature Sensor
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 13: Tilt Switch
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 14: IR Remote Control
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 15: Analog Value Reading
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 16: 74HC595 driving LEDs
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 17: 1-digit LED Segment Display
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 18: 4-digit LED Segment Display
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 19: 8*8 LED Matrix
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 20: 1602 LCD
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 21: Servo Control
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 22: 5V Stepper Motor
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 23: PIR Motion Sensor
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 24: Analog Gas Sensor
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 25: ADXL345 Three Axis Acceleration Module
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 26: HC-SR04 Ultrasonic Sensor
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 27: Joystick Module
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 28: 5V Relay Module
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 29: DS3231 Clock Module
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 30: DHT11 Temperature and Humidity Sensor
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 31: Soil Humidity Sensor
- Arduino UNO R3/MEGA 2560 R3 Starter Kit Project 32: RC522 RFID Module
Buy