Exercise 2


Implement an LED that models a car speedometer display.

Learning Goals:

  • Understand analog input
  • Use constants & variables
  • Function return results
  • Use complex IF/ELSE logic

In this exercise, you build on what you did in Exercise 1, in two parts.

Imagine you are driving your car down the road. Your speed is controlled by the varying pressure on the gas pedal. (Well, at least in ancient analog cars it is; these days there are probably digital sensors down on the floor.) As that changes, the speedometer indicates visually how fast you are going.

You are going to make a crude simulation of this speedometer in order to learn about analog inputs. (In Exercise 1, there were no inputs at all, only digital outputs.) Your analog input is a little sensor known as a potentiometer (pot for short), or a variable resistor. We’ll use one called a trim pot that is very much like a volume knob that you can rotate to get different values.

Your trim pot is the way you’ll simulate the gas pedal input to your system. It will allow you to change the “speed” by rotating the knob. Your challenge is to write a program that reads this sensor and figure out how fast the simulated car is going.

Then you will light up some LEDs in order to indicate the speed. You’ll do this in two ways.

One LED simply indicates whether the speed is over or under a certain value. This is a kind of warning light that the driver is going too fast. We’ll call this the “speeding” indicator LED.

Three other LEDs will be used to give more detail about the speed and indicate in what range it falls. Below a certain threshold value is considered low speed, over a certain ceiling value is high speed, and in between those two is middle speed. Only one of these three LEDs is lit up at a time, and it shows whether the car is traveling in one of the three speed ranges.

That’s the concept for the exercise. Here are the detailed specs:

  • Use an LED on pin 13 for the speeding indicator.
  • You can reuse the three LEDs from the binary counter of Exercise 1 for the low/middle/high speed indicator LEDs (pins 10, 11, and 12).
  • Connect the trim pot to one of the analog pins on the Arduino. This is important!

Modify your breadboard circuit to add the trim pot. You can follow the photos below and check the circuit diagram to see how the trim pot is to be connected.

click to view larger

click to view larger

click to view larger

For the trim pot, there are three pins to connect with wires. The middle one is the data pin, on which you read the analog value. The outer ones go to +5V and GND. It doesn’t matter which one is which. That only affects which direction you turn the knob to go faster or slower. You can swap them if you like to experiment.

Then write your Arduino sketch to implement all of the logic required above. Note that the analogRead command gives a value between 0 and 1023, so your “speed” will always be between those two numbers. This is true for any analog sensor not just potentiometer knobs. That’s just the way the system works.

Here is an sketch you can use to get started. This program shows you how to read an analog sensor and it turns on the speed warning light also. You will have to add the code to do the three LED display part of the spec. Copy and paste this code into a new Arduino sketch, and then save it.

 ahdavidson: 7/27/11
 Shows how to read an analog sensor (like a potentiometer). An LED is turned on
 if the sensor reading is over a certain value.
 Note that the analogRead returns a value between 0 and 1023. That's just the way
 the system works for any analog sensor.

// some constants so we don't have "magic numbers" in our code

const int speedValPin = A0;   // the potentiometer input
const int speedingPin = 13;   // the speeeding warning LED

const int warningSpeed = 512;  // over this turns on the warning light

// this is a variable, not a constant -- it changes during the program

int sensorValue = 0;  // variable to store the value of the potentiometer reading
                      // this will be between 0 and 1023

void setup()
   // first set all pins to the correct mode

   pinMode(speedValPin, INPUT);
   pinMode(speedingPin, OUTPUT);

void loop()
   // each time through the loop, read the value of the pot
   // and store it in a variable

   sensorValue = analogRead(speedValPin);

   // turn on the warning light if it's over the warning speed

   if (sensorValue > warningSpeed)
      digitalWrite (speedingPin, HIGH);
      digitalWrite (speedingPin, LOW);

   // that's it, now go back and keep checking


Here is my solution to the whole exercise, if you want to peek: Speedo.

And here is a modified version of the solution that shows how to use the Serial Monitor in Arduino to display debugging information from the program in the IDE while the program is running. Copy and paste from here: SpeedoDebug.

For extra credit, you can try two things:

  • Add more LEDs so that you can show a finer gradation of speed ranges in the display. You’ll have to ad to the circuit and also modify your code.
  • Try to make the speed range display behave like a peak volume meter on audio gear — have it turn on all LEDs up to and including one that shows the current speed. The original spec calls for only one to be on at a time.