Choose the best reason why sensor interfacing is essential in Arduino projects.
Think about what sensors do and how Arduino uses their information.
Sensors collect data from the environment. Interfacing them with Arduino lets the board read this data to make decisions or control devices.
Given the code below, what will be printed to the Serial Monitor?
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.print("Temperature: "); Serial.print(temperatureC); Serial.println(" C"); delay(1000); }
Think about how the sensor voltage converts to temperature in Celsius.
The code converts the analog reading to voltage, then to temperature using the sensor's formula. For 25°C, the output matches option A.
Find the reason why the Arduino code below always prints zero for sensorValue.
int sensorPin = A0; int sensorValue; void setup() { Serial.begin(9600); } void loop() { sensorValue = analogRead(sensorPin); sensorValue = 0; Serial.println(sensorValue); delay(1000); }
Look carefully at the order of statements inside the loop.
The code reads the sensor value but then immediately sets sensorValue to zero, so it always prints zero.
Identify the correct fix for the syntax error in the code below:
void loop() {
int sensorValue = analogRead(A0)
Serial.println(sensorValue);
}Check if every statement ends properly in Arduino C++.
In Arduino C++, each statement must end with a semicolon. Missing it causes a syntax error.
Consider this Arduino code that reads a sensor 5 times and stores values in an array. How many valid readings does the array contain after the loop?
int sensorPin = A0; int readings[5]; void setup() { Serial.begin(9600); for (int i = 0; i < 5; i++) { readings[i] = analogRead(sensorPin); delay(100); } } void loop() { // nothing here }
Look at the for loop and how many times it runs.
The for loop runs 5 times, storing a sensor reading each time, so the array has 5 valid readings.
