Bird
0
0
Arduinoprogramming~20 mins

Using sensor libraries in Arduino - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sensor Library Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Arduino sensor reading code?
Consider this Arduino sketch that reads a temperature sensor value and prints it. What will be printed on the Serial Monitor?
Arduino
int sensorPin = A0;
void setup() {
  Serial.begin(9600);
}
void loop() {
  int sensorValue = analogRead(sensorPin);
  float voltage = sensorValue * (5.0 / 1023.0);
  float temperatureC = (voltage - 0.5) * 100;
  Serial.println(temperatureC);
  delay(1000);
}
AA raw sensor value between 0 and 1023 printed
BA voltage value between 0 and 5 printed directly
CA constant value 0 printed every second
DA temperature value in Celsius around 200.0 if sensorValue is about 512
Attempts:
2 left
💡 Hint
Think about how analogRead maps sensor values to voltage and then to temperature.
🧠 Conceptual
intermediate
1:30remaining
Which library function initializes the sensor correctly?
You want to use a DHT11 temperature and humidity sensor with the DHT library. Which function call correctly initializes the sensor in your Arduino sketch?
ADHT dht(DHT11, 2);
BDHT dht(2, DHT11);
CDHT dht(11, 2);
DDHT dht(2);
Attempts:
2 left
💡 Hint
Check the order of parameters: pin number first, then sensor type.
🔧 Debug
advanced
2:30remaining
Why does this sensor reading code fail to compile?
This Arduino code uses the Adafruit_Sensor library but fails to compile. What is the cause?
Arduino
#include <Adafruit_Sensor.h>
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
  Serial.begin(9600);
  dht.begin();
}
void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Serial.print("Humidity: ");
  Serial.println(h);
  Serial.print("Temperature: ");
  Serial.println(t);
  delay(2000);
}
ASerial.begin(9600); must be called after dht.begin();
BDHT dht(DHTPIN, DHTTYPE); should be DHT dht(DHTTYPE, DHTPIN);
CMissing #include <DHT_U.h> which is required for Adafruit_Sensor compatibility
DThe delay(2000); is too long and causes a timeout error
Attempts:
2 left
💡 Hint
Check if all required library headers are included for sensor compatibility.
📝 Syntax
advanced
1:30remaining
Which option correctly reads and prints sensor data using a library?
Given a sensor library with a read() method that returns a float, which code snippet correctly reads and prints the sensor value?
Afloat val = sensor.read(); Serial.println(val);
Bfloat val = sensor.read; Serial.println(val);
Cfloat val = sensor.read(); Serial.print(val); Serial.println();
Dfloat val = sensor.read; Serial.print(val);
Attempts:
2 left
💡 Hint
Remember to call functions with parentheses () to execute them.
🚀 Application
expert
3:00remaining
How many sensor readings are stored in the array after this code runs?
This Arduino code reads a sensor 5 times and stores values in an array. How many valid readings does the array contain after the loop?
Arduino
float readings[5];
int i = 0;
void setup() {
  Serial.begin(9600);
  while(i < 5) {
    readings[i] = analogRead(A0) * (5.0 / 1023.0);
    if(readings[i] > 0) {
      i++;
    }
  }
}
void loop() {}
A5 readings, because the loop increments i only when reading is positive
B0 readings, because analogRead returns 0 and i never increments
CInfinite loop, because i never increments if readings[i] is 0
DLess than 5 readings, because some readings might be zero and skipped
Attempts:
2 left
💡 Hint
Think about how the loop increments i only when readings[i] > 0.