Arduino Lesson 6. Digital Inputs - Adafruit Learning System
Maybe your like
- Overview
- Parts
- Breadboard Layout
- Arduino Code
- Push Switches
- Other Things to Do
- Single page
- Feedback? Corrections?
- Text View
- Learn Arduino
-
Tactile Button switch (6mm) x 20 pack $2.50 Add to Cart -
USB Cable - Standard A-B $3.95 Add to Cart -
Diffused Red 5mm LED (25 pack) $4.00 Add to Cart -
Premium Male/Male Jumper Wires - 40 x 6" (150mm) $3.95 Add to Cart -
Half Sized Premium Breadboard - 400 Tie Points $4.95 Add to Cart -
Adafruit METRO 328 Fully Assembled - Arduino IDE compatible Out of Stock
Arduino Code
Load the following sketch onto your Arduino board. Pressing the top button will turn the LED on, pressing the bottom button will turn it off again.
Download File Copy Code /* Adafruit Arduino - Lesson 6. Inputs */ int ledPin = 5; int buttonApin = 9; int buttonBpin = 8; byte leds = 0; void setup() { pinMode(ledPin, OUTPUT); pinMode(buttonApin, INPUT_PULLUP); pinMode(buttonBpin, INPUT_PULLUP); } void loop() { if (digitalRead(buttonApin) == LOW) { digitalWrite(ledPin, HIGH); } if (digitalRead(buttonBpin) == LOW) { digitalWrite(ledPin, LOW); } } /* Adafruit Arduino - Lesson 6. Inputs */ int ledPin = 5; int buttonApin = 9; int buttonBpin = 8; byte leds = 0; void setup() { pinMode(ledPin, OUTPUT); pinMode(buttonApin, INPUT_PULLUP); pinMode(buttonBpin, INPUT_PULLUP); } void loop() { if (digitalRead(buttonApin) == LOW) { digitalWrite(ledPin, HIGH); } if (digitalRead(buttonBpin) == LOW) { digitalWrite(ledPin, LOW); } }The first part of the sketch defines three variable for the three pins that are to be used. The 'ledPin' is the output pin and 'buttonApin' will refer to the switch nearer the top of the breadboard and 'buttonBpin' to the other switch.
The 'setup' function defines the ledPin as being an OUTPUT as normal, but now we have the two inputs to deal with. In this case, we use the set the pinMode to be 'INPUT_PULLUP' like this:
Download File Copy Code pinMode(buttonApin, INPUT_PULLUP); pinMode(buttonBpin, INPUT_PULLUP); pinMode(buttonApin, INPUT_PULLUP); pinMode(buttonBpin, INPUT_PULLUP);The pin mode of INPUT_PULLUP means that the pin is to be used as an input, but that if nothing else is connected to the input it should be 'pulled up' to HIGH. In other words, the default value for the input is HIGH, unless it is pulled LOW by the action of pressing the button.
This is why the switches are connected to GND. When a switch is pressed, it connects the input pin to GND, so that it is no longer HIGH.
Since the input is normally HIGH and only goes LOW, when the button is pressed, the logic is a little up-side-down. We will handle this in the 'loop' function.
Download File Copy Code void loop() { if (digitalRead(buttonApin) == LOW) { digitalWrite(ledPin, HIGH); } if (digitalRead(buttonBpin) == LOW) { digitalWrite(ledPin, LOW); } } void loop() { if (digitalRead(buttonApin) == LOW) { digitalWrite(ledPin, HIGH); } if (digitalRead(buttonBpin) == LOW) { digitalWrite(ledPin, LOW); } }In the 'loop' function there are two 'if' statements. One for each button. Each does an 'digitalRead' on the appropriate input.
Remember that if the button is pressed, the corresponding input will be LOW, if button A is low, then a 'digitalWrite' on the ledPin turns it on.
Similarly, if button B is pressed, a LOW is written to the ledPin.
Page last edited March 08, 2024
Text editor powered by tinymce.
Breadboard Layout Push Switches Related Guides Adafruit Ultimate GPS Logger Shield By lady ada intermediate Introducing Adafruit Trellis By lady ada beginner Arduino Lesson 7. Make an RGB LED Fader By Simon Monk beginner Adafruit Proto Screw Shield By lady ada beginner Arduino Lesson 3. RGB LEDs By Simon Monk beginner Adafruit PCA9685 16-Channel Servo Driver By Bill Earl beginner WiFi Weather Station By M. Schwartz intermediate Adafruit Music Maker Shield By lady ada intermediate 36mm LED Pixels By Phillip Burgess beginner Making Adabot: Part 2 By Rick Winscot beginner Animating Multiple LED Backpacks By Phillip Burgess beginner Ladyada's Learn Arduino - Lesson #0 By lady ada beginner A REST API for Arduino & the CC3000 WiFi Chip By M. Schwartz beginner 8BitBox By Mark Rudolph beginner Your browser does not support the video tag. This links to the guide 1,500 NeoPixel LED Curtain with Raspberry Pi and Fadecandy. 1,500 NeoPixel LED Curtain with Raspberry Pi and... By Phillip Burgess intermediateCreate Wishlist
× Title Description Close Search SearchCategories
Tag » Arduino If Statement Digital Read
-
DigitalRead And If Statements - Arduino Forum
-
If Statement For Checking DigitalRead Not Executing - Arduino Forum
-
DigitalRead() - Arduino Reference
-
Is "if(digitalRead(2) == HIGH)" An Unreliable Way Of Going About It?
-
Reading A Button - - Arduino Modular Electronics Project - Circuitar
-
Arduino Digital Input & If Statement
-
If/else On DigitalRead Not Executing On 'else' Portion
-
DigitalRead() | Référence Du Langage Arduino En Français
-
Arduino IF Statement Code Examples
-
Why Is My If Statement In My Program Not Working? - Stack Overflow
-
Unit 12 – The Button And The If-Statement - Tutorials For Arduino
-
Arduino Lesson 4 - If Statements - YouTube
-
Digital Read In Arduino - Tutorialspoint
Adafruit Ultimate GPS Logger Shield By
Introducing Adafruit Trellis By
Arduino Lesson 7. Make an RGB LED Fader By
Adafruit Proto Screw Shield By
Arduino Lesson 3. RGB LEDs By
Adafruit PCA9685 16-Channel Servo Driver By
WiFi Weather Station By
Adafruit Music Maker Shield By
Making Adabot: Part 2 By
Animating Multiple LED Backpacks By
Ladyada's Learn Arduino - Lesson #0 By
A REST API for Arduino & the CC3000 WiFi Chip By
8BitBox By