A soil moisture sensor helps you check how wet or dry the soil is. This is useful to know when plants need water.
Soil moisture sensor in Arduino
int sensorPin = A0; // Connect sensor to analog pin A0 int sensorValue = 0; // Variable to store sensor reading void setup() { Serial.begin(9600); // Start serial communication } void loop() { sensorValue = analogRead(sensorPin); // Read sensor value Serial.println(sensorValue); // Print value to serial monitor delay(1000); // Wait 1 second before next reading }
analogRead(pin) reads the sensor value from the specified analog pin.
Serial.begin(9600) starts communication to send data to your computer.
int sensorPin = A0; int sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
delay(1000);This program reads the soil moisture sensor every second and prints the moisture level to the serial monitor. You can see the numbers change depending on how wet the soil is.
int sensorPin = A0; // Soil moisture sensor connected to analog pin A0 int sensorValue = 0; // Variable to store sensor reading void setup() { Serial.begin(9600); // Start serial communication at 9600 baud } void loop() { sensorValue = analogRead(sensorPin); // Read the sensor value Serial.print("Soil moisture level: "); Serial.println(sensorValue); // Print the value delay(1000); // Wait 1 second }
The sensor value changes with soil wetness: higher values usually mean drier soil, lower values mean wetter soil.
Make sure to connect the sensor to the correct analog pin and power it properly.
Use the serial monitor in the Arduino IDE to see the sensor readings live.
A soil moisture sensor measures how wet the soil is.
Use analogRead() to get sensor values from the Arduino.
Print values with Serial.println() to watch changes over time.
Practice
What does the analogRead() function do when using a soil moisture sensor with Arduino?
Solution
Step 1: Understand the purpose of analogRead()
TheanalogRead()function reads the voltage from an analog pin and returns a value between 0 and 1023.Step 2: Connect to soil moisture sensor output
The soil moisture sensor outputs an analog voltage that changes with moisture level, whichanalogRead()captures.Final Answer:
It reads the sensor's analog voltage value representing soil moisture. -> Option BQuick Check:
analogRead() reads analog sensor value [OK]
- Thinking analogRead() sends power to sensor
- Confusing analogRead() with Serial.println()
- Assuming analogRead() returns digital HIGH or LOW
Which of the following is the correct way to read a soil moisture sensor connected to analog pin A0 and print its value?
void setup() {
Serial.begin(9600);
}
void loop() {
int moisture = ?;
Serial.println(moisture);
delay(1000);
}Solution
Step 1: Identify correct function to read analog sensor
UseanalogRead()to read analog pin A0 for sensor value.Step 2: Confirm syntax matches Arduino standards
The correct syntax ismoisture = analogRead(A0);to store the sensor value.Final Answer:
moisture = analogRead(A0); -> Option AQuick Check:
Use analogRead() for analog sensor input [OK]
- Using digitalRead() for analog sensor
- Trying to use Serial.read() to get sensor value
- Using analogWrite() which outputs voltage, not reads
What will be printed to the Serial Monitor when this Arduino code runs if the soil moisture sensor reads 512?
void setup() {
Serial.begin(9600);
}
void loop() {
int moisture = analogRead(A0);
Serial.println(moisture);
delay(1000);
}Solution
Step 1: Understand analogRead() output
analogRead() returns a value between 0 and 1023 based on sensor voltage. Here it reads 512.Step 2: Serial.println() prints the value
The code prints the value stored inmoisture, which is 512.Final Answer:
512 -> Option CQuick Check:
analogRead() value printed = 512 [OK]
- Expecting maximum 1023 always printed
- Thinking analogRead() returns 0 or error
- Confusing analogRead() with digitalRead() output
Identify the error in this Arduino code snippet for reading a soil moisture sensor and printing its value:
void setup() {
Serial.begin(9600);
}
void loop() {
int moisture = analogRead(0);
Serial.print(moisture);
delay(1000);
}Solution
Step 1: Check analogRead() argument
UsinganalogRead(0)reads digital pin 0, which is incorrect for analog input.Step 2: Use A0 for analog pin 0
The correct syntax isanalogRead(A0)to read analog pin A0.Final Answer:
analogRead() should use A0 instead of 0. -> Option DQuick Check:
Use A0 constant for analog pins [OK]
- Using integer 0 instead of A0 in analogRead()
- Thinking Serial.print() must be println() always
- Believing delay() is not allowed in loop()
- Calling Serial.begin() inside loop()
You want to turn on a water pump when the soil moisture is below 300. Which code snippet correctly implements this using a soil moisture sensor on A0 and a pump connected to digital pin 7?
void setup() {
Serial.begin(9600);
pinMode(7, OUTPUT);
}
void loop() {
int moisture = analogRead(A0);
if (?) {
digitalWrite(7, HIGH);
} else {
digitalWrite(7, LOW);
}
delay(1000);
}Solution
Step 1: Understand the condition for watering
The pump should turn on when soil is dry, meaning moisture value is less than 300.Step 2: Write the correct if condition
The conditionmoisture < 300triggers the pump ON withdigitalWrite(7, HIGH);.Final Answer:
moisture < 300 -> Option AQuick Check:
Turn pump ON if moisture below threshold [OK]
- Using > instead of < for dry soil condition
- Checking equality instead of range
- Forgetting to set pinMode for pump pin
Ready to prove it?
Pass the test and this pattern moves unknown
