Completely Different Languages!
In the beginning, as the universe burst into existence, so did a nearly infinite set of continuous streams of information. There were exactly zero discontinuous streams of discrete, digital events. However, once digital computers started popping up on the third large rock orbiting our rather ordinary star, many of those computers started digitizing information streams into digital samples. They had to do that so the data could be consumed by the software running on their hardware-clocked lock step CPUs. But that digital data is not real. The real universe is an analog place; a place where infinitely and immeasurably small or large changes occur in an infinite number of infinitely and immeasurably small intervals between our smallest human-measurable quanta of time. By definition any computer data stream that samples information from the universe is inaccurate.
Computer data streams are accurate because they necessarily sample the continuous information flow of the universe at a non-infinte rate. Computer data is also inaccurate because samples taken of infinitely variable information must be measured and quantized in some way.
Suppose there existed a thermometer that could accurately measure temperatures in degrees (on any popular scale, that doesn’t matter) to 1 million decimal places of accuracy. Also, suppose you want to take the temperature of an object with this thermometer and this object at that instant is exactly 3.0 degrees. Would that thermometer provide a precise temperature measurement? Yes! 3.0…0 (with one million zeroes) accurately represents 3.0. Now what if the actual temperature was 0.000…0001 (with a million and 1 zeros after the decomal point) of a degree greater? The thermometer would still read 3.0, but now it would be imprecise. In general any time you measure something from the universe, no matter what accuracy you use, you are recording an approximation of the real value. Also note the practical concern here. Since the thermometer hardware is only accurate to 1 million decimal places, recording the values with a million and 1 decimal places would not help the accuracy in the above case (the thermometer would still incorrectly say 3.0).
To summarize, there are 2 significant factors in the discussion above that affect how well a stream of digital data represents a real world stream of information. How accurately you measure a sample of data is significant, and that is often called the sampling accuracy, or sampling resolution. Accuracy is affected by the hardware senosr’s resolution and the numeric precision used for recording. How often you sample the information stream is also significant, and this is typically called the sampling frequency.
Whenever you quantize a continuous stream of infinitely variable information from the universe and turn it into a discontinuous, discrete data stream, you need to ask how well does that digital data represent the original analog information. Are the sampling resolution and sampling frequency sufficient, appropriate, or overkill for your purposes.
Sensor resolutions are typically measured in significant bits. A temperature sensor with 10 bits of accuracy, for example, could only record whole number values between 0 and 1023 (or perhaps between -512 and 511). Perhaps this could be sufficient for some household applications but might be too imprecise for some industrial applications. If the sensor can only deliver 10 bits of accuracy, then a 10-bit binary number would be all that was needed to accurately record each sample. Using a typical 16 bit, 32-bit or 64-bit integer for this would be overkill, but it might be a convenient and efficient size to use on modern computer architectures. An 8-bit integer on the other hand (e.g., a C language uint8_t type) could not represent all of the possible values from the sensor hardware.
Let’s take a look at how this might work in practice…
The black line in the diagram above represents an analog information source in the real world. You could imagine it to be the temperature of an object, or water levels in a reservoir over time. To create a stream of digital data from this source, you need a sensor that can conduct measurements. In this diagram the measurements are the lengths of the blue lines above or below the horizontal axis. The measurement values are represented by the blue squares. As noted above the accuracy of the sensor hardware and the precision of the numerical recording of the sensor measurements are relevant. Also relevant is the sampling frequency. Here time is represented horizontally and the sampling frequency is the number of measurements recorded over the time period shown. Notice that at this frequency, the digital data stream completely misses the large drop in the real world value between the eighth and ninth samples. In general you need to sample at approximately twice the frequency of the smallest fluctuations that you want to be certain to capture.
What kind of hardware do you need for this?
Since most computers are incapable of working with analog data, you generally need some sort of Analog-to-Digital Convertor (ACD) for analog input, and a Digital-to-Analog Convertor (DAC) for output. There are inexpensive computer chips you can use for each of these purposes. Some small microcontrollers specialize in handling analog IO, like the Arduino line of single board computers (SBCs). Arduinos have built-in 10-bit ADC circuitry. They also have Pulse Width Modulation (PWM) circuitry and libraries to enable them to simulate analog output without real DAC circuitry. Other SBCs, like the Raspberry Pi computers have restricted abilities to do analog output (also via PWM) but they simply cannot sample analog input signals without additional hardware. One option for a Raspberry Pi to receive analog input is to connect an Arduino to the Pi as a slave. See this local article for details on that.
Pulse Width Modulation is cool.
The Arduinos all have multiple GPIO pins that support PWM in hardware for simulated analog output. The Raspberry Pi computers tend to have only one or two pins with hardware for that. However, the Raspberry Pi computers can do a pure-software version of PWM on any of its GPIO pins. Would you like to know more about PWM? There’s a local PWM article that can teach you about it.
Using an ADC chip (for analog input) on a Raspberry Pi
Another alternative for analog input on the Raspberry Pi is to use an ADC chip, like the MCP3008. It produces 10-bit samples, and it communicates over the SPI serial protocol (so you connect it to SPI pins on your Raspberry Pi). You can buy an MCP3008 chip for about $2-$3USD each, if you shop carefully. There’s a great guide on the Adafruit site that shows how to use one (and you might want to pay a little more to buy it there too to support their efforts in writing guides like that).