You are on page 1of 6

ILOILO SCIENCE AND TECHNOLOGY UNIVERSITY

La Paz, Iloilo
ELX 12 – ECE Workshop 2

Name : ___________________________________________________Year & Sec.:________________

Experiment 5: ANALOG INPUT AND OUTPUT

Objectives
At the end of the experiment the students were able to:

1. Identify the Arduino microcontroller analog input and output pins.


2. Use analog I/O pins in programming.
3. Identify Arduino programming commands for declaring analog I/O pins.
4. Interface tact switches, potentiometer, LDR and LEDs on the Arduino board.

Equipment and Materials


1 pc Arduino Board
1 pc USB Standard A-B Cable
1 set Connecting Wires
1 set Desktop computer/Laptop computer/Android device
1 pc Bread Board
1 pc Red LED (or any color available)
1 pc 10 KΩ Resistors
1 pc 10k ohm Potentiometer
1 pc 220 ohm Resistor

Discussion

An analog signal is one that can take on any number of values, unlike a digital signal which has
only two values: HIGH and LOW. To measure the value of analog signals, the Arduino has a built-in
analog-to-digital converter (ADC). The ADC turns the analog voltage into a digital value. The function that
you use to obtain the value of an analog signal is analogRead(pin). This function converts the value of the
voltage on an analog input pin and returns a digital value from 0 to 1023, relative to the reference value.
The reference is 5V on most Arduinos, 7V on the Arduino Mini and Nano, and 15V on Arduino Mega. It
has one parameter which is the pin number.

The Arduino does not have a built-in digital-to-analog converter (DAC), but it can pulse-width
modulate (PWM) a digital signal to achieve some of the functions of an analog output. The function used
to output a PWM signal is analogWrite(pin, value). pin is the pin number used for the PWM output.
value is a number proportional to the duty cycle of the signal. When value = 0, the signal is always off.
When value = 255, the signal is always on. On most Arduino boards, the PWM function is available on
pins 3, 5, 6, 9, 10, and 11. The frequency of the PWM signal on most pins is approximately 490 Hz.
To map an analog input value, which ranges from 0 to 1023 to a PWM output signal, which
ranges from 0 - 255, you can use the map(value, fromLow, fromHigh, toLow, toHigh) function. This
function has five parameters, one is the variable in which the analog value is stored, while the others are
0, 1023, 0 and 255 respectively.

Procedures:
1. Open Arduino IDE. Encode the sketch below:
const int pwm = 3 ; //initializing pin 3 as 'pwm'
variable
void setup()
{
pinMode(pwm,OUTPUT) ; //Set pin 3 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) ;
}

2. Prepare the breadboard. Connect connect components on the microcontroller as shown on Figure
5.1. This time we will be using Arduino Board, use RED LED and 220 ohms resistor.
Figure 5.1.Connection of Power and Ground Using Wires

3. Once the program has been successfully verified, connect the Arduino to the computer using a USB
standard A-B cable. Specify the COM port where the Arduino is connected in Tools > Serial Port >
[COM port used].

4. Specify the board to use by checking the appropriate Arduino board under Tools > Board.

5. Click the (Upload) button. This will now compile and burn the program to the Arduino. [ Answer
guide question number 1]

6. Repeat step 1 with these codes:

const int pwm = 3; //naming pin 3 as 'pwm' variable


const int sensorPin = A0; // analog pin A0 as sensor pin
int adc = 0; //variable to store the value coming from the
sensor
void setup()
{
pinMode(pwm,OUTPUT); //setting pin 3 as output
}
void loop()
{
adc = analogRead(sensorPin); //reading analog voltage and
// storing it in an integer
adc = map(adc, 0, 1023, 0, 255);
/*
----------map funtion------------the above function scales the
output of adc, which is 10 bit and gives values between 0 to 1023, in
values between 0 to 255 form analogWrite funtion which only receives
values between this range */
analogWrite(pwm,adc) ;
}

7. Connect your Arduino board and the components. Refer to Figure 5.2 for connection.

Figure 5.2. A microcontroller with potentiometer.

8. Do steps 3 to 5. After uploading, vary the resistance of the potentiometer by rotating its knob.
Observe the red LED while you are rotating the knob. Answer guide question number 2.

Guide Questions:

1. Describe the behavior of the LED that you connected to the Arduino after uploading the program on
step 5.
Answer:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________

2. Describe the behavior of the LED that you connected to the Arduino after while you are varying the
resistance of the potentiometer.
Answer:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Review Questions:
1. What is analog input? How about analog output?
Answer:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
2. What is the purpose of the command “map”?
Answer:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
3. What is the difference between the analog input to the digital input?
Answer:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________

4. What does the function analogRead() do? Briefly explain where it is applied.
Answer:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________

5. Where can we use the analogWrite()? Explain.


Answer:
_____________________________________________________________________________________
_____________________________________________________________________________________
________________________________________________________________________________

Challenge Yourself:
1. Using a potentiometer as your analog input and two RED LED as digital output, by rotating the knob
of the potentiometer, the LEDs will alternately light up gradually (fading). When the knob is positioned at
maximum, one LED remains lit up while the other is off and if it is positioned on minimum, the other LED
will remain lit up while the first one is now off. Submit a video that shows you turning the potentiometer
knob at least 3 times and your device performing the task correctly. Include yourself in the video. Here is
a video showing a sample execution:
https://drive.google.com/file/d/1FWpY_ZNKdEiRv_FZ5hd0QE-uiL3N_xJ_/view?usp=sharing

Paste, in the box below, a screen shot of your sketch program.

Conclusion (answer the objectives):


_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________

You might also like