Source Code. Projects. Nerd Stuff. Art Stuff.

Analog Input

Digital signals are good at telling us about discrete changes or states in the physical world. However, sometimes we will want a range of values or values over time (ie: data in the form of variable quantities). When we want to measure variably changing conditions like this, we need analog inputs. There are many transducers available to convert various changing conditions to changing electrical quantities. There are photocells, which convert the amount of light falling on them to a varying resistance. Flex sensors change resistance as they are bent. Force-sensitive resistors change resistance based on a changing force applied to the surface of the sensor. Thermistors change resistance in response to changing heat. And there are many more.

In order to read these changing resistances, we put them in a circuit and pass a current through them, so that we can see the changing voltage that results. One of the simplest (and most common) examples of a circuit that uses varying resistance is the voltage divider. A static voltage divider typically looks like this:

But to create a variable resistance, multiple resistors can be replaced with a potentiometer. In this case, the center of the potentiometer (pot) is connected to the microcontroller. The other two sides are attached to power and ground:

The mechanism that is usually contained within a pot is a slider that varies in position from one pin (connected to power), and the other pin (connected to ground). For this reason, depending on the position of the switch, resistance will vary. The ratio of the resistance on one side of the center of the pot to the resistance on the other side provides a varying voltage at the center of the pot.

Alternatively, we can use a variable resistor from our voltage supply to the microcontroller pin, and a fixed resistor to ground.

Here, the variable resistor feeds a varying voltage to the microcontroller pin, while the fixed resistor provides a path to ground. The current will follow the path of least resistance, from the 5V source through the variable resistor to the microcontroller pin. The higher the fixed resistor, the higher the bottom of the voltage range will be (it’s generally good practice to use a fixed resistor that’s in the same order of magnitude as the the range of the variable resistor.

Since a microcontroller’s inputs can read only two values (typically 0 volts or 5 volts), we need an analog-to-digital converter (ADC) to read this changing, or analog voltage, and convert it to a digital form. It reads a changing input voltage and converts it to a binary value, which a microcontroller can then store in memory. ACDs are a common tool, and many microcontrollers have ADCs built in to them. At the time of writing this article, the Atmega328 is the latest chip on the Arduino, and it has 8 ADCs.

When the ADC circuitry attached to an ADC pin on these microcontrollers senses a varying voltage, it converts that voltage to a 10-bit number (0 to 1023). If you choose to read these pins using the ADC command instead of the getPin() command that we used to read them as a digital input, you will get that 10-bit number.

The command in Arduino is the analogRead() command, where pin is the number of the analog input pin you’re using, and analogVar is the variable where we are storing the result from the ADC:

analogVar = analogRead(pin)


Decoupling Analog Inputs

If you find the readings from your analog inputs are inconsistent or you notice “noise” in your readings, it helps to decouple your input circuit. Decoupling means smoothing out the dips and spikes going into the circuit from the rest of your microcontroller circuit. You can do this by placing a capacitor from voltage to ground as close to where the analog input connects to voltage. A capacitor used this way is referred to as a decoupling capacitor. They serve as tiny surge protectors.