Home‎ > ‎CS 11M‎ > ‎

Analog Output and Midterm Review

Anlog Output

  • Arduino's pins are either on (HIGH) or off (LOW) 
  • But it's possible to make an LED "fade" on and off
    • analogWrite()
    • PWM

Pulse-Width Modulation

  • The analogWrite() function is controlling the duty cycle of power supplied to an output pin
  • This technique is called Pulse Width Modulation (PWM)
  • PWM is a technique for getting analog results with digital controls
  • By changing the amount of time that power is supplied to a pin, the amount of power changes proportionally
  • The function analogWrite() controls the pulse width on a scale of 0 to 255 as shown in the following diagram
  • In the image below, the green lines represent a regular time period of around 2 milliseconds

Pulse Width Modulation with Various Duty Cycles

Pulse width modulation with different duty cycles

Source: arduino.cc: pwm

Check Yourself

  1. The number of pulses provided per cycle are/is ________.
  2. The time period for each pulse is about ________ milliseconds.
  3. If the pulse width is a 50% duty cycle, the amount of power supplied (in percent) is ________%.
    1. 25
    2. 50
    3. 75
    4. 100

More Information

Build the 3-color LED Circuit

Star by building the 3-color LED and light sensor:

Build the 3-Color LED and Light Sensor

Test Your Circuit

Download the Test Program from my GitHub:

You should see the RGB LED cycle when you push the buttons: 

    Off -> Red -> Green -> Blue -> White 

Exercise 1: Variable Intensity LED

Start with the following code: 

#include <ArduinoSTL.h>

using namespace std; 

const int redLED = 11;
const int greenLED = 10;
const int blueLED = 9;

void setup() {

void loop() {
  int value; 

  cout << "Enter an intensity value from 0 to 255" << endl;
  cin >> value; 
  if (value < 0) {
    cout << "That's too low!" << endl;
    value = 0;  
  else if (value > 255) {
    cout << "That's too high!" << endl;
    value = 255;
  else {
    cout << "Setting value to: " << value << endl;    

  value = 255 - value; 
  analogWrite(redLED, value);

The code inputs a number and displays the number as an intensity. 


  1. Use the anlaogRead() function to read the squeeze sensor
  2. Display the force on the squeeze sensor as the intensity of the LED
    1. Do you notice anything weird? 
  3. The analogRead() function gives you values in the domain [0 - 1023]
  4. The anolgWrite() function takes values in the domain [0 - 255]
  5. Use math to translate between the two
    1. Verify that your LED shows a linear squeeze amount
  6. Save the file as squeeze-led.ino
  7. Submit the file with the midterm study guide answers

Midterm Review

The midterm review will have three kinds of questions:
  1. Read and explain a code fragment. 
  2. Read and correct a program. 
  3. Write a simple program from scratch. 

Code Fragments 

As a coder you must be able to read and explain what code does. The code fragments are going to be intentionally hard to copy and paste. You should be able to figure it out in your head. If you need to type them in it will cost you time. Here are some practice questions.

What does this print?

What does this print, more? 

Suppose the following code is in loop(). What happens if on successive runs through loop the variable foo has the values:
  • 23
  • 600
  • 123.3
  • 'A'

What are the four outputs given the four values of foo? 

What does this do? 

In the program below assume that pin A1 is connected to a sensor that uses a voltage divider (like your pressure sensor). Suppose this is the contents of your loop() function.
Describe what this program does. What is the purpose of the r2 variable? What would you type into the serial monitor if the fixed resistor in your sensor circuit had the color code BLUE - GREEN - BROWN - GOLD?

Reading Buttons and LEDs

Assume the following code is in the loop() function and that all of the pins have been setup correctly. The LEDs and buttons correspond to the same circuits as on your breadboard. 
Describe what the program above does. What happens if you run the program under the following scenarios:
  • Start the program then press no buttons.
  • Start the program and press RIGHT
  • Start the program and press LEFT -> RIGHT -> LEFT 

Finding Errors  

For these questions you will be given a whole program and asked to find mistakes in it. You don't have to fix the code. You have to describe how it is broken and what steps you would take to fix it. 

Broken Blink #1

Suppose this is code that was turned in for Project 2, Part 1. 
What's wrong with it? You may assume that there's no syntax errors. 


Compared with my solution to the blink assignment there are two important differences:
  1. The line with cin.ignore(1) is missing. Without that line cin will never take the '\n' out of the serial stream and Serial.available() will test true always. Therefore after one input the program will stop blinking.
  2. The digitalWrite() lines are inside of the Serial.available() if statement. If #1 above was fixed this program will only blink a single time when the user inputs a value. 

Broken Blink #2

Same as broken blink #1. 
There are multiple problems with the above program. Name as many as you can find. Justify your answers. 


The problems with this program are:
  1. In order to properly detect both buttons there should be two priorButtonState variables, one for the left and one for the right. Having only one will cause the wrong button to register when the user presses the left then right button or vice-versa.  
  2. The left button is never read. The statement int buttonState = digitalRead(leftButton) happens inside an if statement. The line declares a new variable buttonState that is only valid inside the if. The original variable buttonState that is valid throughout loop is not affected by the digitalRead() therefore the left button is useless.
  3. delay() is used as a variable. The line delay(delay - 20) compiles but causes problems because delay is a function name and not a variable. 

Coding Challenge

In the midterm I will ask you to write a simple program that will be very similar to one of the projects. Study your projects and be ready to reuse your code. If you have points missing for your projects be sure to fix them in preparation for the midterm.