Lab 2 Analog Input and Output: Components

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

LAB 2 ANALOG INPUT AND OUTPUT

Components:
Experiment 1 :
1 x Arduino microcontroller board

Experiment 2 :
1 x Arduino microcontroller board
1 x LED
1 x 330 ohm resistor

Experiment 3:
1 x Arduino microcontroller board
1 x LDR
1 x 10kohm resistor

Experiment 4:
1 x Arduino microcontroller board
1 x Potentiometer
1 x LED
1 x 330 ohm resistor

Procedure:

Experiment 1: Arduino Read and Write using Serial Communication

Build a program which receive the character value from Serial Monitor and Return the value back
to Serial Monitor.
Steps:
1. Connect Arduino
2. Upload the following program.
3. Open Serial Monitor.( Sellect Both NL &CR and select the right Baud Rate)
4. Insert character and check the return character.

A. Hardware setup

B. Software setup

void setup() {
Serial.begin(4800); //set baud rate to 4800 bps
Serial.println("Data Available:"); //print sentences once

1
}
void loop() {

if(Serial.available()) //check if serial data available


{
delay(10);
char data=Serial.read(); // read serial. store in 'data'
Serial.print(data); // print 'data' value
} //repeat loop
}

Procedure:

Experiment 2: Lighting LED using PWM

Build a program using any PWM pins to demonstrate the brightness control of LED. Write the
PWM value on Serial Monitor.

A. Hardware setup

B. Software setup

const int analogOutPin = 9; // Analog output pin that the LED is attached to
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
Serial.begin(9600);
}

void loop() {
for (int i=0; i<255;i++)
{
analogWrite(analogOutPin, i);
Serial.println(i);
delay(5);
}
for (int i=255; i>0;i--)
{
analogWrite(analogOutPin, i);
Serial.println(i);
delay(5);
}

2
Procedure:

Experiment 3: Reading Analog Value using LDR

Read the Analog value from pin A0 and write the value on the serial monitor with Baud rate 9600.

A. Hardware setup

B. Software setup

void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:


void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}

Experiment 4: LED brightness manipulator using Potentiometer

Build a program to manipulate the brightness of LED using Potentiometer as the


manipulator. Print the sensor value and output value accordingly.

A. Hardware setup

3
Analog input, analog output, serial output

Reads an analog input pin, maps the result to a range from 0 to 255
and uses the result to set the pulsewidth modulation (PWM) of an output pin.
Also prints the results to the serial monitor.

The circuit:
* potentiometer connected to analog pin 0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
* LED connected from digital pin 11 to ground

B. Software setup

const int analogInPin = A0; // Analog input pin that the potentiometer is
attached
const int analogOutPin = 11; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the pot


int outputValue = 0; // value output to the PWM (analog out)

void setup() {
Serial.begin(9600); // initialize serial communications at 9600 bps:
}

void loop() {
sensorValue = analogRead(analogInPin); // read the analog in value:

// map it to the range of the analog out:


outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);

4
// print the results to the serial monitor:
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);

// wait 2 milliseconds before the next loop


// for the analog-to-digital converter to settle
// after the last reading:
delay(2);

Lab Report and Assessment

Build a system which automatically control the brightness of 3 LEDs using LDR to simulate
the Street Light system (which brighter when surrounding is dark).
Then, Include a potentiometer to manually control the brightness as a manual brightness
control setting.
The auto mode (mode 1) and manual mode (mode 2) is toggled using Serial input.
Display operation mode and brightness value on the serial monitor.

The report need to be submitted during the following week before the class begin.
The report should consist of
1. Introduction
2. Schematics
3. Flowchart
4. Discussion
5. Conclusion
(page1: cover page, names, page2: intro and schematics, page3: code flowchart, page 4:
discussion n conclusion)

You might also like