Lab 2 Analog Input and Output: Components
Lab 2 Analog Input and Output: Components
Lab 2 Analog Input and Output: Components
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:
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() {
Procedure:
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:
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);
}
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
void setup() {
Serial.begin(9600); // initialize serial communications at 9600 bps:
}
void loop() {
sensorValue = analogRead(analogInPin); // read the analog in value:
4
// print the results to the serial monitor:
Serial.print("sensor = ");
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
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)