Circuit
The conclusion is, the above circuit sends a LOW signal to pin3, when button is unpressed and a HIGH signal when the button is pressed.
About digitalRead() function: Remember that the digitalWrite function was used to send a HIGH or LOW signal to a digital pin. The digitalRead() function is its opposite. It receives(reads) whether the signal coming on a digital pin is HIGH or LOW.
Code:
int buttonState = 0;
int myLED = 13;
int button = 2;
void setup()
{
pinMode(button, INPUT);
pinMode(myLED, OUTPUT);
}
void loop()
{
// read the state of the pushbutton value
buttonState = digitalRead(button);
// check if pushbutton is pressed. if it is, the
// buttonState is HIGH
if (buttonState == HIGH) {
// turn LED on
digitalWrite(myLED, HIGH);
} else {
// turn LED off
digitalWrite(myLED, LOW);
}
delay(10); // Delay a little bit to improve simulation performance
}