Analog Inputs

Analog Inputs (AI) are one of the four contexts or types of circuits that are core to tangible.

If you have not yet looked at the tangible matrix Building Block, you should check it out now. It will introduce you to all four contexts.

Context :: Analog Input

matrix highlighting analog input

Analog refers to signals, circuits, or logical systems that are variable or graded. They can be the top or bottom of the waterfall and every shade in between.

Input and Output are defined from the perspective of our Arduino.

Electrical signals generated in circuits and sent into the Arduino are INPUTS.

So, Analog Inputs are variable signals (V) that move INTO the Arduino.

Card

Your kit contains a reference card for analog inputs.

Each card indicates the context, command, circuit and offers a code sample.

card image of analog input

Video :: Analog Inputs

Get the Slides

Classic Example

At the introductory level, two circuits can be used as classic examples of an analog input: the POTentiometer, or POT and a photoCell.

Circuit

POTs and photoCells are both resistive circuits that make use of a concept called a voltage divider. The electrical similarity of these examples means they can be swapped out and the core behaviours will remain the same. The Analog Input card in your kit depicts a photocell circuit. We will look at both in this Building Block.

Potentiometer Circuit

The circuit for reading a potentiometer is very similar to the basic POT setup. In this case we will use the Arduino 5V for power (not your battery!) and connect the POT wiper to an Analog IN pin.

schematic of pot to Analog in
POT connected to Analog in A0
Photocell Circuit

As above, the basic photocell circuit is modified to connect to an Analog In pin.

pot schematic AI

Command :: analogRead()

The code that creates an Analog Input is:

    state = analogRead(pin);

where:
pin = A0 – A5 (inclusive)
state = 0 – 1023

Note 1: these pins are prefixed with a capital A

The line of code above means:

Read the analog (variabel, staircase) state of pinAx and store that reading in a variable called state.

How Does it Work?

Much like we did for Analog Outputs we can imagine the input signal as a staircase of changing voltages.

The steps on the staircase of an Analog Input are much shorter than their output sibling, so even though we still cover a range of 5V, there are 4x as many steps! The state range for Analog Input therefore is 0 – 1023. (Note: analog inputs store 10-bit numbers while analog outputs use 8-bit numbers).

As mentioned above, both examples belong to a class of circuits known as voltage dividers.

When two resistors are placed in series with one another and we measure the voltage at the point between them we see a voltage that falls between max power (top of waterfall, 5V in Arduino Uno) and ground (bottom of waterfall, 0V). The exact voltage depends on the ratio of the resistors.

POTentiometer
The wiper in the POT, acts to continuously divide the total resistance of the device. We there for have two resistors that change in opposition to each other. When one gets big, the other gets small. Technically we say that they are in an inverse relationship.

When you turn the dial of the POT all the way towards the leg connected to ground you make the resistance between 5V and the wiper relatively LARGE. The resistance between wiper and ground (0V) gets close to zero. In this case the signal to the Arduino is small and the value (state) of an analogRead is close to zero.

When you turn the dial of the POT all the way towards the leg connected to 5V the resistance between 5V and the wiper gets close to zero. The resistance between wiper and ground gets relatively LARGE. In this case the signal to the Arduino is close to 5V and the value (state) of an analog read is close to the upper limit of 1023.

PhotoCell
In the case of the photocell we have one fixed resistor and one variable resistor that depends on light.

When the environment is dim, the resistance of the photoCell is HUGE ( ~1 Million Ohms). In this case the signal to the Arduino is small and the value (state) of an analog read is close to zero.

When the environment is bright, the resistance of the photoCell drops rapidly (~1k or less in bright light). In this case the signal to the Arduino is close to 5V and the value (state) of an analog read is close to the upper limit of 1023.

Important

In Arduino, we read inputs.

Check the Arduino Docs for this topic

Code Sample

Connect your circuit to pin A0. Upload the code below. Open the Serial Plotter.

With a photocell circuit; cover the sensor with your hand watch the plotter trace. With the POT circuit; turn the dial, watch the trace. Notice that the POT holds it value.

int sensorPin = A0;
int state = 0;

void setup() {
  pinMode(sensorPin, INPUT); 
  Serial.begin(9600);
}
void loop() {
  state = analogRead( sensorPin ); 
  Serial.println(state); 
  delay(50); 
}
What to expect

The trace below is a screen cap from my serial plotter while running the above code with a photocell connected. The relatively flat parts of the curve on the left and right are from moments when full room (ambient) light fell on the sensor.

The wobbly bit in the middle is from me quickly covering and uncovering the sensor with my hand.

Give it a try.

Going Further

Some follow up Arduino references:

*pinMode

*Serial

*see analogInputs in action with a photocell or a pot

*learn about thresholding analog sensor values.

Leave a Reply

Your email address will not be published. Required fields are marked *