Analog Inputs on Arduino


Before going too much further, we should talk a bit about what a signal actually is, electronic signals specifically. The signals we're talking about are time-varying "quantities" which convey some sort of information. In electrical engineering the quantity that's time-varying is usually voltage (if not that, then usually current). So when we talk about signals, just think of them as a voltage that's changing over time.

Signals are passed between devices in order to send and receive information, which might be video, audio, or some sort of encoded data. Usually the signals are transmitted through wires, but they could also pass through the air via radio frequency (RF) waves. Audio signals, for example, might be transferred between your computer's audio card and speakers, while data signals might be passed through the air between a tablet and a WiFi router.

Analog Input

Because a signal varies over time, it's helpful to plot it on a graph where time is plotted on the horizontal, x-axis, and voltage on the vertical, y-axis. Looking at a graph of a signal is usually the easiest way to identify if it's analog or digital; a time-versus-voltage graph of an analog signal should be smooth and continuous. While these signals may be limited to a range of maximum and minimum values, there is still an infinite number of possible values within that range. 

The analog input of an Arduino is a 10-bit analog to digital converter.  The "10-bit" portion lets you know what your sensitivity is.  The equation is 2^n where "n" is the number of slots you have.  2^10 = 1024.  Basically, you have to fit the analog signal into a discreet digital value.  Arduino reads from 0 to 5v so you spread the 1024 "slots" over 5V giving you 5V / 1024 = .0048V.  So you can read a voltage change of about 5mV.

Analog Sensor Examples


By turning the shaft of the potentiometer, we change the amount of resistance on either side of the wiper which is connected to the center pin of the potentiometer. When hooked up to an Arduino, this changes the relative "closeness" of that pin to 5 volts and ground, giving us a different analog input. When the shaft is turned all the way in one direction, there are 0 volts going to the pin, and we read 0. When the shaft is turned all the way in the other direction, there are 5 volts going to the pin and we read 1024. In between, analogRead() returns a number between 0 and 1024 that is proportional to the amount of voltage being applied to the pin.

Because the analog input of the potentiometer is from 0 to 1024, in order to use A0 as an output range for PWM, we need to get that range closer to 0 to 255. This can be done almost by dividing A0 by four. This will also allow us to use the full range of the potentiometer compared to just a quarter of it- allowing for a more sensitive range. Close enough is close enough for the Arduino. This can also be done by mapping the potentiometer to the PWM range of 0 to 255 exactly. Mapping does the math precisely for you. 

Now that you have a range of 0 to 255 you have to do something with it. This can be done with variables and logic. In a series of IF logic statements, you can set an action that if the value coming in from “Value,” which is the variable set in this instance for the reading of A0, is less than some threshold, then set each of the RGB Led pins to some state. In this case, pin 3 (attached to red) is high, pin 5 (attached to blue) is low, and pin 6 (attached to green) is low, producing a red glow to the LED.

You can also set ranges by setting the value between specific ranges by using the AND command in the math operators. Saying the value is greater than 42 and less that 84 is the exact same thing as saying the is between 42 and 84. Notice how there is an overlap between the values below (42) and the value above (43.) If absolute precision is not required, this is a good action to take to ensure you don’t have any dead readouts on the potentiometer by accident. 

Flex Sensor

Unlike a potentiometer that has a clean range of 0 to 1024 while hooked up to the Ardunio, not every analog sensor has that. Flex sensors come in all different sizes with all different ranges and even if you knew what each specific sensor's specifications were, most of them still don’t have an easy range to define. The average flex sensor read between 700 and 900 while hooked up to an Arduino, but some range significantly lower or higher than that. This is because as a variable resistor like the potentiometer, the flex sensor has a range of high to low or low to high resistance. However, unlike potentiometers, most flex sensors never go to zero ohms. 

That said, there is an easy way to combat this while coding. When using a sensor that doesn’t have a clean and easy range to use, you can constrain a sensor's input to a specific range. You can do this by creating a variable that is set to constrain the input the Arduino reads to a range of whatever you prefer. Again, with the Arduino, it is good practice to set the range between 0 and 255 so that it lines up with the potential range of the PWM output of the Arduino. This can then be used the same as the potentiometer’s ranges with IF statements and ranges set as you did before. 

However, because a flex sensor is not as clean as the potentiometer, it will take some tinkering and experimentation to find the actual range of the sensor. Depending on the sensor, it may still not go all the way down to 0 or all the way up to 255. This can be found pretty quickly in TinkerCAD by experimentation.

Note: The Arduino is assuming you are working with a 5V max and is actually reading the voltage drop caused by the variable resistance in a flex sensor. This means you have to plug the voltage input of the Flex Sensor into the 5V output of the Arduino. The output of the flex sensor is then fed into one of the analog pins. 

Well, now you know how to use two different types of analog inputs and two different ways to set the analog value ranges. Either of these methods works pretty consistently across all analog sensors. Now you should be able to create circuits of your choosing that use outside influences.