Tutorial.DigitalReadSerial History

Hide minor edits - Show changes to markup

October 29, 2012, at 12:24 PM by Roberto Guido - corrected broken wired links to dev.arduino.cc
Changed lines 5-9 from:

This example shows you how to monitor the state of a switch by establishing serial communication between your Arduino and your computer over USB.

to:

This example shows you how to monitor the state of a switch by establishing serial communication between your Arduino and your computer over USB.

Changed lines 53-54 from:

The first thing you need to do in the main loop of your program is to establish a variable to hold the information coming in from your switch. Since the information coming in from the switch will be either a "1" or a "0", you can use an int datatype. Call this variable sensorValue, and set it to equal whatever is being read on digital pin 2. You can accomplish all this with just one line of code:

to:

The first thing you need to do in the main loop of your program is to establish a variable to hold the information coming in from your switch. Since the information coming in from the switch will be either a "1" or a "0", you can use an int datatype. Call this variable sensorValue, and set it to equal whatever is being read on digital pin 2. You can accomplish all this with just one line of code:

May 02, 2012, at 03:34 AM by Scott Fitzgerald -
Changed line 65 from:
to:
March 15, 2012, at 05:14 AM by Scott Fitzgerald -
Changed lines 57-60 from:

Once the Arduino has read the input, make it print this information back to the computer as a decimal (DEC) value. You can do this with the command Serial.println() in our last line of code:

Serial.println(sensorValue, DEC);

to:

Once the Arduino has read the input, make it print this information back to the computer as a decimal value. You can do this with the command Serial.println() in our last line of code:

Serial.println(sensorValue);

November 16, 2011, at 12:21 AM by Scott Fitzgerald -
Changed line 65 from:
to:
September 30, 2011, at 02:56 AM by Scott Fitzgerald -
Changed line 65 from:
to:
September 17, 2010, at 05:16 PM by Tom Igoe -
Changed lines 79-83 from:
  • BareMinimum - bare minimum code needed to start a sketch
  • Blink? - turn an LED on and off.
  • AnalogReadSerial - read a potentiometer, print the state out to the Serial Monitor
  • Fade - fade an LED with analog output
to:
September 07, 2010, at 08:56 PM by Tom Igoe -
Changed lines 53-54 from:

The first thing you need to do is to establish a variable to hold the information coming in from your switch. As the information coming in from the switch will be either a "1" or a "0", using an int datatype should do the trick. Call this variable sensorValue, and set it to equal whatever is being read on digital pin 2. You can accomplish all this with just one line of code:

to:

The first thing you need to do in the main loop of your program is to establish a variable to hold the information coming in from your switch. Since the information coming in from the switch will be either a "1" or a "0", you can use an int datatype. Call this variable sensorValue, and set it to equal whatever is being read on digital pin 2. You can accomplish all this with just one line of code:

Changed lines 57-58 from:

Finally, your Arduino needs to print this information back to the computer as a decimal (DEC) value. You can do this with the command Serial.println() in our last line of code:

to:

Once the Arduino has read the input, make it print this information back to the computer as a decimal (DEC) value. You can do this with the command Serial.println() in our last line of code:

Deleted lines 63-64:
Deleted line 69:

See Also:

September 07, 2010, at 08:54 PM by Tom Igoe -
Changed lines 51-52 from:

Now that your setup has been completed, move into the main loop of your code. When your button is pressed, 5 volts will freely flow through our circuit, and when it is not pressed, the circuit will be broken. This is a form of digital input, meaning that the switch can only be in either an ON state (seen by our Arduino as a "1") or an off state (seen by our Arduino as a "0"), with nothing in between.

to:

Now that your setup has been completed, move into the main loop of your code. When your button is pressed, 5 volts will freely flow through your circuit, and when it is not pressed, the input pin will be connected to ground through the 10-kilohm resistor. This is a digital input, meaning that the switch can only be in either an on state (seen by your Arduino as a "1", or HIGH) or an off state (seen by your Arduino as a "0", or LOW), with nothing in between.

September 07, 2010, at 08:51 PM by Tom Igoe -
Changed lines 32-33 from:

If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it will randomly return either HIGH or LOW. That's why you need a pull-down resistor in the circuit.

to:

If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it doesn't have a solid connection to voltage or ground, and it will randomly return either HIGH or LOW. That's why you need a pull-down resistor in the circuit.

September 07, 2010, at 08:51 PM by Tom Igoe -
Changed lines 30-31 from:

Pushbuttons or switches connect two points in a circuit when you press them. When the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to ground (through the pull-down resistor) and we read a LOW. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to 5 volts, so that we read a HIGH.

to:

Pushbuttons or switches connect two points in a circuit when you press them. When the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to ground (through the pull-down resistor) and reads as LOW, or 0. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to 5 volts, so that the pin reads as HIGH, or 1.

August 29, 2010, at 06:57 PM by Christian Cerrito -
Changed lines 51-52 from:

Now that your setup has been completed, move into the main loop of your code. We know that when your button is pressed, 5 volts will freely flow through our circuit, and when it is not pressed, the circuit will be broken. This is a form of digital input, meaning that the switch can only be in either an ON state (seen by our Arduino as a "1") or an off state (seen by our Arduino as a "0"), with nothing in between.

to:

Now that your setup has been completed, move into the main loop of your code. When your button is pressed, 5 volts will freely flow through our circuit, and when it is not pressed, the circuit will be broken. This is a form of digital input, meaning that the switch can only be in either an ON state (seen by our Arduino as a "1") or an off state (seen by our Arduino as a "0"), with nothing in between.

August 24, 2010, at 07:55 AM by Christian Cerrito -
Changed lines 80-81 from:
to:
August 24, 2010, at 07:54 AM by Christian Cerrito -
Changed lines 78-80 from:
to:
August 24, 2010, at 07:51 AM by Christian Cerrito -
Changed lines 82-85 from:
  • Blink?: turn an LED on and off.
  • AnalogReadSerial - Read a potentiometer, print the state out to the Serial Monitor
  • Fade - Fading an LED with analog output
to:
  • Blink? - turn an LED on and off.
  • AnalogReadSerial - read a potentiometer, print the state out to the Serial Monitor
  • Fade - fade an LED with analog output
August 24, 2010, at 07:20 AM by Christian Cerrito -
Changed lines 72-73 from:
to:

See Also:

August 21, 2010, at 06:37 AM by Christian Cerrito -
Deleted lines 27-28:

Pushbuttons or switches connect two points in a circuit when you press them. This example turns on the built-in LED on pin 13 when you press the button.

Changed lines 30-37 from:

When the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to ground (through the pull-down resistor) and we read a LOW. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to 5 volts, so that we read a HIGH.

You can also wire this circuit the opposite way, with a pullup resistor keeping the input HIGH, and going LOW when the button is pressed. If so, the behavior of the sketch will be reversed, with the LED normally on and turning off when you press the button.

If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it will randomly return either HIGH or LOW. That's why you need a pull-up or pull-down resistor in the circuit.

to:

Pushbuttons or switches connect two points in a circuit when you press them. When the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to ground (through the pull-down resistor) and we read a LOW. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to 5 volts, so that we read a HIGH.

If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it will randomly return either HIGH or LOW. That's why you need a pull-down resistor in the circuit.

August 21, 2010, at 06:27 AM by Christian Cerrito -
Added lines 28-39:

Pushbuttons or switches connect two points in a circuit when you press them. This example turns on the built-in LED on pin 13 when you press the button.

Connect three wires to the Arduino board. The first two, red and black, connect to the two long vertical rows on the side of the breadboard to provide access to the 5 volt supply and ground. The third wire goes from digital pin 2 to one leg of the pushbutton. That same leg of the button connects through a pull-down resistor (here 10 KOhms) to ground. The other leg of the button connects to the 5 volt supply.

When the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to ground (through the pull-down resistor) and we read a LOW. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to 5 volts, so that we read a HIGH.

You can also wire this circuit the opposite way, with a pullup resistor keeping the input HIGH, and going LOW when the button is pressed. If so, the behavior of the sketch will be reversed, with the LED normally on and turning off when you press the button.

If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it will randomly return either HIGH or LOW. That's why you need a pull-up or pull-down resistor in the circuit.

August 21, 2010, at 03:51 AM by Christian Cerrito -
Changed lines 41-42 from:

