Tutorial #4A: analogRead Serial
Many signals from sensors, such as photoresistors, pressure sensors, etc. are analog signals,
i.e., continuously varying from 0 to 5 volts.
The analogRead() function converts this voltage to a 10-bit digital value, from 0 to 1023.
i.e., continuously varying from 0 to 5 volts.
The analogRead() function converts this voltage to a 10-bit digital value, from 0 to 1023.
/*
ReadAnalogVoltage
Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor.
Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
*/
void setup() { // the setup routine runs once when you press reset:
Serial.begin(9600); // initialize serial communication at 9600 bits per second:
}
void loop() {
int sensorValue = analogRead(A0); // read the input on analog pin 0: sensorValue is a local variable
float voltage = sensorValue * (5.0 / 1023.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
Serial.println(voltage); // print out the value you read:
}
ReadAnalogVoltage
Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor.
Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
*/
void setup() { // the setup routine runs once when you press reset:
Serial.begin(9600); // initialize serial communication at 9600 bits per second:
}
void loop() {
int sensorValue = analogRead(A0); // read the input on analog pin 0: sensorValue is a local variable
float voltage = sensorValue * (5.0 / 1023.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
Serial.println(voltage); // print out the value you read:
}
The results can be viewed either numerically, using the Serial Monitor, or graphically using the Serial Plotter:
Challenge:
If voltage(brightness) greater than threshold, turn on an LED;
else turn it off
See https://www.arduino.cc/en/Tutorial/ifStatementConditional on using conditionals
If voltage(brightness) greater than threshold, turn on an LED;
else turn it off
See https://www.arduino.cc/en/Tutorial/ifStatementConditional on using conditionals