Analog input on digital circuits

Digital circuit design is a really useful skill for someone who intends to be building electronic devices. Understanding the logic of binary states is something you certainly need if you want to do anything with a micro-controller, and if you want to integrate complex software controls with you tiny devices you do want to use a micro-controller.

However, most of the world is analog, not digital. While there are certainly a lot of interfaces you can design that are purely digital, there are many sorts of interactions that are analog only. Thus, in order to do some of the really interesting stuff in electronics design you need to understand analog inputs and how to handle them.

The first thing to understand is that while you may be using analog devices, if they are connected to a micro-controller then their inputs are being converted to a digital format. So while the number of states an analog device can have are actually infinite, when building circuits they are limited by the precision of your micro-controller’s analog inputs. The Arduino uses a 10-bit analog input system, which allows for 1024 (0-1023) possible states. Roughly: the Arduino can measure analog inputs to within about 0.1% precision.

In order to play around with this I decided to set up a series of LEDs (that is, multiple outputs) to indicate variable resistance levels. More LEDs light up as resistance increases.

Step one is setting up the LEDs. This is pretty simple. You might notice that I’ve got a single resister on the ground connector rather than one for each LED. The idea here is to use the sequencing features of electric circuits to save on resisters. This should result in the same effect as a separate resister for each LED. (Note: This is not actually true, as we’ll see later on in the post.)

LED array

LED array

Next we need to hook up our input: a variable resister. In this case we’re using a potentiometer. Like all variable resisters we need three connections: power, ground, and the input pin for the micro-controller. Here’s the potentiometer set up on the breadboard.

Add the potentiometer

Add the potentiometer

Now that we have our circuits put together, we need to connect them to the micro-controller. Remember that since each LED is lit separately, it needs its own pin on the board.

Connect it all to the Arduino

Connect it all to the Arduino

Now all that’s left is to do our code. It’s pretty simple: the Arduino can sense 1024 possibilities, round that off to 1000 for ease of use. There are 8 LEDs which gives each one an equal integral of 125 values. Simply divide them up and have a series of non-interfering IF statements.

void loop()
{
int inputValue = analogRead(5);

if(inputValue > 125) digitalWrite(9,HIGH);
else digitalWrite(9,LOW);
if(inputValue > 250) digitalWrite(8,HIGH);
else digitalWrite(8,LOW);
if(inputValue > 375) digitalWrite(7,HIGH);
else digitalWrite(7,LOW);
if(inputValue > 500) digitalWrite(6,HIGH);
else digitalWrite(6,LOW);
if(inputValue > 625) digitalWrite(5,HIGH);
else digitalWrite(5,LOW);
if(inputValue > 750) digitalWrite(4,HIGH);
else digitalWrite(4,LOW);
if(inputValue > 875) digitalWrite(3,HIGH);
else digitalWrite(3,LOW);
if(inputValue > 1000) digitalWrite(2,HIGH);
else digitalWrite(2,LOW);
}

Observant people will note that my code has an unfortunate little error: while my circuit theoretically measures resistance, lighting more LEDs as resistance goes up, what this code actually does is light more LEDs as resistance drops. This is easily fixed by swapping all greater-than symbols for less-than symbols in the IF statements.

Let’s watch this baby in action.

The key thing to note here is that as more and more LEDs are lit, they all get dimmer. This little problem had me banging my head against the wall until I thought it must be a simple power-drain problem with not enough current to light all the LEDs. Except this thing is running on a 500mA power supply, and there’s no way these things need 100mA a piece. The problem is actually one of the interaction between parallel and serial circuits, which I’ll talk about more in the next post.

Thomas

Tags:

One Response to “Analog input on digital circuits”

  1. [...] Brain Dump Inside the Mind of Thomas Robertson « Analog input on digital circuits [...]

Leave a Reply