Next, we initialize digital pin 2, the pin that will read the output from our button, as an input:

to:

Next, initialize digital pin 2, the pin that will read the output from your button, as an input:

Changed lines 45-48 from:

Now that our setup has been completed, we move into the main loop of our code. We know that when our button is pressed, 5 volts will freely flow through our circuit, and when it is not pressed, the circuit will be broken. This is a form of digital input, meaning that the switch can only be in either an ON state (seen by our Arduino as a "1") or an off state (seen by our Arduino as a "0"), with nothing in between.

The first thing we need to do is to establish a variable to hold the information coming in from our switch. We know that the information coming in from the switch will be either a "1" or a "0", so using an int datatype should do the trick. We will call this variable sensorValue, and we know that we want it to equal whatever we are reading on digital pin 2. We can accomplish all this with just one line of code:

to:

Now that your setup has been completed, move into the main loop of your code. We know that when your button is pressed, 5 volts will freely flow through our circuit, and when it is not pressed, the circuit will be broken. This is a form of digital input, meaning that the switch can only be in either an ON state (seen by our Arduino as a "1") or an off state (seen by our Arduino as a "0"), with nothing in between.

The first thing you need to do is to establish a variable to hold the information coming in from your switch. As the information coming in from the switch will be either a "1" or a "0", using an int datatype should do the trick. Call this variable sensorValue, and set it to equal whatever is being read on digital pin 2. You can accomplish all this with just one line of code:

Changed lines 51-52 from:

Finally, we need our Arduino to print this information back to our computer as a decimal (DEC) value. We do this with the command Serial.println() in our last line of code:

to:

Finally, your Arduino needs to print this information back to the computer as a decimal (DEC) value. You can do this with the command Serial.println() in our last line of code:

August 21, 2010, at 03:44 AM by Christian Cerrito -
Changed lines 5-9 from:

This example allows you to monitor the state of a switch by establishing serial communication between your Arduino and your computer over USB.

to:

This example shows you how to monitor the state of a switch by establishing serial communication between your Arduino and your computer over USB.

August 21, 2010, at 03:14 AM by Christian Cerrito -
Changed lines 45-46 from:

Now that our setup has been completed, we move into the main loop of our code. We know that when our button is pressed, 5 volts will freely flow through our circuit, and when it is not pressed, the circuit will be broken. This is a form of digital input, meaning that the switch can only be in either an ON state (seen by our Arduino as a "1") or and off state (seen by our Arduino as a "0"), with nothing in between.

to:

Now that our setup has been completed, we move into the main loop of our code. We know that when our button is pressed, 5 volts will freely flow through our circuit, and when it is not pressed, the circuit will be broken. This is a form of digital input, meaning that the switch can only be in either an ON state (seen by our Arduino as a "1") or an off state (seen by our Arduino as a "0"), with nothing in between.

August 21, 2010, at 03:14 AM by Christian Cerrito -
Changed lines 37-38 from:

In the program below, the very first thing that you do in the setup function is to establish serial communications, at 9600 bits of data per second, between your Arduino and your computer with the line:

to:

In the program below, the very first thing that you do will in the setup function is to begin serial communications, at 9600 bits of data per second, between your Arduino and your computer with the line:

Changed lines 41-42 from:

Next, we initialize digital pin 2, the pin that will read the output from our button, as an input with the line:

to:

Next, we initialize digital pin 2, the pin that will read the output from our button, as an input:

Changed lines 55-58 from:

Now, when you open your Serial Monitor in our Arduino environment, you will see a stream of "0"s if your switch is open, or "1"s if your switch is closed.

to:

Now, when you open your Serial Monitor in the Arduino environment, you will see a stream of "0"s if your switch is open, or "1"s if your switch is closed.

August 21, 2010, at 03:09 AM by Christian Cerrito -
Changed lines 55-56 from:
to:

Now, when you open your Serial Monitor in our Arduino environment, you will see a stream of "0"s if your switch is open, or "1"s if your switch is closed.

August 21, 2010, at 03:04 AM by Christian Cerrito -
Changed lines 47-48 from:

