Arduino Lesson 6. Digital Inputs

Similar documents
Adafruit IO Basics: Digital Output

Adafruit IO Basics: Digital Input

Circuit Playground Digital Input

Adafruit IO Basics: Analog Input

Interior Purse Light. Created by Becky Stern. Last updated on :41:08 PM UTC

Adafruit Color Sensors

Adafruit IO Basics: Color

Adafruit IO Basics: Servo

MLX90393 Wide-Range 3-Axis Magnetometer

Adafruit IO Basics: Temperature & Humidity

Adafruit DRV2605 Haptic Controller Breakout

Adafruit Capacitive Touch Sensor Breakouts

Adafruit LED Sequins. Created by Becky Stern. Last updated on :00:06 PM EST

Using IFTTT with Adafruit IO to Make an IoT Door Detector

Interactive Gift Box. Created by codingpro. Last updated on :47:40 AM UTC

Magical Mistletoe. Created by Leslie Birch. Last updated on :45:29 PM UTC

Adafruit LED Sequins. Created by Becky Stern. Last updated on :02:00 AM UTC

Adafruit MCP9808 Precision I2C Temperature Sensor Guide

Adafruit TPL5111 Reset Enable Timer Breakout

NeoPixie Dust Bag with Circuit Playground Express

Adafruit MPRLS Ported Pressure Sensor Breakout

i2c/spi LCD Backpack Created by lady ada Last updated on :11:04 PM UTC

Naughty or Nice Machine

Monochrome OLED Breakouts

Adafruit CCS811 Air Quality Sensor

Adafruit VL53L0X Time of Flight Micro-LIDAR Distance Sensor Breakout

Adafruit IO Basics: ESP Arduino

Adafruit I2C FRAM Breakout

Adafruit DRV2605 Haptic Controller Breakout

Pinball Controller for ipad

Adafruit TPL5110 Power Timer Breakout

Adafruit Si5351 Clock Generator Breakout

Tent Lantern. Created by Timothy Reese. Last updated on :17:25 AM UTC

Adafruit 1.27" and 1.5" Color OLED Breakout Board

Adafruit APDS9960 breakout

Adafruit PowerBoost 500 Shield

Coffee Detonator: The TNT Plunger Grinder

Adafruit AS channel Visible Light Sensor

Adafruit GPIO Expander Bonnet for Raspberry Pi Created by Kattni Rembor. Last updated on :12:47 PM UTC

Adafruit MMA8451 Accelerometer Breakout

Adafruit Si7021 Temperature + Humidity Sensor

Adafruit AMG8833 8x8 Thermal Camera Sensor

Mad Science Test Tube Rack

Chirping Plush Owl Toy

TSL2561 Luminosity Sensor

Adafruit ATWINC1500 WiFi Breakout

Adafruit MMA8451 Accelerometer Breakout

Adafruit 8x16 LED Matrix FeatherWing

Adafruit Mini TFT with Joystick Featherwing

MCP Bit DAC Tutorial

Adafruit MMA8451 Accelerometer Breakout

Audio Prank Gift Box. Created by Becky Stern. Last updated on :46:15 PM UTC

1.8" TFT Display Breakout and Shield

Adafruit PCF8523 Real Time Clock

Neon LED Signs. Created by John Park. Last updated on :11:09 PM UTC

Adafruit AM2320 Sensor

Toy Car Speed Timer. Created by Kirby Griese. Last updated on :13:49 PM UTC

Trinket-Powered Conference Room Occupancy Display

MCP Bit DAC Tutorial

Adafruit Mini TFT " 160x80

Light-Up Angler Fish Embroidery

Circuit Playground Express Laser Tag

3D Printed Camera LED Ring

Adafruit Pi Cobbler Kit

Adafruit Feather 32u4 Basic Proto

Alohamora Bottle. Created by Erin St Blaine. Last updated on :58:53 PM UTC

Joy Featherwing. Created by Dean Miller. Last updated on :03:07 PM UTC

Adafruit DS3231 Precision RTC Breakout

FLORA and GEMMA ICSP. Created by Becky Stern. Last updated on :42:16 PM UTC

Large Pi-based Thermometer and Clock

Mystery Box: NeoMatrix Mk I

