Unexpectedly unknown? INPUT PULLUP, The INPUT PULLUP keyword may now be used as the second argument of the pinMode function in Arduino IDE 1.0.1. Although this function is useful, I'll reiterate my introduction because I believe it may not be widely recognized.
/*
Button
Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 2.
The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
* Note: on most Arduinos there is already an LED on the board
attached to pin 13.
created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
It will resemble the diagram below if you connect it in this manner.
I choose to remove the wiring for the LED and utilize the one on the Arduino Uno instead. Creating a circuit schematic.
equivalent to this When the button is not pressed, a pull-down resistor connects the button read pin to GND. At this point, the button pin will be low if you read it. When the button is pressed, it connects to +5V, and when it is read, it goes HIGH. This sketch sets the button read pin using pinMode(buttonPin, INPUT); and the INPUT keyword.
Now let's replace this INPUT keyword with the INPUT PULLUP term. The pull-up resistor inside the microcontroller chip is enabled when this keyword is used to bring the pin into INPUT mode. Since the reverse pull-up is enabled in the current circuit, I changed the wiring (circuit) and sketch to reverse the relationship between when the button is depressed and when it is not depressed. In the previous circuit, the pull-down side had a resistor. I must make it right.
With one fewer resistor and one jumper wire than the previous one, it is much simpler.
The diagram is
This is what takes place. For comparison, the internal pull-up resistor, which is not depicted in a typical circuit schematic, is also displayed. On the data sheet, the resistance value appears to range from 20 K lowest to 50 K maximum, hence 33 K is shown with a gap.
Create the circuit as shown above, changing the read button's setting to pinMode(buttonPin, INPUT PULLUP);. The button pin will thus read as HIGH when the button is not pressed and LOW when the button is pressed. The portion that utilises the read value must also be modified because it is read with the opposite value from the beginning.
Let's switch this test to the opposite if (buttonState == LOW) if (buttonState == HIGH).
const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Inputモードでプルアップ抵抗を有効に
}
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) { // ボタンが押されていたら、ピンの値はLOW
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}
The functionality of this is identical to that of the original sketch or circuit, however it is challenging to comprehend the processing when the button state changes. Prepared, let's now compare the constants buttonON and buttonOFF.
const int buttonON = LOW; // ボタンが押されているとピンの値はLOW
const int buttonOFF = HIGH; // ボタンが押されていないとピンの値はHIGH
const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == buttonON) { // ボタンが押されていたら
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}
Does it now make sense more easily?
This time, I experimented with using the INPUT PULLUP keyword to activate the Arduino AVR microcomputer chip's internal pull-up resistor function. Take advantage of the fact that you can reduce wiring and parts if you know this.
Source - SwitchScience