📖 Lesson 3.4 – Threshold Value Logic
🎯 Learning Objectives
After completing this lesson, students will be able to:
✅ Understand what a threshold value is
✅ Understand why threshold values are important
✅ Calibrate an LDR sensor properly
✅ Select suitable threshold values for different environments
✅ Improve the performance of automatic lighting systems
✅ Avoid false triggering problems
1. Introduction
In the previous lesson, we used this condition:
if(ldrValue < 400)
{
digitalWrite(13, HIGH);
}
else
{
digitalWrite(13, LOW);
}
But why did we choose:
400
Why not:
200
or
800
The answer is:
Threshold Value
A threshold value is the decision point that tells Arduino when to perform an action.
2. What is a Threshold Value?
A threshold value is a predefined sensor reading used to make decisions.
Think of it as a boundary line.
Example:
Threshold = 400
If:
LDR < 400
Darkness detected
↓
LED ON
If:
LDR > 400
Light detected
↓
LED OFF
3. Real-Life Example
Imagine a school examination.
Passing Marks:
40
If student scores:
45
Pass
If student scores:
35
Fail
The value:
40
acts as a threshold.
Similarly, Arduino uses threshold values for decision making.
4. Why Do We Need Threshold Values?
Sensors continuously produce changing values.
Example:
615
620
618
610
605
Arduino needs a point where it decides:
Bright
or
Dark
That decision point is called the threshold value.
5. Understanding LDR Readings
Every environment produces different readings.
Example:
Bright Sunlight
900 – 1023
Room Light
500 – 800
Evening
250 – 500
Darkness
0 – 250
These values vary from place to place.
6. Selecting a Threshold Value
Suppose your readings are:
| Condition | Reading |
|---|---|
| Sunlight | 950 |
| Room Light | 700 |
| Evening | 350 |
| Darkness | 100 |
A reasonable threshold would be:
400
Because:
Above 400
Bright Environment
Below 400
Dark Environment
7. Testing Sensor Values
Before selecting a threshold:
Always measure actual values.
Upload this program:
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println(analogRead(A0));
delay(200);
}
Observe readings in:
- Morning
- Afternoon
- Evening
- Night
8. Creating a Reading Table
Students should create a table like this:
| Condition | Reading |
|---|---|
| Flashlight | ______ |
| Room Light | ______ |
| Window Light | ______ |
| Evening | ______ |
| Dark Room | ______ |
This helps determine the correct threshold.
9. Example Threshold Selection
Suppose:
| Condition | Reading |
|---|---|
| Bright | 850 |
| Room Light | 600 |
| Evening | 350 |
| Darkness | 120 |
A suitable threshold:
400
This cleanly separates bright and dark conditions.
10. What Happens if Threshold is Too Low?
Example:
if(ldrValue < 100)
Problem:
Only complete darkness activates the LED.
Evening light may not trigger the system.
Result:
Poor performance.
11. What Happens if Threshold is Too High?
Example:
if(ldrValue < 900)
Problem:
LED may remain ON almost all the time.
Even normal daylight may trigger the LED.
Result:
False activation.
12. Finding the Best Threshold
A good threshold:
✅ Detects darkness correctly
✅ Ignores normal daylight
✅ Avoids false triggering
✅ Provides stable operation
Example
Darkness Reading:
150
Room Light:
700
Choose:
400
Middle value.
13. Updating the Program
Example:
int threshold = 400;
Program:
int threshold = 400;
int ldrValue;
void setup()
{
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop()
{
ldrValue = analogRead(A0);
if(ldrValue < threshold)
{
digitalWrite(13, HIGH);
}
else
{
digitalWrite(13, LOW);
}
}
14. Making Threshold Adjustable
Instead of hardcoding:
400
Use:
int threshold = 400;
Advantages:
- Easier calibration
- Easier modifications
- Better code readability
15. Understanding Sensor Calibration
Calibration means adjusting the system to work correctly in a specific environment.
Example:
Classroom
Threshold:
450
Outdoor Garden
Threshold:
600
Street Light
Threshold:
500
Different environments require different calibration values.
16. Real-World Example
Automatic Street Light System:
Day:
LDR = 850
LED OFF
Evening:
LDR = 380
LED ON
Night:
LDR = 120
LED ON
This entire decision is based on threshold logic.
17. Industrial Applications
Threshold logic is used in:
Automatic Street Lights
Water Tank Systems
Fire Detection Systems
Gas Leakage Detection
Temperature Monitoring
Smart Agriculture
Industrial Automation
18. Common Beginner Mistakes
Mistake 1
Using random threshold values.
Always measure sensor readings first.
Mistake 2
Copying threshold values from the internet.
Every environment is different.
Mistake 3
Not recalibrating after changing location.
Mistake 4
Ignoring sensor testing.
Always verify readings using Serial Monitor.
19. Best Practices
✅ Observe sensor readings first
✅ Create a reading table
✅ Select middle-range threshold values
✅ Test in different lighting conditions
✅ Recalibrate when necessary
📊 Summary
In this lesson, we learned:
✅ What a threshold value is
✅ Why threshold values are important
✅ How Arduino uses threshold logic
✅ Sensor calibration
✅ Selecting the correct threshold value
Threshold values are the foundation of automation because they allow Arduino to make intelligent decisions based on sensor readings.
📖 Key Terms
Threshold Value
A reference value used for decision making.
Calibration
Adjusting sensor settings for proper operation.
Analog Reading
Sensor value between 0 and 1023.
Automation Logic
Decision-making process used in automatic systems.
False Triggering
Incorrect activation of a system.
🎯 Quiz
1. What is a threshold value?
A. Power Supply
B. Reference value used for decisions ✅
C. Sensor Type
D. Programming Language
2. Why do we use threshold values?
A. To power sensors
B. To upload programs
C. To separate different conditions ✅
D. To increase voltage
3. What should be done before selecting a threshold?
A. Buy a new sensor
B. Measure actual readings ✅
C. Change Arduino
D. Replace LED
4. What happens if the threshold is too high?
A. Sensor stops working
B. False triggering may occur ✅
C. Arduino resets
D. LED burns
5. Calibration means:
A. Deleting code
B. Adjusting the system for correct operation ✅
C. Installing software
D. Charging batteries
🏠 Assignment
Task 1
Record LDR readings in different lighting conditions.
Task 2
Determine the best threshold value for your classroom.
Task 3
Test three threshold values (300, 500, 700) and compare results.
Task 4
Explain why calibration is important in sensor-based projects.
Task 5
Create a chart showing LED behavior for different LDR readings.