This tutorial will explain how to connect a switch to arduino board and read its state.
Components Required
- Arduino Board
- Switch
- Wires
- BreadBoard
- 10 ohm Resistor
Connection Preview
PROCEDURE
- Connect a ground of an arduino to negative part rail of the breadboard.
- Connect 5v pin to the positive part rail of the breadboard.
- Connect Digital pin 2 to one of the legs of the switch.
- On the same leg, connect 10 ohm resistor to ground.
- Connect the other leg of the switch to 5v(positive part of breadboard).
- Open Arduino Editor(If you have not done it).
- On the setup function type:
Serial.begin(9600)
- On the same function initialize digital pin 2, the pin that will read the output from your button, as an input:
pinMode(2,INPUT);
- Initialize the variable to Read the value.
int sensorValue = digitalRead(2);
- Print the value to the Serial
Serial.println(sensorValue);
Full Source Code
|
|