Bird
0
0
Arduinoprogramming~10 mins

Soil moisture sensor in Arduino - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Soil moisture sensor
Start
Initialize sensor pin
Read sensor value
Convert value to moisture level
Print moisture level
Wait some time
Read sensor value
The program starts by setting up the sensor pin, then repeatedly reads the sensor, converts the reading to moisture level, prints it, waits, and repeats.
Execution Sample
Arduino
const int sensorPin = A0;
int sensorValue = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue);
  delay(1000);
}
This code reads the soil moisture sensor value from analog pin A0 every second and prints it to the serial monitor.
Execution Table
StepActionsensorValueOutputDelay(ms)
1Initialize sensorPin to A0---
2Start Serial communication---
3Read sensor value from A0523Print 5231000
4Read sensor value from A0510Print 5101000
5Read sensor value from A0498Print 4981000
6Read sensor value from A0505Print 5051000
7Read sensor value from A0495Print 4951000
...Loop continues reading and printing.........
💡 The loop runs forever, reading and printing sensor values every 1000 ms.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5...final
sensorValue0523510498505495...
Key Moments - 3 Insights
Why does sensorValue change each time in the loop?
Because analogRead reads the current moisture level from the sensor each time (see execution_table steps 3-7), so sensorValue updates with new readings.
Why do we use delay(1000) after printing?
delay(1000) pauses the program for 1000 milliseconds (1 second) to avoid flooding the serial monitor with too many readings too fast (see execution_table Delay column).
What does Serial.begin(9600) do in setup()?
It starts serial communication at 9600 bits per second so the Arduino can send data to the computer (see execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the sensorValue at step 4?
A523
B510
C498
D505
💡 Hint
Check the sensorValue column in execution_table row with Step 4.
At which step does the program first print the sensor value?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the Output column in execution_table to find when printing starts.
If delay(1000) was removed, how would the execution_table change?
AOutput would print faster without delay
BProgram would stop after first reading
CsensorValue would not change
DsensorPin would change
💡 Hint
Consider the Delay column and what delay(1000) controls in execution_table.
Concept Snapshot
Soil moisture sensor reads analog values from pin A0.
Use analogRead(sensorPin) to get moisture level.
Print values with Serial.println().
Use delay() to wait between readings.
Loop repeats to monitor moisture continuously.
Full Transcript
This Arduino program reads a soil moisture sensor connected to analog pin A0. It starts by setting sensorPin to A0 and begins serial communication at 9600 baud in setup(). In the loop(), it reads the sensor value using analogRead(sensorPin), stores it in sensorValue, and prints it to the serial monitor. Then it waits 1000 milliseconds before repeating. The sensorValue changes each loop because the sensor measures current moisture. The delay prevents flooding the output. This cycle repeats forever, giving continuous moisture readings.