Task 1
Arduino Mega
Arduino Mega is AtMega2560 microcontroller-based development board with 54 digital pins and 16 analog channels along with various serial communication protocols such as UART, SPI and I2C. Arduino can be programmed by Arduino IDE and code can be uploaded by USB. Arduino operating voltage is 5V.
Serial communication
In Arduino, UART module can be used for serial communication. In UART serial transfer of data the data is transferred serially but asynchronously. The baud rate which is number of bits transferred per second of transmitter and receiver must be same for effective communication. The Tx and Rx pins in Arduino used for serial communication.
ADC Module
ADC module is required to measure the analog values. The analog input voltage is converted to discrete value and using that values the corresponding input voltage can be determined. There are 16 analog channels in Arduino A0 – A15, which can be used to read analog input of 0 V – 5 V.
Connecting photocell with Arduino
Photocell is connected to Arduino as a voltage divider circuit in which the voltage is taken across the series resistor connected with photocell.
Task 2
Circuit diagram
Here AI1 and AI2 pins are A1 and A2 and DI1 is digital pin 13 and LED pins are pwm pins 3,5 and 6
The code is as below
const int AI1=A1; // Declare pins
const int AI2=A2;
const int DI1=13;
const int AO1=3;
const int AO2=5;
const int AO3=6;
void setup()
{
pinMode(DI1, INPUT); // Configure pin 13 as input
Serial.begin(9600); // Initialize UART with baud rate 9600bps
}
int a1,a2, period;
float v;
void loop()
{
Serial.print(“Digital input is:”); // Print digital input to serial monitor
Serial.println(digitalRead(DI1));
if(digitalRead(DI1)==0) // If input is low
{
Serial.print(“Analog input 1 is:”); // Print analog input 1 to serial monitor
Serial.println(analogRead(AI1));
a1=analogRead(AI1); // Read potentiometer value
period=map(a1,0,1023,100,1000); // Convert to required delay between 100 ms to 1000 ms
analogWrite(AO1,255); // Led 1 is ON with full brightness
analogWrite(AO2,0);
analogWrite(AO3,0);
delay(period); // delay determined by potentiometer
analogWrite(AO1,0); // Led 1 is off
delay(period); // delay determined by potentiometer
analogWrite(AO2,255); // Led 2 on
analogWrite(AO1,0);
analogWrite(AO3,0);
delay(period); // delay determined by potentiometer
analogWrite(AO2,0); // Led 2 off
delay(period); // delay determined by potentiometer
analogWrite(AO3,255); // Led 3 on
analogWrite(AO1,0);
analogWrite(AO2,0);
delay(period); // delay determined by potentiometer
analogWrite(AO3,0); // Led 3 off
delay(period); // delay determined by potentiometer
}
else // If input is high
{
a2=analogRead(AI2); // Read LDR output
v=(a2*5.0)/1023.0; // Convert to voltage
Serial.print(“Analog input 2 is:”); // Print analog input 2 (voltage) to serial monitor
Serial.println(v);
if(v<2) // If voltage is less than 2
{
analogWrite(AO1,255); // Only 1 LED on
analogWrite(AO2,0);
analogWrite(AO3,0);
}
else if(v>2 && v<4) // If voltage is between 2V and 4V
{
analogWrite(AO1,255); // Two LEDs on
analogWrite(AO2,255);
analogWrite(AO3,0);
}
else // If voltage greater than 4 V
{
analogWrite(AO1,255); // All LEDs on
analogWrite(AO2,255);
analogWrite(AO3,255);
}
}
}
Task 3
Simulation diagram is as below:
When ditial input is 0 , LEDs will blink sequentially
When input is 1 and analog input from LDR is less than 2 V