Bird
Raised Fist0
Arduinoprogramming~20 mins

Soil moisture sensor in Arduino - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Soil Moisture Sensor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Reading soil moisture sensor value
What is the output on the serial monitor when this Arduino code runs with the sensor reading analog value 600?
Arduino
int sensorPin = A0;
void setup() {
  Serial.begin(9600);
}
void loop() {
  int sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue);
  delay(1000);
}
A600
B1023
C0
DAnalog read error
Attempts:
2 left
💡 Hint
The analogRead function returns a value between 0 and 1023 based on the sensor voltage.
🧠 Conceptual
intermediate
1:30remaining
Understanding soil moisture sensor output range
Which statement correctly describes the soil moisture sensor output when connected to Arduino analog pin?
AThe sensor outputs a fixed voltage regardless of moisture.
BThe sensor outputs an analog voltage that varies with moisture level.
CThe sensor outputs a digital HIGH or LOW signal only.
DThe sensor outputs a PWM signal to indicate moisture.
Attempts:
2 left
💡 Hint
Think about how analog sensors work with Arduino analog pins.
🔧 Debug
advanced
2:00remaining
Fixing incorrect sensor reading code
What error does this Arduino code produce when trying to read soil moisture sensor value? int sensorPin = A0 void setup() { Serial.begin(9600); } void loop() { int moisture = analogRead(sensorPin); Serial.println(moisture); delay(1000); }
Arduino
int sensorPin = A0;
void setup() {
  Serial.begin(9600);
}
void loop() {
  int moisture = analogRead(sensorPin);
  Serial.println(moisture);
  delay(1000);
}
ASyntaxError: missing semicolon after variable declaration
BRuntimeError: analogRead failed
CNo output, code runs fine
DTypeError: sensorPin is not defined
Attempts:
2 left
💡 Hint
Check the line where sensorPin is declared.
❓ Predict Output
advanced
2:00remaining
Calculating moisture percentage from sensor value
What is the output of this code if analogRead returns 512? int sensorPin = A0; void setup() { Serial.begin(9600); } void loop() { int sensorValue = analogRead(sensorPin); int moisturePercent = map(sensorValue, 0, 1023, 0, 100); Serial.println(moisturePercent); delay(1000); }
Arduino
int sensorPin = A0;
void setup() {
  Serial.begin(9600);
}
void loop() {
  int sensorValue = analogRead(sensorPin);
  int moisturePercent = map(sensorValue, 0, 1023, 0, 100);
  Serial.println(moisturePercent);
  delay(1000);
}
A512
B0
C50
D100
Attempts:
2 left
💡 Hint
The map function converts a number from one range to another.
🧠 Conceptual
expert
2:30remaining
Choosing correct sensor pin and reading method
Which option correctly explains why soil moisture sensor must be connected to an analog pin and read with analogRead() on Arduino?
ABecause soil moisture sensor outputs a digital signal that analogRead() converts to analog value.
BBecause digital pins automatically convert analog signals to digital values.
CBecause analog pins can only read digital signals from sensors.
DBecause soil moisture sensor outputs an analog voltage that digital pins cannot read directly.
Attempts:
2 left
💡 Hint
Think about the difference between analog and digital pins on Arduino.

Practice

(1/5)
1.

What does the analogRead() function do when using a soil moisture sensor with Arduino?

easy
A. It prints the sensor value to the serial monitor.
B. It reads the sensor's analog voltage value representing soil moisture.
C. It sends power to the soil moisture sensor.
D. It converts the sensor value to a digital HIGH or LOW.

Solution

  1. Step 1: Understand the purpose of analogRead()

    The analogRead() function reads the voltage from an analog pin and returns a value between 0 and 1023.
  2. Step 2: Connect to soil moisture sensor output

    The soil moisture sensor outputs an analog voltage that changes with moisture level, which analogRead() captures.
  3. Final Answer:

    It reads the sensor's analog voltage value representing soil moisture. -> Option B
  4. Quick Check:

    analogRead() reads analog sensor value [OK]
Hint: analogRead() always reads voltage from analog pins [OK]
Common Mistakes:
  • Thinking analogRead() sends power to sensor
  • Confusing analogRead() with Serial.println()
  • Assuming analogRead() returns digital HIGH or LOW
2.

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);
}
easy
A. moisture = analogRead(A0);
B. moisture = digitalRead(A0);
C. moisture = Serial.read(A0);
D. moisture = analogWrite(A0);

Solution

  1. Step 1: Identify correct function to read analog sensor

    Use analogRead() to read analog pin A0 for sensor value.
  2. Step 2: Confirm syntax matches Arduino standards

    The correct syntax is moisture = analogRead(A0); to store the sensor value.
  3. Final Answer:

    moisture = analogRead(A0); -> Option A
  4. Quick Check:

    Use analogRead() for analog sensor input [OK]
Hint: Use analogRead() for analog sensors, not digitalRead() [OK]
Common Mistakes:
  • Using digitalRead() for analog sensor
  • Trying to use Serial.read() to get sensor value
  • Using analogWrite() which outputs voltage, not reads
3.

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);
}
medium
A. 0
B. 1023
C. 512
D. AnalogRead Error

Solution

  1. Step 1: Understand analogRead() output

    analogRead() returns a value between 0 and 1023 based on sensor voltage. Here it reads 512.
  2. Step 2: Serial.println() prints the value

    The code prints the value stored in moisture, which is 512.
  3. Final Answer:

    512 -> Option C
  4. Quick Check:

    analogRead() value printed = 512 [OK]
Hint: Serial.println() prints the sensor value read by analogRead() [OK]
Common Mistakes:
  • Expecting maximum 1023 always printed
  • Thinking analogRead() returns 0 or error
  • Confusing analogRead() with digitalRead() output
4.

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);
}
medium
A. Serial.print() should be Serial.println() to add a new line.
B. Serial.begin() must be called inside loop().
C. delay() cannot be used inside loop().
D. analogRead() should use A0 instead of 0.

Solution

  1. Step 1: Check analogRead() argument

    Using analogRead(0) reads digital pin 0, which is incorrect for analog input.
  2. Step 2: Use A0 for analog pin 0

    The correct syntax is analogRead(A0) to read analog pin A0.
  3. Final Answer:

    analogRead() should use A0 instead of 0. -> Option D
  4. Quick Check:

    Use A0 constant for analog pins [OK]
Hint: Use A0, A1, etc. for analogRead() pins [OK]
Common Mistakes:
  • 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()
5.

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);
}
hard
A. moisture < 300
B. moisture > 300
C. moisture == 300
D. moisture != 300

Solution

  1. Step 1: Understand the condition for watering

    The pump should turn on when soil is dry, meaning moisture value is less than 300.
  2. Step 2: Write the correct if condition

    The condition moisture < 300 triggers the pump ON with digitalWrite(7, HIGH);.
  3. Final Answer:

    moisture < 300 -> Option A
  4. Quick Check:

    Turn pump ON if moisture below threshold [OK]
Hint: Use if moisture < threshold to activate pump [OK]
Common Mistakes:
  • Using > instead of < for dry soil condition
  • Checking equality instead of range
  • Forgetting to set pinMode for pump pin
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard