This Arduino sketch reads analog voltage values from four input pins (A0, A1, A2, A3) and outputs these values to the serial monitor. Additionally, it controls an LED connected to digital pin 13 based on the voltage value from A0.
- Reads voltage values from analog pins:
A0,A1,A2, andA3. - Outputs the voltage readings to the serial monitor.
- Turns an LED ON or OFF based on the voltage value from
A0.
- Connect analog sensors or voltage sources to the following pins:
A0A1A2A3
- Connect an LED to digital pin
13with a current-limiting resistor.
- Pin Initialization:
- Analog pins
A0,A1,A2,A3are set as inputs. - Digital pin
13is set as output for the LED.
- Analog pins
- Serial Communication:
- The serial monitor is initialized at
9600baud rate. - Voltage readings are printed as
Variable_1,Variable_2,Variable_3, andVariable_4.
- The serial monitor is initialized at
- LED Control:
- The LED on pin
13is turned ON if the voltage read fromA0is below 10. Otherwise, it is turned OFF.
- The LED on pin
int led = 13;
int voltage = A0;
int voltage1 = A1;
int voltage2 = A2;
int voltage3 = A3;
int vol = 0;
int vol2 = 0;
int vol3 = 0;
int vol4 = 0;
void setup() {
pinMode(voltage, INPUT);
pinMode(voltage1, INPUT);
pinMode(voltage2, INPUT);
pinMode(voltage3, INPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
vol = analogRead(voltage);
Serial.print("Variable_1: ");
Serial.println(vol);
vol2 = analogRead(voltage1);
Serial.print("Variable_2: ");
Serial.println(vol2);
delay(99);
vol3 = analogRead(voltage2);
Serial.print("Variable_3: ");
Serial.println(vol3);
delay(99);
vol4 = analogRead(voltage3);
Serial.print("Variable_4: ");
Serial.println(vol4);
delay(99);
if (vol < 10) {
digitalWrite(led, HIGH);
} else {
digitalWrite(led, LOW);
}
}