LED Stego Flex Spike Hoodie

FLORA TV-B-Gone. Created by Becky Stern. Last updated on :32:57 PM UTC

Trellis 3D Printed Enclosure

Introducing Adafruit Trellis

Adafruit SGP30 TVOC/eCO2 Gas Sensor

Adafruit ATWINC1500 WiFi Breakout

3d Printed Neopixel Tactile Switch Buttons

Flora Wearable GPS. Created by Becky Stern. Last updated on :32:36 PM UTC

IS31FL x9 Charlieplexed PWM LED Driver

Adabot Operation Game

Adafruit 8x16 LED Matrix FeatherWing

Adafruit ATWINC1500 WiFi Breakout

RGB LCD Shield. Created by lady ada. Last updated on :48:40 PM UTC

Adafruit IO Basics: Feeds

Flora Brake Light Backpack

MP3 Feather - Gordon Cole

Crickit Dancing Marionette Kit Created by Dano Wall. Last updated on :03:11 PM UTC

Celebration Spectacles

Adafruit WINC1500 WiFi Shield for Arduino

Mystery Box: Haunted Radio

NeoPixel Ring Bangle Bracelet

Sino:bit with Arduino

LED Lightbox. Created by Sam Clippinger. Last updated on :50:00 AM UTC

Guardian Shield+ Zelda Breath of the Wild

Mystery Box: Crypto Countdown Case

DIY Circuit Playground Shields

Adafruit eink Display Breakouts

0.96" mini Color OLED

OLED 2864 Display module (SKU:TOY0007)

Transcription:

Arduino Lesson 6. Digital Inputs Created by Simon Monk Last updated on 2018-02-27 10:20:04 PM UTC

Guide Contents Guide Contents Overview Parts Part Qty Breadboard Layout Arduino Code Push Switches Other Things to Do 2 3 4 4 4 5 6 8 9 Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-6-digital-inputs Page 2 of 9

Overview In this lesson, you will learn to use push buttons with digital inputs to turn an LED on and off. Pressing the button nearer the top of the breadboard will turn the LED on, pressing the other button will turn the LED off. Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-6-digital-inputs Page 3 of 9

Parts To complete this lesson, you will need the following parts. Part Qty 5mm Red LED 1 270 Ω Resistor (red, purple, brown stripes) 1 Tactile push switch 2 Half-size Breadboard 1 Arduino Uno R3 1 Jumper wire pack 1 Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-6-digital-inputs Page 4 of 9

Breadboard Layout Although the bodies of the switches are square, the pins protrude from opposite sides of the switch. This means that the pins will only be far enough apart when they are the correct way around on the breadboard. Remember that the LED has to be the correct way around with the shorter negative lead to the right. Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-6-digital-inputs Page 5 of 9

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. /* 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: 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. Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-6-digital-inputs Page 6 of 9

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. Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-6-digital-inputs Page 7 of 9

Push Switches Switches are really simple components. When you press a button or flip a lever, they connect two contacts together so that electricity can flow through them. The little tactile switches that are used in this lesson have four connections, which can be a little confusing. Actually, there are only really two electrical connections, as inside the switch package pins B and C are connected together, as are A and D. Adafruit Industries https://learn.adafruit.com/adafruit-arduino-lesson-6-digital-inputs Page 8 of 9

Other Things to Do There are a couple of things we could try with this hardware. Firstly, you could try taking what you learnt in lesson 5 and adding some commands to the sketch that print something to the Serial Monitor whenever either switch is pressed. Remember that as well as printing out a message using something like this in your 'loop' function: Serial.println( Button A Pressed ); You will also need to start serial communication in the 'setup' function by doing this: while (!Serial); Serial.begin(9600); A second modification that you could make would be to make the buttons do something different. So, for example you could change the sketch so that if button A is pressed, the LED turns on, but then turns off again after 30 seconds. Hint: Think of this as being a very slow blink. About the Author Click Here for the Next Lesson https://adafru.it/auv Simon Monk is author of a number of books relating to Open Source Hardware. The following books written by Simon are available from Adafruit: Programming Arduino, 30 Arduino Projects for the Evil Genius and Programming the Raspberry Pi. Adafruit Industries Last Updated: 2018-02-27 10:20:04 PM UTC Page 9 of 9