Q4L3 _ AnalogRead Function
Lesson 14: Analog Read
Prepared by: Jovie Bon B. Cabarles
The analogRead Function
Purpose: Allows Arduino to read analog signals using pins A0 - A5.
Sensors: Some sensors output an analog signal that can be read by Arduino.
Syntax:
analogRead(pin);pin: Specifies the Arduino pin number to read/input from.
Signal Resolution
A0 to A5 pins have 10-bit resolution.
Values: 0 to 1023 corresponding to 0V to 5V.
Conversion:
Formula:
Voltage = (5/1023)*signalExample: If the signal read is 523,
Voltage = (5/1023) * 523 = 2.5562V.
Basic Syntax for Analog Read
Arduino Code Structure for Reading:
void setup() { pinMode(pin, INPUT); }void loop() { analogRead(pin); }Where pin specifies the Arduino pin number.
Example Application: Photoresistor
Photoresistor: A type of resistor whose resistance varies with light intensity.
Schematic: Display simulation on Tinkercad when clicking the photoresistor.
Brightness simulation represented by a slider.
Voltage Divider Circuit
Use of a voltage divider using a photoresistor and resistors.
Formula for Voltage Drop:
V2 = Vs * (R2 / (R1 + R2))Example Calculations:
For R2 = 1k, V2 = 0.88V
For R2 = 2.5k, V2 = 1.77V
Impact of Resistance Changes: Voltage drop across resistors varies with resistance.
Wiring Diagram and Connections
Diagrams depict connection of a photoresistor to A0.
Behavior: If light sensed is dim, resistance is high, yielding a higher voltage drop, and vice versa.
Arduino Coding for Reading Signals
Declare pin A0 as INPUT to read signal:
int Photoresist1;void setup() { pinMode(A0, INPUT); Serial.begin(9600); }void loop() { Photoresist1=analogRead(A0); Serial.println(Photoresist1); }
Serial Monitor Usage
Opens to observe data read by the photoresistor.
Essential function:
Serial.begin(9600);in setup.To display readings:
Serial.println(Photoresist1);
Signal Reading and Simulation
Start simulation and observe numerical outputs representing signals read by A0.
Adjusting the photoresistor output will show corresponding signal changes in the serial monitor.
Problem #2: LED Control Based on Light Intensity
New Circuit Addition: Incorporate an LED.
Arduino should turn on LED if the signal from A0 is greater than 500:
if (Photoresist1 > 500) { digitalWrite(LED, HIGH); }
Else Condition: Turn off the LED when below threshold:
else { digitalWrite(LED, LOW); }
Example: Automated Light Switch Simulation
When photoresistor detects an analog signal over 500, it activates the LED as a light switch reactant.
Activities for Practice**
Use two photoresistors to control an LED based on relative brightness.
Conditional logic required if Photoresistor1 senses more light than Photoresistor2 to switch the LED on, otherwise keep it off.
Further challenge: Blink LED thrice for 5 ms if Photoresistor1 is dimmer than Photoresistor2.
Conclusion
Demonstrating practical applications of analogRead function in Arduino and understanding component interactions through code and circuit behaviors.