Circuit Playground Digital Input

Similar documents
Arduino Lesson 6. Digital Inputs

Adafruit MCP9808 Precision I2C Temperature Sensor Guide

NeoPixie Dust Bag with Circuit Playground Express

Adafruit Color Sensors

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

Adafruit IO Basics: Digital Input

TSL2561 Luminosity Sensor

Adafruit VL53L0X Time of Flight Micro-LIDAR Distance Sensor Breakout

Coffee Detonator: The TNT Plunger Grinder

Adafruit APDS9960 breakout

Adafruit TPL5111 Reset Enable Timer Breakout

Chirping Plush Owl Toy

Adafruit TPL5110 Power Timer Breakout

Adafruit Si7021 Temperature + Humidity Sensor

Monochrome OLED Breakouts

Adafruit IO Basics: Digital Output

Android GBoard Morse Code Control with Circuit Playground Express

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

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

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

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

Adafruit DRV2605 Haptic Controller Breakout

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

1.8" TFT Display Breakout and Shield

MLX90393 Wide-Range 3-Axis Magnetometer

MCP Bit DAC Tutorial

Adafruit Capacitive Touch Sensor Breakouts

Adafruit IO Basics: Temperature & Humidity

NeoPixel Basketball Hoop

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

Adafruit IO Basics: Analog Input

Adafruit MMA8451 Accelerometer Breakout

GPS Logging Dog Harness

Using IFTTT with Adafruit IO to Make an IoT Door Detector

Circuit Playground Express Laser Tag

Adafruit MPRLS Ported Pressure Sensor Breakout

Celebration Spectacles

Trinket-Powered Conference Room Occupancy Display

Adafruit Si5351 Clock Generator Breakout

Introducing Adafruit Trellis

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

Sino:bit with Arduino

Adafruit DRV2605 Haptic Controller Breakout

Adafruit AS channel Visible Light Sensor

Adafruit AM2320 Sensor

Adafruit MMA8451 Accelerometer Breakout

Adafruit SGP30 TVOC/eCO2 Gas Sensor

Circuit Playground Express Head-Tilt Ears

Adafruit 8x16 LED Matrix FeatherWing

Adafruit ATWINC1500 WiFi Breakout

Adafruit Mini TFT " 160x80

NeoPixel LED Cortana Costume

Sword & Wand Prop Effects with Circuit Playground

Trellis 3D Printed Enclosure

Feather Weather Lamp. Created by Ruiz Brothers. Last updated on :54:26 PM UTC

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

Adafruit I2C FRAM Breakout

IS31FL x9 Charlieplexed PWM LED Driver

Introducing Circuit Playground

Circuit Playground Kaleidoscope

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

Adafruit IO Basics: Color

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

Adafruit DS3231 Precision RTC Breakout

Adabot Operation Game

Con Badge with Circuit Playground Express

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

Introducing Circuit Playground

0.96" mini Color OLED

Pushrod Garage. Created by John Park. Last updated on :07:30 PM UTC

Flora Brake Light Backpack

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

NeoPixel Ring Bangle Bracelet

Adafruit AMG8833 8x8 Thermal Camera Sensor

Adafruit 8x16 LED Matrix FeatherWing

MCP Bit DAC Tutorial

Mad Science Test Tube Rack

Adafruit LIS3DH Triple-Axis Accelerometer Breakout

Adafruit IO Basics: Servo

Adafruit MMA8451 Accelerometer Breakout

ISS Pin. Created by Leslie Birch. Last updated on :27:30 PM UTC

Adafruit 1.27" and 1.5" Color OLED Breakout Board

Adafruit Mini TFT with Joystick Featherwing

Bunny Ears with MakeCode

Naughty or Nice Machine

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

Adafruit MAX31865 RTD PT100 or PT1000 Amplifier

CircuitPlayground Minecraft Gesture Controller

Adafruit PCF8523 Real Time Clock

Adafruit CCS811 Air Quality Sensor

DIY Circuit Playground Shields

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

Getting Started with FLORA

Secret Hollow Book Intrusion Detector

Wind Blowing Emoji Prop

