Index
Definition
ADC stands for Analog-to-Digital Converter. It is an electronic component or subsystem that converts an analog signal, such as a voltage or current, into a digital signal that can be processed by a digital system, such as a computer or microcontroller.
ADC in Arduino
- Arduino Uno: The Uno has a 10-bit ADC, which means it can represent analog voltage levels with a resolution of 2^10 or 1024 discrete values. It has 6 analog input pins, labeled A0 to A5.
- Arduino Mega: The Mega has a 10-bit ADC as well, but it has 16 analog input pins, labeled A0 to A15.
- Arduino Due: The Due has a 12-bit ADC, which means it can represent analog voltage levels with a resolution of 2^12 or 4096 discrete values. It has 12 analog input pins, labeled A0 to A11.
- Arduino Nano: The Nano has a 10-bit ADC, with 8 analog input pins, labeled A0 to A7.
To read an analog voltage using the ADC on an Arduino, you would use the analogRead()
function in the Arduino IDE. This function takes as an argument the analog input pin number, and returns an integer between 0 and the maximum value of the ADC (1023 for a 10-bit ADC, or 4095 for a 12-bit ADC) representing the digital approximation of the analog voltage level.
Let’s explore analog input and output with an example.
Objective
Use a potentiometer to control the brightness of an LED.
Required Components:
- Arduino Uno
- LED
- 220-ohm resistor (for LED)
- Potentiometer (e.g., 10k-ohm)
- Breadboard and jumper wires
Circuit Setup:
- LED Circuit:
- Connect the longer leg (anode) of the LED to pin 9 on the Arduino through a 220-ohm resistor.
- Connect the shorter leg (cathode) of the LED to GND on the Arduino.
- Potentiometer Circuit:
- Connect one outer pin of the potentiometer to 5V on the Arduino.
- Connect the other outer pin to GND.
- Connect the middle pin (wiper) of the potentiometer to A0 (analog input pin 0) on the Arduino.
Code Explanation
In this example, we’ll read the potentiometer’s position using analogRead()
, and then use analogWrite()
to adjust the LED’s brightness based on the potentiometer’s input.
Let’s explore analog input and output with an example.
Code:
// Define pins
const int ledPin = 9; // LED connected to digital PWM pin 9
const int potPin = A0; // Potentiometer connected to analog pin A0
void setup() {
// Initialize the LED pin as output
pinMode(ledPin, OUTPUT);
}
void loop() {
// Read the value from the potentiometer
int potValue = analogRead(potPin);
// Map the potentiometer value (0 to 1023) to LED brightness (0 to 255)
int ledBrightness = map(potValue, 0, 1023, 0, 255);
// Set the LED brightness using PWM
analogWrite(ledPin, ledBrightness);
// Small delay to allow for a smoother transition
delay(10);
}
Code Breakdown:
- Variable Definitions:
ledPin
andpotPin
are set to specify the pins connected to the LED and potentiometer, respectively.
- Setup Function:
pinMode(ledPin, OUTPUT);
sets the LED pin as an output to control the brightness.
- Loop Function:
analogRead(potPin);
reads the voltage from the potentiometer, giving a value between 0 and 1023 (representing 0V to 5V).map(potValue, 0, 1023, 0, 255);
scales the potentiometer’s reading to a range from 0 to 255, whichanalogWrite()
uses to control brightness.analogWrite(ledPin, ledBrightness);
sets the LED brightness with PWM on pin 9 based onledBrightness
.
How PWM Works for Analog Output
Arduino Uno can’t produce true analog output, but PWM (Pulse Width Modulation) simulates it. PWM rapidly switches the LED on and off at different “duty cycles,” which changes how long it stays on compared to off in each cycle:
- 0% duty cycle = fully off (LOW).
- 50% duty cycle = half brightness.
- 100% duty cycle = fully on (HIGH).
Result
- Turning the potentiometer will vary the
potValue
read from analog input A0. analogWrite()
uses this mapped value to control the LED’s brightness. As you turn the potentiometer, the LED will dim and brighten smoothly.
This example shows how analogRead and analogWrite can control variable analog signals, giving you greater flexibility in interfacing sensors and controlling devices like LEDs
Other Inbuilt Functions in Arduino For ADC
analogReference(type)
: This function sets the reference voltage used for the ADC. The type can beDEFAULT
(the default voltage reference is used),INTERNAL
(the internal reference voltage of the Arduino is used), orEXTERNAL
(an external reference voltage is used).analogWrite(pin, value)
: This function writes an analog value (between 0 and 255) to a PWM (Pulse-Width Modulation) pin. The duty cycle of the PWM signal determines the average voltage output.analogReadResolution(bits)
: This function sets the resolution of the ADC to the specified number of bits. The default resolution is 10 bits.analogWriteResolution(bits)
: This function sets the resolution of the PWM output to the specified number of bits. The default resolution is 8 bits.
These are some of the most commonly used ADC-related functions in Arduino. There are other functions and libraries available as well for more advanced usage.