Maker.io main logo

Learn to Use the Arduino’s Analog I/O

2017-02-20 | By All About Circuits

License: See Original Project Arduino

Courtesy of All About Circuits

An analog signal can take on any number of values. A digital signal, on the other hand, has only two values: HIGH and LOW. The Arduino has a built-in analog-to-digital converter (ADC) that measures the value of analog signals. The ADC converts the analog voltage into a digital value. The function used in order to obtain the value of an analog signal is analogRead(pin). This function converts the value of an analog input pin’s voltage and returns a digital value from 0 to 1023, relative to the reference value. Most Arduinos have a reference of 5V, 15V on an Arduino Mega, and 7V on the Arduino Mini and Nano. The pin number is its only parameter.

The Arduino does not have a digital-to-analog converter (DAC) built-in, but it can do pulse-width modulation (PWM) a digital signal used to achieve some of an analog output’s functions. The function analogWrite(pin, value) is used to output a PWM signal. The pin number used for the PWM output is pin. A number proportional to the duty cycle of the signal is listed as value. When value = 0, the signal is always off. When value = 255, the signal is always on. The PWM function works on pins 3, 5, 6, 9, 10, and 11 on most Arduino boards. The frequency of the PWM signal on most pins is approximately 490 Hz. On the Uno and similar boards, pins 5 and 6 have a frequency of approximately 980 Hz. Pins 3 and 11 on the Leonardo also run at 980 Hz.

You can use the function: map (value, fromLow, fromHigh, toLow, toHigh) in order to map an analog input value. This will range from 0 to 1023 to a PWM output signal, which will range from 0 - 255. This function has five parameters, the first is the variable in which the analog value is stored;  the others are 0, 1023, 0 and 255 respectively.

Experiment 1: Controlling the Brightness of LED

In this experiment we will control an LED’s brightness with a PWM signal on an analog output pin

Hardware Required

Wiring Diagram

As shown in the diagram below, an LED is connected to pin 2 on the Arduino. To change the LED’s brightness, the program will vary the duty cycle of the PWM signal output of pin 2.

 

Code

Copy Code
                  const int pwm = 2 ;         //initializing pin 2 as ‘pwm’ variable
void setup()
{
    pinMode(pwm,OUTPUT) ;     //Set pin 2 as output
}
void loop()
{
    analogWrite(pwm,25) ;         //setting pwm to 25
    delay(50) ;             //delay of 50 ms
    analogWrite(pwm,50) ;    
    delay(50) ;
    analogWrite(pwm,75) ;
    delay(50) ;
    analogWrite(pwm,100) ;
    delay(50) ;
    analogWrite(pwm,125) ;
    delay(50) ;
    analogWrite(pwm,150) ;
    delay(50) ;
    analogWrite(pwm,175) ;
    delay(50) ;
    analogWrite(pwm,200) ;
    delay(50) ;
    analogWrite(pwm,225) ;
    delay(50) ;
    analogWrite(pwm,250) ;
}

Experiment 2: LED Brightness Control Using Potentiometer

In this experiment, we will control the brightness of the LED using a potentiometer. We will the analogRead() function to read a voltage and the analogWrite() function to output a PWM signal, whose duty cycle is proportional to the analog voltage.

Hardware Required

Wiring Diagram

Connect the circuit as shown below. When you rotate the potentiometer, the voltage on pin A0 will change. The program will then change the duty cycle of the PWM signal on pin 2, changing the brightness of the LED.

 

 

Code

Copy Code
                  const int pwm = 2 ;     //naming pin 2 as ‘pwm’ variable 
const int adc = 0 ;     //naming pin 0 of analog input side as ‘adc’
void setup()
{
    pinMode(pwm,OUTPUT) ;     //setting pin 2 as output
}
void loop()
{
    int adc  = analogRead(0) ;     //reading analog voltage and storing it in an integer
    adc = map(adc, 0, 1023, 0, 255); 
/*
----------map funtion------------the above funtion scales the output of adc, which is 10 bit and gives values btw 0 to 1023, in values btw 0 to 255 form analogWrite funtion which only receives  values btw this range
*/
    analogWrite(pwm,adc) ;
}

 

 

 

制造商零件编号 SSL-LX5093ID
LED RED DIFFUSED T-1 3/4 T/H
Lumex Opto/Components Inc.
制造商零件编号 A000066
ARDUINO UNO R3 ATMEGA328P BOARD
Arduino
制造商零件编号 CF14JT220R
RES 220 OHM 5% 1/4W AXIAL
Stackpole Electronics Inc
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum