Analog Input and Output

We have seen that digital information only has two possibilities, on and off. Analog information, on the other hand can hold a range of possible values. We perceive the world as a stream of analog information via our eyes and ears and other senses. By using analog information with our Arduino, we can respond to the users’ input in a more complex fashion. We can control the brightness of an LED, setting it to shine brightly, glow dimly, or show any range of values in between.

Analog information is continuous and can hold a range of possible values. It is quite simple to attach sensors and variable resistors to the Arduino so we can capture information with a range of values, not just on and off. Some examples of sensors would be photoresistors, which capture information about light levels, or thermistors, which capture information about temperature. There are many, many types of sensors available that can give us information about the world around us that can be attached to the Arduino.

We will learn how to add a potentiometer to a circuit to control the brightness of our LED, from the dimmest glow to the brightest.

ch6-circuit-pot-final-2-01

What’s a potentiometer?

A potentiometer, sometimes called a pot, is a knob or dial that can be turned to increase or decrease the amount of resistance depending on how far, and in which direction, it is turned. Potentiometers come in many sizes and shapes. We will be using a 10k ohm potentiometer in our circuit.

ch6-pot-schematic-symbol-01

In order to build our circuit, first we will make a basic circuit using an LED, a 220 Ohm resistor, some jumpers and an Arduino Uno. This is quite similar to the basic circuit we have used in our other examples, with one important difference. The Arduino pin attached to the LED is pin 9, not pin 13.

ch6-circuit-pot-step-1-01

Now we will attach the potentiometer. A potentiometer has 3 pins. In our circuit, we will attach the middle pin to a pin on the Arduino, one of the outer pins to the power bus, and the other outer pin to the ground bus. It doesn’t matter which outer pin goes to power and which one to ground.

We will place the potentiometer parallel to the trench as depicted in the drawing. Each pin of the potentiometer is in a separate row of tie points, with an empty tie point between each of the pins. Orient the potentiometer facing away from the Arduino, with the dial over the trench, as shown below. This will make it easier for you to reach the potentiometer to turn it and to integrate it into the rest of the circuit.

ch6-circuit-pot-step-2-01

Now we’ll attach the potentiometer to the power and ground busses on the Arduino.

ch6-circuit-pot-step-3-01

Finally, we will attach the middle pin of the potentiometer to pin 9 on the Arduino.

ch6-circuit-pot-step-4-01-1

Upload the sketch

Now that you’ve built the circuit, attach your Arduino to a computer. From the File Menu > Examples > 03.Analog > AnalogInOutSerial. Save the sketch, renaming it as something like MyAnalogInOutSerial. Click the Verify button, then click the Upload button. Turn the potentiometer. Your LED should go from dim to bright, depending on how far it is turned.

Using a photoresistor

Let’s look at how to use a sensor to test something about conditions in a room. We’ll look at a photocell, also known as a photoresistor. A photoresistor can test for light levels in a room. Before you start making adjustments to the circuit, make sure that it is not connected to a computer or other power source.

photocell-01

Setting up the circuit for a photoresistor is slightly more complicated than using a potentiometer. To set up this circuit, first take out the potentiometer from your breadboard. Then place the photocell, or photoresistor into the breadboard, with one end attached to pin A0. There is no polarity or direction in a photocell, you can place either one of the leads in that row of tie points. Place the other lead in another row of tie points.circuit-add-photocell-01

Next we will attach the other lead to power with a jumper as shown in the drawing.

circuit-photocell-jumper-02

Add a 10K resistor to the circuit. One lead is in the same row of tie points as the jumper to pin A0 and one lead of the resistor. The other end of the resistor is in another row of tie points, and jumped to the ground bus.
photocell-circuit-resistor-03

You can attach the computer back to the Arduino now. If you have not made any changes to the code, you should be able to run the same sketch on the Uno as you downloaded and saved when you built the potentiometer circuit. If your hand is covering the photocell, the LED will be shine more dimly, the more light that shines on it, the brighter the LED will light up.

photocell-circuit-final-small-01

Further reading/resources