Home‎ > ‎CS 11M‎ > ‎

Analog Input

Questions, Answers and Review

  • Questions from last class? 
  • Questions about labs or upcoming homework? 

Electricity Basics 

There are two fundamental quantities in electrical systems:
  • Voltage, or electrical potential, which is the force that causes electrons to move and
  • Current which is the rate of flow of electrons

 
Electrons can flow through air but it takes tremendous voltage (or high voltage) to do that. Electrons flow easily through wire. Giving electrons a path allows them to flow.  
 
The resistor keeps the electrons from flowing too fast. If you connect the positive and negative pins of a battery directly together that's a short circuit. The rapid flow of electrons will cause the battery to heat up possibly causing it to burst and expose you to toxic chemicals.  

Ohm's Law

 
Image source: Wikipedia/Matt Rider

Ohm's Law is named after German physicist Georg Ohm. It's describes the relationship between voltage, electrical current, resistance and power. The 12 equations implied by Ohm's law are shown in the picture on the right. The most important formulas to remember are:  
  • Voltage (E) = Current (I) x Resistance (R)  
and  
  • Power (P) = Current (I) x Voltage (E)  
The others can be derived using simple algebra. Take the circuit above. The resistor has a value of 220 ohms and the battery pack delivers 3 volts. To compute the current we can use the formula: 

    Current (I) = Voltage (E) / Resistance (R) 
    Current (I) = 3.0 volts / 220 ohms Current (I) = 0.0136 amps (or 13.6 milliamps)

Question: How much power is this circuit using? 

Schematic Symbols

Schematics are drawings that make it easy to show someone else a circuit without having to take a picture of a breadboard. In a schematic each circuit element has a symbol that's usually easy to draw by hand.  

Exercise 1: Draw a schematic with a battery and a resistor.

The Fritzing program is installed on class computers. Open it and start with a blank canvas. Add a battery pack and a resistor. 
  • Save your project as resistor.fzz 

Reading Resistors

instruments_rescolorcode.jpg
The colored bands on resistors tell you the resistors value in Ohms. The color code system was invented when it was not practical to write tiny numbers on the resistor and still exists today because it's very easy to use. The chart on the right shows what each color band means. Some resistors have four bands and some resistors have five. You can tell which band is the "left" side because there's always a space before the last band, which tells you the precision or tolerance of the resistor. 

Quesitons: Color Codes

  1. Decode: Red - Red - Black
  2. Decode: Brown - Black - Red
  3. Decode: Blue - Green - Black - Brown
  4. What's the color code of a 550 Ohm resistor? 
  5. What's the color code of a 10,000 Ohm resistor?

Image Source: Make Magazine


Series and Parallel Circuits

Resistors (and other components) can be arranged in two fundamental configurations called series and parallel. Series configuration is pictured below: 
  https://sites.google.com/a/cabrillo.edu/cs-11m/schedule/electricity-components-analogio/SeriesResistor.png?attredirects=0
When resistors are in series the resistance values add together.

    R (total) = R1 + R2

Parallel configuration is shown in the picture below:
https://sites.google.com/a/cabrillo.edu/cs-11m/schedule/electricity-components-analogio/ParallelResistor.png?attredirects=0 
When resistors are in parallel the equivalent resistance is:

    R (total) = (R1 * R2) / (R1 + R2) 

Activity: What is the equivalent value if you put two identical resistors in parallel? 

  • Solve the parallel equation for any resistor value. 
  • R1 and R2 should be the same value 
  • Answer the following questions
    • What resistor value did you choose? 
    • What is the resistance of two of those resistors in parallel? 

Voltage Dividers 

A voltage divider is a special use of series resistors that is very handy when one of your resistors is a sensor or a knob. The picture below shows a potentiometer (which is a variable resistor) in series with another resistor.  
 
Notice the "Voltage" label. Controlling the knob or sensor will change the voltage on that wire. The formula for the voltage on the wire is: 

    Voltage = Voltage (Battery) x R2 / (R1 + R2)

Analog Input and Output

The Arduino, like all processors, is digital, it only understands ones and zeros. In order to make sense out of our analog world it takes special circuitry on the Arduino. In this section you'll learn how to control that circuitry using C++ commands. 

Analog Input

Your Arduino has pins labeled with "A" and a number (e.g. A0, A1, etc). These pins are special pins because they are connected to an internal Analog to Digital Converter (A2D). That circuit turns a voltage into a binary number based on a reference. The reference sets the maximum allowable input voltage. An A2D gives you a coded number that represents a voltage between zero and the reference. The drawing below shows an example of how that number is generated: 
 
The chart shows you how to interpret the output of a 3-bit A2D with a 5v reference voltage. The Arduino has a 10-bit A2D which means that the values it gives you are between zero and 1024 (way too many to draw!). The reference voltage can be set in your program but it's best to keep it at the default setting (5v). The Arduino code below reads the voltage on pin A0: 

void setup() {  
 
 Serial.begin(9600);
} 

void loop() {
  int reading = analogRead(A1); 
}

Exercise 2: Read the Squeeze Input Voltage

Start this exercise by building the pressure sensor: 

Build the Pressure Sensor

Starter Code


#include <ArduinoSTL.h>

using namespace std; 

void setup() {   
  Serial.begin(9600);
} 

void loop() {
  int reading = analogRead(A1);
  delay(10);
}

Procedure

  1. Add a cout statement to the code above to print out the reading. 
  2. In the serial monitor verify that you see the reading change when you squeeze the pressure sensor.
  3. Convert the value you get into a voltage, what is it? 
Save your program as voltmeter.ino and submit it to Canvas with next week's lab. 

Analog Output

Arduino is not capable of making a true analog output. However, for many things that need an "analog" signal you can use a trick called Pulse Width Modulation. Arduino.cc has an excellent tutorial on PWM. Please read it.  
You can use PWM to make an LED glow with different intensities. The LED blinks so fast your eye cannot see the difference. 

Exercise 3: Using Analog with LEDs (Optional)

 
Build the circuit pictured at the right. Don't forget the resistor! When you're done program Arduino with the following code. What happens? 

void setup() {
  pinMode(11, OUTPUT);
} 

void loop() {

  for (i=0; i<255; i++) {
    analogWrite(11, i);
    delay(10);
  }
} 
  1. Change the code so that the LED fades from light to dark. 
  2. Compile and upload your code to verify it works correctly.
  3. Save your fader.ino file to submit to Canvas with the next lab.
Subpages (1): Project 3: Analog Data
Comments