====== Analog to Digital Conversion ======
So far, we've been focused solely on analog electronics. Being alive in the 21st century, you've no doubt noticed that there are a whole lot of digital electronic devices around. You can't just jam a wire from your circuit board into a computer and have it transmit data, so we sometimes need a way to map a range of analog values into discrete digitized values. Digital devices also tend to react poorly to being subjected to voltages higher or lower than their design mandates.
You've actually already done a sort of digital conversion! If you think about it, a comparator circuit essentially has a one-bit output, so you've already used them to distinguish between voltages above and below a threshold. We're going to continue the trend throughout today's lab.
===== Digital Logic Signals: =====
When working with digital circuits, voltages are essentially mapped to binary values of either **H**igh **(1)** or **L**ow **(0)**. There are quite a few different standards out there for what voltages are mapped to which, but we'll work with a 5V standard:
* 5V standard (Used by Transistor Transistor Logic (TTL) and some Complementary Metal Oxide Semiconductor (CMOS))
* Above 2V is **H**IGH
* Below 0.8V is **L**OW
* Between 0.8V and 2V is UNDEFINED (Don't do this!)
What about other logic families?
3.3V is the other dominant logic family used by modern devices, but there are other standards for even lower power consumption. The trade off is that they are much less tolerant of any noise
| {{:phylabs:lab_courses:phys-226-wiki-home:spring2020-lab-10-logic-gates:logic_ranges.png|}} |
| Image courtesy of a Texas Implements application report |
==== Analog to Unary ====
To start off, let's not worry about how to encode data into bits, but instead how we can discriminate between a range of values. Consider the circuit below:
This is just a series of three comparators, tied to an increasing set of thresholds.
^ $V_{in}$ ^ $V_{out}$ ^
| < 1 V | 000 |
| 1V - 2V | 001 |
| 2V - 3V | 011 |
| >3V | 111 |
Build the circuit and test its behavior
This is all well and good for breaking up a range of values coarsely, but what if we want more precision? To divide the same voltage range into 0.5V increments would take the following circuit:
We've slightly more than doubled the number of comparators needed to double our resolution here. Since getting up to 8 bits would take 255 comparators, this is obviously not something we want to build by hand! Furthermore, we don't want to have to run a cable with that many wires to our computer. You can use some digital logic to condense your outputs, but thankfully there are other options too.
==== Successive Approximation ====
What if we were willing to trade a bit of time to make it so we had to use fewer parts? This is the idea behind a Successive Approximation ''SA'' approach. The general setup is as follows:
- Make a voltage divider that successively halves the reference voltage
- e.g. if we have an 8-bit scale, make a voltage divider with outputs of 4V, 2V, and 1V
- Hook up these outputs to a [[phylabs:lab_courses:phys-226-wiki-home:lab_9_10_audio_project_alt:start#preparing_a_mixer|summing amplifier]] with switches that can toggle the connections on and off.
- Add an [[phylabs:lab_courses:phys-226-wiki-home:lab_6_op_amps_i:start#inverting_amplifiers|inverting amplifier]] to flip the sign of the output
- Feed that signal into one half of a comparator
- Feed your analog signal to the other half of a comparator
A circuit to do this is shown below:
Usually, this design would have some logic circuits in it to toggle the switches (or more realistically, switch some transistors). For practice, you will be doing the logic yourself. You'd start by flipping the first (4V) switch. If the comparator output goes high, it means that the input is less than 4V. In that case, flip the switch off again. Otherwise, repeat the process for each successively lower voltage. When you're done, the state of your switches **IS**
the digital conversion of your input.
Build the circuit, and test a few different inputs.
Sure, we used three op-amps/comparators. But, we don't need any more to scale up the precision! All we need is to keep splitting our reference voltage into smaller increments, add a few switches, and take more time.
----
===== Dedicated ADCs =====
Nowadays, you can find a ridiculous variety of chips dedicated to converting analog signals to digital ones. At the time of writing this, [[https://www.digikey.com/en/products/filter/data-acquisition/analog-to-digital-converters-adc/700 | Digikey]] has 14,210 parts listed. Some microcontrollers (e.g. an Arduino) have ADCs built into the chip along with the rest of the device, but others (like Raspberry Pis) will need you to supply your own.
==== On interfaces ====
One of the first questions you'll need to ask yourself when choosing an ADC is how you want your digital data presented. The versions we built essentially had parallel outputs: all outputs can be measured at the same time. The downside is that you need one wire for each bit, which you'll find can eat up a lot of the input/output pins on a microcontroller. The upside is that it can update quickly.
The other option is some form of serial output: you have a single connection for data from your ADC to your device. Now, you have the downside that your signal is stretched out in time. In practice, you'll usually need to have both a data connection and a clock connection to let the receiving device know when you're sending it a new bit. On the upside, you need only a pair of wires to convey the output.
As a rule of thumb, you should use an ADC with a serial output as the default. Parallel outputs are good for either higher speed readouts or ease of implementation. The latter is because you don't need to do any work with clock interfaces or communication protocols; you can just connect your outputs to a bunch of pins and read the values when you want.
----
==== Our ADC: The ADC0804 ====
We've chosen an 8-bit ADC with parallel outputs for this lab. The basic setup of our circuit is shown below:
| |
| Our basic analog to digital converter. Don't panic, it isn't as bad as it looks at first. Taken from the [[https://www.ti.com/document-viewer/ADC0804-N/datasheet#application_and_implementation/lit82187873|Texas Instruments datasheet]]. |
There are a few things to note here:
- Lots of pins are grounded. Most of them do have functionality, but we just aren't using it right now.
- The digital outputs run from pins 11 to 18. However, pin 11 is the Most Significant Bit ''MSB'' (i.e. it doesn't toggle until halfway though the analog range) and pin 18 is the Least Significant Bit ''LSB''.
- This isn't likely to be problematic here, but when working with an ADC with a serial output you'll need to check the order that bits are sent in.
- Pins 19 and 4 are used as part of a [[phylabs:lab_courses:phys-226-wiki-home:lab_7_op_amps_ii:start#schmitt_triggersback_to_op-amps|Schmitt Trigger]] clock.
- This is why we teach you about analog circuits first. They can (and will) show up when working with digital devices.
- We manually toggle a pin in order to start a measurement (or ($\overline{WR}$)ite the output).
- After the ADC has finished measuring, it will change the $\overline{INT}e\overline{R}$rupt from high to low.
- Often you'd connect both of these to a microcontroller.
- By connecting the two together, we're basically making our chip start a new measurement every time the last one finishes.
Build the circuit and test its behavior.
==== Microcontroller Inputs ====
Now we get to the point of all of this, actually reading the digital values with a device. For this, we're going to use a Raspberry Pi Pico running MicroPython. This isn't a course on coding, so we've got a basic program already in place. If you know Python, you'll be able to dig through what it is doing a bit.
Connect the pins of your Pico to the digital outputs of your ADC as shown below
| |
| |
After connecting the device, plug the Pico's USB cable into the lab computer. Open the ''Thonny'' application, and click on the hamburger menu in the lower right corner. Select ''MicroPython (Generic)''. Use the menu to open a new file, selecting the ''This Computer'' option. Open the ''Readout.py'' file that's on the desktop, or download a new copy below. Finally, run the script by pushing the green button (or pushing ''F5'')
FILE DOWNLOAD
With this, the Pico will start printing out what the individual bits are, and then what the Analog equivalent is. Congratulations! You've digitized a signal. You can log it, plot it, whatever your heart desires.