What is oversampling?
Oversampling is a process by which you can increase the effective resolution of a measurement by taking many samples and averaging them together. This article won't be going into the mathematics of why this works, but it will explain the process. It all begins with the sensor; a sensing element will almost always report its value as an analog voltage. In this context, analog means a real voltage value, as opposed to a digital value, which is quantified as a binary number. Since your computer only deals in binary information, at some point the voltage value needs to be digitized. This is the job of the analog-to-digital converter (ADC).
An ADC will convert analog voltage values into discrete digital values in a process called sampling. Often the maximum sampling rate is much higher than the rate of digital data that the user requires from the device, making oversampling a possibility. An ADC converts the analog values to digital values of a certain number of bits. For example, a 12-bit ADC produces 12-bit binary numbers as its output. An ADC increases in cost drastically as you add bits of resolution, which is another reason that oversampling is desirable. Again, we won't delve into the math behind it, but oversampling requires that you take four times the number of samples per bit of extra resolution gained. So in order to oversample a 12-bit ADC to a 16-bit result, you'd need to average together 256 samples (or 4^4) for a single measurement.
When does oversampling work best?
To be clear, oversampling will never be quite as good as having that many bits of resolution to start with. Oversampling works the best when the input value is constantly changing by small amounts. This ensures that the sum accumulated within the sampling period will vary enough to create a higher resolution value in between the original values that the input was bouncing between. Luckily, most signals already have small variations in the form of noise. As long as the noise changes randomly from sample to sample by an amount close to the smallest detectable change, (a binary value of 1) and as long as there is an equal chance of the input falling on the value above or the value below the previous value, it can be considered "white noise" and will aid the oversampling process. In some systems where this variation isn't present, artificial noise will be added in a process called "dithering" in order to make oversampling effective.
The numbers in this example have been simplified to make it easier to understand.
Suppose you have a 4-bit ADC connected to a temperature sensor whose range is 0-100°C. It samples the sensor once per second, but you only require hourly readings from the sensor, so you decide to oversample it to effectively get 8 bits of resolution. Before oversampling, this terrible 4-bit ADC can only produce values from 0 to 15, since 15 is the highest number that can be expressed by a 4-bit binary number. To convert that to temperature in our program, we'd take the 4-bit value, divide by 15, and multiply by the full range of the sensor (100°C). By taking the difference between two readings that differ by a digital value of 1, we can find that the temperature resolution of the 4-bit reading is 6.67°C.
In order to oversample to 8 bits, we need to add 256 consecutive samples together and "average" them. Although since we're working with binary numbers, we actually have to shift the binary sum right by the number of extra bits (4), which is the same as dividing by 16 in this case. Suppose during the 256 samples, the sensor is reading 4-bit values between 13 and 15 (corresponding to a temperature of 86.67 to 100°C) and the sum of those 256 samples is 3498. We shift by 4 in order to get the binary value 218. To convert this to temperature, we divide it by the highest 8-bit number (255) and multiply by the full range of the sensor (100°C) resulting in a measurement of 85.49°C. In order to determine the new resolution, we take the next highest 8-bit number (219), convert to temperature (85.88°C) and take the difference from our other reading to get 0.39°C. Compared to our previous resolution of 6.67°C, this is a major improvement.