The first thing we need to do is to establish a variable to hold the information coming in from our switch. We know that the information coming in from the switch will be either a "1" or a "0", so using intdatatype should do the trick. We will call this variable sensorValue, and we know that we want it to equal whatever we are reading on digital pin 2. We can accomplish all this with just one line of code:

to:

The first thing we need to do is to establish a variable to hold the information coming in from our switch. We know that the information coming in from the switch will be either a "1" or a "0", so using an int datatype should do the trick. We will call this variable sensorValue, and we know that we want it to equal whatever we are reading on digital pin 2. We can accomplish all this with just one line of code:

August 21, 2010, at 03:03 AM by Christian Cerrito -
Changed lines 47-48 from:

The first thing we need to do is to establish a variable to hold the information coming in from our switch. We know that the information coming in from the switch will be either a "1" or a "0", so using int datatype should do the trick. We will call this variable sensorValue, and we know that we want it to equal whatever we are reading on digital pin 2. We can accomplish all this with just one line of code:

to:

The first thing we need to do is to establish a variable to hold the information coming in from our switch. We know that the information coming in from the switch will be either a "1" or a "0", so using intdatatype should do the trick. We will call this variable sensorValue, and we know that we want it to equal whatever we are reading on digital pin 2. We can accomplish all this with just one line of code:

August 21, 2010, at 03:02 AM by Christian Cerrito -
August 21, 2010, at 03:01 AM by Christian Cerrito -
Changed lines 51-52 from:

Finally, we need our Arduino to print this information back to our computer as a decimal(DEC) value. We do this with the command Serial.println() in our last line of code:

to:

Finally, we need our Arduino to print this information back to our computer as a decimal (DEC) value. We do this with the command Serial.println() in our last line of code:

August 21, 2010, at 03:01 AM by Christian Cerrito -
Changed lines 47-48 from:

The first thing we need to do is to establish a variable to hold the information coming in from our switch. We know that the information coming in from the switch will be either a "1" or a "0", so using int datatype should do the trick. We will call this variable sensorValue, and we know that we want it to equal whatever we are reading on digital pin 2. We can accomplish all this with just one line of code:

to:

The first thing we need to do is to establish a variable to hold the information coming in from our switch. We know that the information coming in from the switch will be either a "1" or a "0", so using int datatype should do the trick. We will call this variable sensorValue, and we know that we want it to equal whatever we are reading on digital pin 2. We can accomplish all this with just one line of code:

August 21, 2010, at 03:00 AM by Christian Cerrito -
Changed lines 51-52 from:

Finally, we need our Arduino to print this information back to our computer as a decimal value. We do this with the command Serial.println( DEC) in our last line of code:

to:

Finally, we need our Arduino to print this information back to our computer as a decimal(DEC) value. We do this with the command Serial.println() in our last line of code:

August 21, 2010, at 02:59 AM by Christian Cerrito -
Changed lines 51-52 from:

Finally, we need our Arduino to print this information back to our computer. We do this asking it to * Serial.println() this information to its serial port, as a decimal value, in our last line of code:

to:

Finally, we need our Arduino to print this information back to our computer as a decimal value. We do this with the command Serial.println( DEC) in our last line of code:

August 21, 2010, at 02:58 AM by Christian Cerrito -
Changed lines 51-52 from:

Finally, we need our Arduino to relay this information back to our computer. We do this asking it to "print" this information to its serial port, as [[Serial.print| @@ a decimal ]] a in our last line of code:

to:

Finally, we need our Arduino to print this information back to our computer. We do this asking it to * Serial.println() this information to its serial port, as a decimal value, in our last line of code:

August 21, 2010, at 02:54 AM by Christian Cerrito -
Changed lines 47-51 from:

The first thing we need to do is to establish a variable to hold the information coming in from our switch. We know that the information coming in from the switch will be either a "1" or a "0", so an int datatype

@@

to:

The first thing we need to do is to establish a variable to hold the information coming in from our switch. We know that the information coming in from the switch will be either a "1" or a "0", so using int datatype should do the trick. We will call this variable sensorValue, and we know that we want it to equal whatever we are reading on digital pin 2. We can accomplish all this with just one line of code:

int sensorValue = digitalRead(2);

Finally, we need our Arduino to relay this information back to our computer. We do this asking it to "print" this information to its serial port, as [[Serial.print| @@ a decimal ]] a in our last line of code:

