Week 3: Digital In/Output & Analog In with Arduino

Now we're starting to get fancy and connect the circuit components on the breadboard to the Arduino pins. Now connected to the Arduino, I'll write programs to control the behavior of those components. 

The switch is connected to Digital Pin 2, notice that it also has a 10-kilohm pulldown resistor. I learned that it provides "the digital input pin with a reference to ground" because "without it, the input will behave unreliably." Good to know!

Each LED is also connected to the Arduino. Digital Pin 3 connects to the red LED through a 220 ohm resistor, and Digital Pin 4 also through a 220 ohm resistor to the Yellow LED.

Using the Arduino web editor, I declared the switch as input and the two LEDs as outputs. Next, I programmed one light to shine only when the circuit closes and the other to light only when the circuit opens. Digital Pins only read two states of incoming voltages: on (HIGH) or off (LOW). In this case, when the button is pressed to close the circuit, a voltage greater than 3.0V is present at the pin (for my 5V board). Without not pressed, the circuit is open and the voltage reading is less than 1.5V at the pin. (Reference)

Question: How do I write a program that "listens" to a particular pattern or sequence of light flashes, and once received lights a third LED? I think I need to create a variable that stores the number of times the switch has been closed...

Analog Input

Here's another potentiometer connected to a LED (remember last week's post?), but this time through the Arduino board. Notice that the pot is connected to an analog pin instead of a digital pin. Unlike digital pins, an analog pin may read a range of incoming voltages--not just the two states of OV or 5V. The range of input values from the pot may be mapped to an output range of values to control the behavior of another component, in this case the brightness of a LED. 

This LED is connected to Digital Pin 9, which allows for Pulse Width Modulation (PWM), a technique for simulating analog output through a digital pin. Microcontrollers cannot output changing voltages, but six digital pins on the Arduino Uno provide this option to imitate analog output--look for the ~ symbol. Compared to last week's lab with the pot, I observed much smoother graduation of changes in the LED throughout the entire spin of the pot's wiper, as opposed to the light dimming completely around midway through the turn.

Next, I created a circuit with a different variable resistor: a photocell. Each photocell in my circuit is connected to an analog pin. First, with a script I wrote in the Arduino Web Editor, I measured the input range of values for each photocell. The first one returned values of 190 when completely covered and up to 890 when fully exposed to the light. The second, 150-835. I then mapped the input values of the first photocell to the output range of values for red LED, connected at Digital Pin 9, and finally, mapped the second photocell to the green LED at Digital Pin 11.