Question.2
Sam wants to read the proximity sensor value, which provides a voltage output between 0V and 12V for a distance range of 0cm to 10 cm shown below.

Analyze the code and circuit. What will be the voltage on Arduino pin A0?

void setup() {
Serial.begin(9600);
}
void loop() {
int analogValue = analogRead(A0);
Serial.println(analogValue);
while(1);
}
An ADC converts analog signals (temperature, voltage) into a digital value.
ADC is used for reading voltage output from temperature sensors (e.g., LM35), light sensors (e.g., LDR), and to read potentiometers, etc.

How many digital values (steps) can an ADC use to represent an input analog signal.
Higher resolution gives more precise readings.
ADC Steps = 2 ^ n
where, n = number of ADC bits

Minimum voltage change ADC can detect = AVref / number-of-steps.
= 5 / 1024
= 4.882 mV
The GIF below shows how different resolutions of ADC (8-bit, 10-bit- 12-bit, and 16-bit) vary the digital OUTPUT for input analog voltage change.
AVref = 5V

AVref = 3.3V

The number of times the ADC measures (samples) the analog signal per second.
Arduino UNO can achieve a maximum sampling rate of 15 kHz.

Higher sampling rate results in a more accurate digital representation of the input signal.
Generally, in controllers (AVR, PIC) following types of ADC registers are present.
Considering AVR controllers:
| Register | Description |
|---|---|
| ADMUX | Selects the reference voltage and the ADC input channel (A0 to A7) |
| ADCSRA | Controls the ADC, enabling it, starting conversions, and setting the prescaler. |
| ADCL & ADCH | Holds the bits of the ADC result. |
NOTE: ARM and RISC-based 32-bit controllers have more add-on registers (e.g., conversion control registers, sampling time registers, interrupt control registers, etc).
Most microcontrollers have a single ADC unit shared across multiple input channels. Multiplexers allow one channel to be read at a time.
Function | Description |
|---|---|
analogRead(pin) | Reads the analog value from a specified pin (returns a value between 0 and 1023 for 10-bit ADC). |
analogReference(type) | Sets the reference voltage for the ADC. Options include DEFAULT, INTERNAL, EXTERNAL, etc. |
1. Potentiometer ADC Reading

void setup() {
Serial.begin(115200);
}
void loop() {
int val = analogRead(A0);
Serial.println(val); // prints 0 to 1023
delay(100);
}2. Potentiometer Voltage Reading

void setup() {
Serial.begin(115200);
}
void loop() {
int val = analogRead(A0);
float voltage = sensorValue * (5.0 / 1023.0); // convert to voltage
Serial.println(voltage); // prints 0 to 5.0
delay(100);
}
3. Change ADC reference voltage

void setup() {
Serial.begin(115200);
// Set ADC reference to internal = 1.1V
analogReference(INTERNAL);
}
void loop() {
int val = analogRead(A0);
float voltage = sensorValue * (1.1 / 1023.0);
Serial.println(voltage); // prints 0.0 to 1.1
delay(100);
}