Serial.println(sensorValue, DEC);

August 21, 2010, at 02:40 AM by Christian Cerrito -
Added lines 36-51:

In the program below, the very first thing that you do in the setup function is to establish serial communications, at 9600 bits of data per second, between your Arduino and your computer with the line:

Serial.begin(9600);

Next, we initialize digital pin 2, the pin that will read the output from our button, as an input with the line:

pinMode(2,INPUT);

Now that our setup has been completed, we move into the main loop of our code. We know that when our button is pressed, 5 volts will freely flow through our circuit, and when it is not pressed, the circuit will be broken. This is a form of digital input, meaning that the switch can only be in either an ON state (seen by our Arduino as a "1") or and off state (seen by our Arduino as a "0"), with nothing in between.

The first thing we need to do is to establish a variable to hold the information coming in from our switch. We know that the information coming in from the switch will be either a "1" or a "0", so an int datatype

@@

August 20, 2010, at 11:04 PM by Christian Cerrito -
Changed lines 5-9 from:

This example allows you to monitor the state of a switch connected to your Arduino by establishing serial communication with your computer over USB.

to:

This example allows you to monitor the state of a switch by establishing serial communication between your Arduino and your computer over USB.

August 20, 2010, at 11:02 PM by Christian Cerrito -
Changed lines 5-9 from:

This example allows you to monitor the state of a switch connected to your Arduino via serial communication with your computer over USB.

to:

This example allows you to monitor the state of a switch connected to your Arduino by establishing serial communication with your computer over USB.

August 20, 2010, at 11:01 PM by Christian Cerrito -
Changed lines 5-9 from:

This example allows you to monitor the state of a switch connected to your Arduino via serial communication with computer over USB.

to:

This example allows you to monitor the state of a switch connected to your Arduino via serial communication with your computer over USB.

August 20, 2010, at 11:01 PM by Christian Cerrito -
Changed lines 5-9 from:

This example turns on a LED on pin 13 when your button or switch is turned "on", completing the shown circuit. Additionally, you will be able to monitor the state of your switch on your computer via serial communication with your Arduino over USB.

to:

This example allows you to monitor the state of a switch connected to your Arduino via serial communication with computer over USB.

August 20, 2010, at 10:39 PM by Christian Cerrito -
Changed line 35 from:

Describe what's going on here

to:

Code

August 20, 2010, at 10:37 PM by Christian Cerrito -
Changed line 32 from:
to:
August 20, 2010, at 10:36 PM by Christian Cerrito -
Changed lines 24-25 from:
to:
August 18, 2010, at 12:07 AM by Christian Cerrito -
Changed lines 22-23 from:
to:
August 18, 2010, at 12:06 AM by Christian Cerrito -
Changed lines 23-24 from:
to:
August 18, 2010, at 12:05 AM by Christian Cerrito -
August 18, 2010, at 12:02 AM by Christian Cerrito -
Changed lines 5-9 from:

This example turns on a LED on pin 13 when your button or switch completes the shown circuit. Additionally, you will be able to monitor the state of your circuit on your computer via serial communication with your Arduino over USB.

to:

This example turns on a LED on pin 13 when your button or switch is turned "on", completing the shown circuit. Additionally, you will be able to monitor the state of your switch on your computer via serial communication with your Arduino over USB.

August 17, 2010, at 11:51 PM by Christian Cerrito -
Changed lines 5-9 from:

This example turns on a LED on pin 13 when your button or switch completes the shown circuit. Additionally, you will be able to monitor the state of your circuit on your computer via http://dev.arduino.cc/wiki/uno/Reference/Serial? with your Arduino over USB.

to:

This example turns on a LED on pin 13 when your button or switch completes the shown circuit. Additionally, you will be able to monitor the state of your circuit on your computer via serial communication with your Arduino over USB.

August 17, 2010, at 11:46 PM by Christian Cerrito -
Changed lines 5-9 from:

This example turns on a LED on pin 13 when your button or switch completes the shown circuit. Additionally, you will be able to monitor the state of your circuit on your computer via Reference/Serial? with your Arduino over USB.

to:

