Embedded C and an Arduino sensor
Read a sensor, make a decision, and control an LED in a simulated device loop.
- Explain firmware and a repeated device loop
- Separate portable C decision logic from Arduino board calls
- Test a sensor threshold at, above and below its boundary
A microcontroller runs firmware close to sensors and actuators. A greenhouse controller might sample temperature, compare it with a limit, and switch a warning light. The Arduino API provides pinMode, analogRead, and digitalWrite; the Analog Read Serial example shows a sensor value being read and reported. Arduino sketches are compiled as C++, with C-like syntax and board-specific functions. We will isolate the decision in ordinary C17 so the browser runner can test it without hardware.
Try it
Up to 15 XPSimulate the sensor loop
Predict the LED state, load each mission reading, and run one loop cycle. Try changing the sliders between missions to see the threshold boundary.
Mission 1: A hot sensor should turn the warning LED on.
Score 0/3Simulated 10-bit sensor reading (0–1023). No Arduino or physical circuit is connected.
reading >= thresholdSet the values, then run one loop cycle.
1#include <stdio.h>
2
3int warning_led(int reading, int threshold) {
4 return reading >= threshold;
5}
6
7int main(void) {
8 printf("%s\n", warning_led(720, 600) ? "LED ON" : "LED OFF");
9 printf("%s\n", warning_led(420, 600) ? "LED ON" : "LED OFF");
10 return 0;
11}LED ON LED OFF
1const int SENSOR_PIN = A0;
2const int LED_PIN = 13;
3const int THRESHOLD = 600;
4
5void setup() {
6 pinMode(LED_PIN, OUTPUT);
7 Serial.begin(9600);
8}
9
10void loop() {
11 int reading = analogRead(SENSOR_PIN);
12 bool warning = reading >= THRESHOLD;
13 digitalWrite(LED_PIN, warning ? HIGH : LOW);
14 Serial.println(reading);
15 delay(100);
16}The Arduino IDE supplies the board headers and C++ build environment for setup and loop. The sketch is not runnable in the plain C browser compiler. In a product, engineers also consider sensor calibration, noisy readings, electrical limits, and whether blocking delay would stop other work. Arduino’s Blink Without Delay shows a nonblocking timing pattern.
Key takeaways
Firmware reads inputs, decides, and controls outputs repeatedly.
Arduino sketches use C++ and board APIs; portable C logic can still be tested separately.
Test threshold boundaries and account for real sensor noise and timing.
Lesson quiz
4 questions · pass with 3 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write C
These run for real. Write the program, press Run tests, and read what the compiler tells you - learning to read compiler messages is half of learning C.
Test a warning threshold
Read a threshold and three simulated sensor readings. Print ON when a reading is at least the threshold, otherwise OFF, one result per line. Keep the decision in a small C function so it could later be reused in firmware.
- Below, at, above
- Zero boundary
Your code is compiled with gcc 14 (-Wall -Wextra) and run on Compiler Explorer (godbolt.org), a free public service.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…