Adafruit 7-Segment LED FeatherWings

Light-Up Angler Fish Embroidery

LED Stego Flex Spike Hoodie

FLORA Pixel Brooch. Created by Becky Stern. Last updated on :19:07 PM EST

3d Printed Neopixel Tactile Switch Buttons

Transcription:

Circuit Playground Digital Input Created by Carter Nelson Last updated on 2017-02-27 03:36:50 AM UTC

Guide Contents Guide Contents Overview Required Parts Before Starting Digital Signals 3V Logic Pocket Ref Circuit Hookup Hello Digital Floating Inputs Pull It Up Pull It Down Which Way? Bouncing Inputs Under The Hood Questions and Code Challenges Questions Code Challenges 2 3 3 5 6 6 7 8 11 15 18 21 24 25 31 35 35 35 Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 2 of 35

Overview In this guide we will use our Circuit Playground along with some alligator clips and a resistor to explore how we can read digital inputs. No soldering required! Required Parts You will need the following items. Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 3 of 35

Circuit Playground (http://adafru.it/3000) Small Alligator Clip Test Lead (http://adafru.it/1008) 10k Ohm Resistor (http://adafru.it/2784) Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 4 of 35

Before Starting If you are new to the Circuit Playground, you may want to first read these overview guides. Overview (http://adafru.it/ncg) Lesson #0 (http://adafru.it/rb4) This project will use the Arduino IDE. Make sure you have added the board support for the Circuit Playground as well as installed the Circuit Playground library. MUST DO BOTH. This is covered in the guides linked above. Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 5 of 35

Digital Signals The term digital signal (http://adafru.it/ubi) can be used to describe a wide range of signal types. But in this guide we are going to use a very simple definition for a digital signal - a signal which has only two values. Such a signal would look something like this: The two values could be anything. However, since we are dealing with an electrical signal, we will be dealing with an electrical value - voltage. But what are the two values for the voltage? Typically, 0V is used for one of them and is referred to as "LOW". The other one is referred to as "HIGH" and its voltage depends on the hardware (http://adafru.it/ubj), but some common values you'll see are 5V (ex: Arduino Uno) and 3.3V (ex: Circuit Playground). 3V Logic Let's focus on the 3.3V logic used by the Circuit Playground. Quite often this is just referred to as "three volt logic" as we're all too lazy to mention that extra "point three". So...what about 3.2999V? It's not 3.3V, so is it HIGH? Is it LOW? Or what about 0.1V? It's not 0V, so it it LOW? Is it HIGH? Real world signals will have some noise and thus will behave this way. Therefore, to deal with this, a range of values is setup to represent HIGH and LOW. For 3.3V level logic, it looks like this: Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 6 of 35

So 3.2999V would be seen as HIGH and 0.1V would be seen as LOW. Everything that is designed to work with 3.3V logic is expected to work within these limits. If it doesn't, it is considered broken. Nothing should be generating values in the "gray zone". Pocket Ref For this guide, it's good enough to just keep this decoder ring in mind: 0V = LOW = FALSE = 0 3.3V = HIGH = TRUE = 1 Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 7 of 35

Circuit Hookup Alright, let's go hands on. The hookup for this guide is very simple. While a button could be used, we will simply touch the alligator clips together to simulate this same function. We'll use a resistor a little later, but for now just connect the alligator clips as shown. This is the overall view of the hookup. RED to 3.3V BLUE to #3 BLACK to GND Simply connect the alligator clips to the gold pads on the edge of the Circuit Playground as shown. READ THE FOLLOWING TO PREVENT POTENTIAL DAMAGE. Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 8 of 35

Be careful to not let the BLACK and RED alligator clips touch each other directly. We will be touching other alligator clips together, but never these two. Touching RED to BLUE is OK. Touching BLACK to BLUE is OK. Touching BLACK to RED is NOT Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 9 of 35

OK. This will short power to ground and the Circuit Playground will not be happy. Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 10 of 35

Hello Digital Alright, let's start super simple. To read the digital value on the #3 pad, we will use the Arduino library function digitalread (http://adafru.it/ubk). You can use the following sketch to read and print the value. /////////////////////////////////////////////////////////////////////////////// // Circuit Playground Digital In - Hello Digital // // Author: Carter Nelson // MIT License (https://opensource.org/licenses/mit) #include <Adafruit_CircuitPlayground.h> /////////////////////////////////////////////////////////////////////////////// void setup() { Serial.begin(9600); CircuitPlayground.begin(); pinmode(3, INPUT); } /////////////////////////////////////////////////////////////////////////////// void loop() { int reading = digitalread(3); Serial.println(reading); delay(500); } With this code loaded and running on the Circuit Playground, open the Serial Monitor. Tools -> Serial Monitor Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 11 of 35

Now touch the BLACK clip to the BLUE clip. This sets the voltage of the digital input to 0V (LOW). You should see 0s scroll by in the Serial Monitor. Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 12 of 35

Now touch the RED clip to the BLUE clip. This sets the voltage of the digital input to 3.3V (HIGH). You should see 1s scroll by in the Serial Monitor. Pretty straight forward. When the digital input pin is connected to 0V (GND) it is LOW and prints out 0s. When the digital input pin is connected to 3.3V it is HIGH and prints out 1s. And there are our two values, 0 (LOW) and 1 (HIGH). So we're done, right? Of course not. Just getting started. What about the case where neither the BLACK nor the RED are touching the BLUE clip? Is that LOW? Is that HIGH? Truth is, I don't know. And no one does, not even the Circuit Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 13 of 35

Playground. The poor little BLUE alligator clip is floating in space not tied to any known value. This is called a "floating input" and we'll look at how to deal with this next. Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 14 of 35

Floating Inputs Here's what our alligator clip setup looks like when nothing is connected to the BLUE clip. You might think that the voltage would be 0V and the digital pin would read LOW. After all, it's not attached to anything. It's attached to nothing. And 0 is like nothing. However, voltages don't exist at a point - they exist between two points. And in the case of the floating input, one of those points is not fixed to any known value. Therefore, the voltage is also unknown. The end result is that the actual value is unpredictable, which isn't very useful. You can try using the sketch from the previous section and watch for the value to change. However, the following sketch will monitor the pin constantly (very fast) and detect when it changes. /////////////////////////////////////////////////////////////////////////////// // Circuit Playground Digital In - Floating Input Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 15 of 35

// // Author: Carter Nelson // MIT License (https://opensource.org/licenses/mit) #include <Adafruit_CircuitPlayground.h> int initialvalue; /////////////////////////////////////////////////////////////////////////////// void setup() { Serial.begin(9600); while (!Serial) ; CircuitPlayground.begin(); pinmode(3, INPUT); initialvalue = digitalread(3); Serial.print("Initial value = "); Serial.println(initialValue); Serial.println("Waiting for that to change..."); } /////////////////////////////////////////////////////////////////////////////// void loop() { int reading = digitalread(3); if (reading!= initialvalue) { Serial.print("It changed! value = "); Serial.println(reading); while (true) ; // sit here forever } } With this code loaded and running on the Circuit Playground, open the Serial Monitor. Tools -> Serial Monitor Now pick up the BLUE alligator clip and try touching it with your finger. You may need to try touching it several times, but hopefully at some point it will see a change and report it as shown below. Note that your initial value might be 1 - it totally depends on what ever randomness is happening in your test environment. Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 16 of 35

The fix for this is pretty straight forward. You just set things up so that the digital input, the BLUE alligator clip, is always attached to a known value, in this case either the RED (3.3V) alligator clip or the BLACK (0V) alligator clip. This is called "pulling" the input, as you are forcing it (pulling it) to a specific value. When the input is pulled to 3.3V it is called "pulling up". When the input is pulled to 0V it is called "pulling down". You can go either way. But wait! Remember we can't touch the RED and BLACK wires directly to each other. So if one of those is already attached to the BLUE clip, how do we ever touch the other one? The answer is to use a resistor - and guess what it's called? Well, a pull up resistor or a pull down resistor, as the case may be. Let's play with both. Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 17 of 35

Pull It Up Let's start with a pull up resistor. To do so, we will take the 10k ohm resistor and attach it as shown below. We can just use the alligator clips to hook this up. Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 18 of 35

Attach the 10k ohm resistor between the BLUE and RED alligator clips. Now run the sketch from the previous section again. Try touching the BLUE alligator clip with your hand as you did before. Now it should never see any changes. It should be nicely held at 3.3V. Now try touching the BLACK clip to the BLUE clip. Now it should see the change and report it. So we've fixed the floating input problem. The resistor "pulls" the input "up" to 3.3V and the pin reads HIGH (1). When we touch the BLACK clip, the pin sees 0V and reads LOW (0). Additionally, the resistor prevents a short between power and ground. That's why it's important to touch the BLACK only to the BLUE. Even with this resistor in the circuit, it is still bad to touch BLACK to RED. DON'T DO IT!! Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 19 of 35

We can do the same thing in the other direction. That is, we can "pull down". Let's look at that next. Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 20 of 35

Pull It Down This is the same idea as the pull up resistor, except now it is attached to GND. The hook up is shown below. Again, we use the alligator clips to set this up. Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 21 of 35

Attach the 10k ohm resistor between the BLACK and BLUE alligator clips. Run the sketch again and touch the BLUE clip with your finger. As before, it should not detect any changes. It is being held LOW and is happy there. Now try touching the RED clip to the BLUE clip. It should see the change and report it. Note that the only thing different between this and the pull up configuration is the order of the values. A pull up is normally HIGH and goes LOW when the alligator clips are touched together. A pull down is normally LOW and goes HIGH when the alligator clips are touched together. Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 22 of 35

And remember that our touching of the alligator clips is like pressing a button. So the same logic applies for button presses as well. Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 23 of 35

Which Way? So you've got two options for dealing with floating inputs. You can either pull it up or pull it down. But which one should you use? It's a good question but beyond the scope of the guide. It's one of those "depends" kind of things. For now, it is enough to simply understand what a floating input is, why it is an issue, and how they can be dealt with. Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 24 of 35

Bouncing Inputs Bumbles (http://adafru.it/ubl) aren't the only thing that bounce. So do push buttons. When you press your basic, normally open, momentary style button, it closes its little internal switch connection. In an ideal world we could hook that up to our Circuit Playground, like we have the alligator clips, and get an input signal that would look like this: That's the way a button should work and how most people think they work. However, in reality it looks more like this: Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 25 of 35

It goes up and down several times before finally stabilizing. Take a button apart and check out its guts and see how it works. Inside you'll find some form of mechanism that brings two contacts together - much like we are doing with the alligator clips. However, the contact doesn't happen instantly. It bounces around a bit, going closed, open, closed, open, closed, etc. This is purely a mechanical phenomenon, kind of like dropping a ball - it bounces before finally sitting still on the ground. It does this over the period of time = t. While this period of time is very fast, and seems almost instantaneous to you and me, to the Circuit Playground, it's a huge period of time. The Circuit Playground can execute many many instructions in that period of time. We can use our alligator clip setup to gain an appreciation for this. We will use the "pull down" setup from the previous section, so make sure you've got the alligator clips hooked up that way. Now run the following sketch. /////////////////////////////////////////////////////////////////////////////// // Circuit Playground Digital In - Bounce Demo // // Author: Carter Nelson // MIT License (https://opensource.org/licenses/mit) #include <Adafruit_CircuitPlayground.h> int currentstate; int previousstate; uint32_t count; /////////////////////////////////////////////////////////////////////////////// Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 26 of 35

void setup() { Serial.begin(9600); CircuitPlayground.begin(); pinmode(3, INPUT); count = 0; currentstate = digitalread(3); previousstate = currentstate; } /////////////////////////////////////////////////////////////////////////////// void loop() { currentstate = digitalread(3); if (currentstate!= previousstate) { count = count + 1; Serial.print("State changed from "); Serial.print(previousState); Serial.print(" to "); Serial.print(currentState); Serial.print(". count = "); Serial.println(count); previousstate = currentstate; //delay(100); } } With this sketch loaded and running on the Circuit Playground, open the Serial Monitor. Tools -> Serial Monitor Now play around with touching the alligator clips together. Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 27 of 35

Touch the RED to the BLUE alligator clip. Did you get more than one print out when you made the connection? Try touching the clips several times and watch the behavior of the messages in the Serial Monitor. If you do it just right, you can get only one message to print. However, more often you get multiple lines. Same when the connection is removed - the bounce happens in both directions. Touching the alligator clips together like this is a very bouncy operation. The Circuit Playground is fast enough to see all those bounces and prints out a message each time. Here's what I get trying this after a fair amount of coffee to add some extra bounce. Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 28 of 35

So how do we fix this? There are many ways, both hardware based and software based. So many in fact that a discussion of debouncing techniques is worthy of its own guide (or more). However, we will demonstrate one very simple software approach. The trick is to just add a delay after the initial detection. This forces the Circuit Playground to go to sleep during the bouncing and not pay any attention to it. To test this, simply uncomment (remove the //) the following line in the previous sketch. (it's near the bottom) delay(100); Run this new code and play around again with touching the alligator clips together. Now you should get a single print out when touched and separated as shown below. The main drawback to this technique is that the Circuit Playground can not do anything else during the delay() period. For more complicated programs the processor is always doing something and you try to avoid forcing is to go to sleep using something like delay(). However, for simple programs, this technique works just fine. In fact, you'll see it used quite often. It's simple and works. Ever seen anything like this at the top of a sketch? #define DEBOUNCE 100 Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 29 of 35

Look elsewhere in the code and you'll likely find: delay(debounce) It's using the same technique and simply defining the delay period globally. Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 30 of 35

Under The Hood The Circuit Playground has two push buttons (http://adafru.it/ubm). They are simple momentary buttons that effectively do the same thing as pressing the alligator clips together. But how are they integrated into the Circuit Playground? Do they deal with any of the issues we've talked about? Let's take a look. Take a look at the schematic (http://adafru.it/ubn) for the Circuit Playground. The circuit diagram for the buttons can be found in the upper left. Let's just look at one button since they are both used in the same way. Here's a blow up of the part of the circuit diagram that matters. The thing above the label EVQQ is the actual button. Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 31 of 35

We don't have a button in our alligator clip setup, so let's remove it. Now we are left with this. Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 32 of 35

It may not look like it, but this is identical to the "pull down" version of our alligator clip setup. Here's the schematic showing where the various alligator clips are. Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 33 of 35

So the Circuit Playground deals with the floating input issue by using a pull down resistor. The same thing is done for both buttons. Righty-o. Now, what about dealing with switch bounce? Any magic going on there? Well, let's take a look at the code for CircuitPlayground.leftButton() and see what's going on. Here it is (direct link to repo (http://adafru.it/uet)): boolean Adafruit_CircuitPlayground::leftButton(void) { return digitalread(cplay_leftbutton); } Hmmmm. It just returns the value from digitalread() for the input the button is attached to. Same as we've been doing with our alligator clips. That's it. Nothing else. So, there is no debouncing being done by the Circuit Playground library. It is left up to the user (you) to incorporate this in their program somehow. Adafruit Industries https://learn.adafruit.com/circuit-playground-digital-input Page 34 of 35

Questions and Code Challenges The following are some questions related to this project along with some suggested code challenges. The idea is to provoke thought, test your understanding, and get you coding! While the sketches provided in this guide work, there is room for improvement and additional features. Have fun playing with the provided code to see what you can do with it. Or do something new and exciting! Questions The microcontroller on the Circuit Playground has internal pull up resistors. Why were external pull downs used instead? (hint: it's kind of subjective) Why was a value of 10k ohms used for the pull up/down resistor? Code Challenges Reduce the amount of delay in the bounce sketch and see how it affects the debounce behavior. Try making it as low as 1, i.e. delay(1). Modify the bounce sketch to read the input from one of the Circuit Playground push buttons to examine its bounce behavior. (remove the delay) Adafruit Industries Last Updated: 2017-02-27 03:36:49 AM UTC Page 35 of 35