This example turns on a LED on pin 13 when your button or switch completes the shown circuit. Additionally, you will be able to monitor the state of your circuit on your computer via http://dev.arduino.cc/wiki/uno/Reference/Serial? with your Arduino over USB.

August 17, 2010, at 11:40 PM by Christian Cerrito -
Changed lines 5-9 from:

This example turns on a LED on pin 13 when your button or switch completes the shown circuit. Additionally, you will be able to monitor the state of your circuit on your computer via [[serial communication | Reference/Serial with your Arduino over USB.

to:

This example turns on a LED on pin 13 when your button or switch completes the shown circuit. Additionally, you will be able to monitor the state of your circuit on your computer via Reference/Serial? with your Arduino over USB.

August 17, 2010, at 11:40 PM by Christian Cerrito -
Changed lines 5-9 from:

This example turns on a LED on pin 13 when your button or switch completes the shown circuit. Additionally, you will be able to monitor the state of your circuit on your computer via [[serial communication | with your Arduino over USB.

to:

This example turns on a LED on pin 13 when your button or switch completes the shown circuit. Additionally, you will be able to monitor the state of your circuit on your computer via [[serial communication | Reference/Serial with your Arduino over USB.

August 17, 2010, at 11:35 PM by Christian Cerrito -
Changed lines 5-14 from:

Pushbuttons or switches connect two points in a circuit when you press them. This example turns on the built-in LED on pin 13 when you press the button.

Connect three wires to the Arduino board. The first two, red and black, connect to the two long vertical rows on the side of the breadboard to provide access to the 5 volt supply and ground. The third wire goes from digital pin 2 to one leg of the pushbutton. That same leg of the button connects through a pull-down resistor (here 10 KOhms) to ground. The other leg of the button connects to the 5 volt supply.

When the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to ground (through the pull-down resistor) and we read a LOW. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to 5 volts, so that we read a HIGH.

You can also wire this circuit the opposite way, with a pullup resistor keeping the input HIGH, and going LOW when the button is pressed. If so, the behavior of the sketch will be reversed, with the LED normally on and turning off when you press the button.

If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it will randomly return either HIGH or LOW. That's why you need a pull-up or pull-down resistor in the circuit.

to:

This example turns on a LED on pin 13 when your button or switch completes the shown circuit. Additionally, you will be able to monitor the state of your circuit on your computer via [[serial communication | with your Arduino over USB.

August 17, 2010, at 11:21 PM by Christian Cerrito -
Changed lines 5-6 from:

This example

to:

Pushbuttons or switches connect two points in a circuit when you press them. This example turns on the built-in LED on pin 13 when you press the button.

Connect three wires to the Arduino board. The first two, red and black, connect to the two long vertical rows on the side of the breadboard to provide access to the 5 volt supply and ground. The third wire goes from digital pin 2 to one leg of the pushbutton. That same leg of the button connects through a pull-down resistor (here 10 KOhms) to ground. The other leg of the button connects to the 5 volt supply.

When the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to ground (through the pull-down resistor) and we read a LOW. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to 5 volts, so that we read a HIGH.

You can also wire this circuit the opposite way, with a pullup resistor keeping the input HIGH, and going LOW when the button is pressed. If so, the behavior of the sketch will be reversed, with the LED normally on and turning off when you press the button.

If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is because the input is "floating" - that is, it will randomly return either HIGH or LOW. That's why you need a pull-up or pull-down resistor in the circuit.

August 17, 2010, at 11:20 PM by Christian Cerrito -
Changed lines 11-12 from:
  • 10 K ohm resistor
to:
  • 10k ohm resistor
  • breadboard
  • hook-up wire
August 17, 2010, at 11:10 PM by Christian Cerrito -
Changed line 10 from:
  • A momentary swtich, button, or toggle switch
to:
  • A momentary switch, button, or toggle switch
August 17, 2010, at 11:10 PM by Christian Cerrito -
Added lines 10-12:
  • A momentary swtich, button, or toggle switch
  • 10 K ohm resistor
August 17, 2010, at 10:57 PM by Christian Cerrito -
Changed lines 3-6 from:

Example Name

Description

to:

Digital Read Serial

This example

August 13, 2010, at 10:23 PM by Tom Igoe -
Added lines 1-35:

Share