ME102 Lab 2: Input and Output with the Arduino

  1. Hardware setup
    • Output
    • Input
    • Sample Solution
  2. Interacting with the Outside World
    • Digital Input
    • Analog Input
    • PWM Output
    • Quadrature Encoding
  3. Putting it together and Check Off
  4. References

1| Setting up the software environment

This lab will requires the same setup as last week. However, breadboard space is limited and some planning will be required to fit everything nicely on the board. You will be making an LED array that will display different patterns based on three types of input: a pushbutton, an optical encoder and a potentiometer. You should make an electrical circuit that is based on the following electrical schematic. The code that you will be experimenting with relies on using the input and output pin numbers provided. In addition to that, you’ll be lightly modifying some provided sketches.

Output- LED Array

Larger image here.

Take the LEDs given to you in your kit and build a line of LED’s. Remember to select suitable pull-up resistors that are around 1k ohms. The SN7417 (datasheet) can only sink 40mA.

Input

larger image here

There are three different types of inputs that we’ll be exploring: digital input with a tactile switch, digital input with an optical encoder, and analog input with a potentiometer.

The pushbutton is connects two points in a circuit when you press it. The pushbutton sits between a pull-up resistor and ground. A pull-up resistor is a resistor used to force the  wire to settle to a HIGH state, and prevent excess current from flowing into your circuit. When the switch is pressed, voltage at the input to the 74LS14 (datasheet) is then 0.

Optical encoders (datasheet) are a way to measure rotational position, and velocity. Two photodiodes are offset at 90 degrees and based on pulse frequency and the difference of the pulses from the diodes, one can figure out rotational position and direction of rotation. Make sure that you are correctly supplying voltage to the encoder. Applying voltage wrongly can break this $50 part.

The potentiometer will be the only analog input that you will be using. As described in lab 1, a voltage follower using either the OAP4342 (datasheet) or the TLV2374 (datasheet) acts to separate the voltage from the potentiometer and the signal voltage.

Sample Solution


Larger image available

2| Interacting with the Outside World

Digital Input

First, verify that your completed LED array works correctly by loading the provided sketch “BlinkAll”. This should cause the LED’s to blink in unison. To demonstrate digital input, we will be controlling the LED’s with a pushbutton.

PushButton

  1. Load the “Blinkall” sketch
  2. Modify it so that when the button is held down, the lights stop blinking.  They can be all on or all off.
  3. Sometimes, you may want to press a button and shut everything down. Load the “latchButton” sketch.
  4. Modify it so that when a button is pressed, the lights stop blinking. And when the button is pressed again, the lights resume blinking.

Something to Consider: Debouncing

Push buttons, like the one used in lab, have a tactile response due to a leaf spring that is inside. A single button press will often result in physical rebounding that can send tiny little bounces. The image below shows both the actual response (top) and a digital interpretation (bottom) of a switch.

Analog Input

Analog input is another way of encoding information into the digital realm. The Arduino measures voltages between 0V and 5V with an ADC; it then scales the measured voltages to an integer value between 0 and 1023. In this part of the lab, you will be using a potentiometer to control the luminosity of the LED’s. The Arduino’s analogWrite() function really outputs a PWM signal. Due to the use of the inverter, analogWrite(0) is a 100% duty cycle whereas analogWrite(255) is a 0% duty cycle.

Set up your potentiometer

  1. Load the “Potentiometer” Sketch. Note that at low duty cycles, the LED lights may flicker.
  2. Modify it so that when the button is held down, one of the lights blinks in and out. Its luminosity should still be determined by the potentiometer.

Quadrature Encoding

Sometimes, you may run across a sensor or actuator for which other people have written a library. Libraries generally simplify your code by creating a layer of abstraction that removes low level details. For example, instead of decoding the pulses of a quadrature encoder to figure out rotational change, you only need to call a “helper” function to determine its position. There are many tested libraries available at http://arduino.cc/en/Reference/Libraries.

To use the quadrature library:

  1. Unzip arduino-quadrature-0.90.zip  into your your Arduino installation’s hardware/libraries directory.
  2. If a Quadrature folder exists, then someone before you has already done so.
  3. View the sample sketch quadratureTest.
  4. The serial monitor of the Arduino IDE is used here in order to see the effect of the encoder.
  5. To toggle the serial monitor, click the rightmost button at the top of the IDE.
  6. The black box at the bottom should now start displaying numbers. Turn the encoder knob, and you should see the numbers change.
  7. Create a sketch that allows the optical encoder to control which LED is on. As the encoder rotates clockwise, the LED that is on should cycle from 1 to 5. When the encoder rotates counter clockwise, the LED that is on should cycle the other direction.

Limitations of the Arduino

The Arduino does not have a built in quadrature encoder, and this makes it difficult to use optical encoders for motor control. Dedicated IC’s for quadrature decoding such as the HP HCTL 20XX. They range between $10 and $20 per chip, and will require some thinking to implement them properly.

3| Putting it together and Check Off

You must have a board that completes all the following:

  1. Button that turns off all lights when held
  2. An array LED lights that scroll by when the encoder is rotated.
  3. The luminosity of the LED’s is controlled by the potentiometer.

4| References

http://www.ganssle.com/debouncing.pdf

A fantastic introduction to debouncing and various algorithms devised to resolve it.

http://www.neufeld.newton.ks.us/electronics/?cat=9

The source of the quadrature library. This guy is also creating a CNC printer with the Arduino.