Challenge - 5 Problems
Sensor Library Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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); }
Attempts:
2 left
💡 Hint
Think about how analogRead maps sensor values to voltage and then to temperature.
✗ Incorrect
The code converts the analog sensor reading to voltage, then to temperature in Celsius using the sensor's formula. If sensorValue is about 512, voltage is about 2.5V, so temperatureC is about (2.5 - 0.5)*100 = 200°C, but typical sensor outputs vary. The code prints temperatureC, not raw values or voltage.
🧠 Conceptual
intermediate1: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?
Attempts:
2 left
💡 Hint
Check the order of parameters: pin number first, then sensor type.
✗ Incorrect
The DHT library constructor takes the pin number first, then the sensor type constant. So DHT dht(2, DHT11); means sensor on pin 2 of type DHT11.
🔧 Debug
advanced2: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); }
Attempts:
2 left
💡 Hint
Check if all required library headers are included for sensor compatibility.
✗ Incorrect
The Adafruit_Sensor library requires including DHT_U.h for the DHT sensor to work properly. Without it, the code may fail to compile due to missing definitions.
📝 Syntax
advanced1: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?
Attempts:
2 left
💡 Hint
Remember to call functions with parentheses () to execute them.
✗ Incorrect
Option A correctly calls the read() method with parentheses and prints the value. Option A and D miss parentheses causing errors or wrong values. Option A prints value but adds an extra println() which is valid but not asked.
🚀 Application
expert3: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() {}
Attempts:
2 left
💡 Hint
Think about how the loop increments i only when readings[i] > 0.
✗ Incorrect
The loop continues until i reaches 5. It only increments i when the reading is positive. Since analogRead returns values from 0 to 1023, and multiplied by (5.0/1023.0), readings are between 0 and 5 volts. Zero is rare but possible. The loop waits until 5 positive readings are stored.
