📘 CodingMarble Learn

Sensors: How Machines Sense the World

A sensor turns a physical quantity (light, temperature, distance, moisture, sound, pressure) into an electrical signal. Analogue sensors give a smooth voltage; an ADC turns it into a number the computer can use. A controller compares the number with a threshold and switches an actuator, often through a relay. Readings taken at regular times are data logging. Good sensors have the right range, sensitivity, accuracy and response time.

🎬 Step-by-step story

  1. The yellow ball is a physical quantity: light. The green block is a sensor. It turns light into an electrical signal (blue dots).
  2. Light goes up. An LDR's resistance goes down, so the voltage it gives changes. The green bar shows this signal.
  3. The signal is smooth (analogue). The purple ADC turns it into a number from 0 to 1023 (digital). Count the purple steps.
  4. The blue controller compares the number with the red threshold line. When it gets dark, the relay clicks and the lamp turns ON.
  5. Now a reading is saved again and again. The dots make a graph: this is data logging.
  6. Free play: choose another sensor, move the slider and see when the output turns on.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Does the sensor decide when to switch the lamp?

No. The sensor only reports. The controller compares with the threshold and decides; the relay and lamp act.

Why does the LDR give a different voltage when its resistance changes?

The LDR is used with a fixed resistor as a voltage divider; when the LDR's share of the resistance changes, its share of the voltage changes too. Watch the green bar move with light.

Why do we need an ADC at all?

A computer can only store numbers. The ADC chops the smooth signal into steps — count the purple blocks.

Why does the lamp use a relay and not the controller's pin?

The controller pin gives only milliamps; the relay lets that tiny current switch the lamp's bigger current.

Why is the logged graph a bit wobbly?

Real sensors pick up small random changes called noise; logging many readings shows the real trend.

Do all sensors turn the output on below the threshold?

No. A light sensor switches the lamp on when it is darker; a temperature sensor switches the fan on when it is hotter. Try both in free play.

What is a sensor?

A sensor is a device that notices a change in the world and turns it into an electrical signal. The thing it measures is a physical quantity: light, temperature, distance, sound, pressure, moisture, motion or gas.

A sensor is an input device. It does not decide anything. It only reports. The decision is taken by a controller (a microcontroller or computer), and an actuator (lamp, motor, buzzer, fan, pump) does the action.

Simple chain: physical quantity → sensor → signal → controller → actuator.

Common kinds of sensors

Analogue, digital and the ADC

An analogue signal can take any value in a range, like a voltage from 0 V to 5 V. A digital signal has only fixed steps, like the numbers 0, 1, 2 … 1023.

A computer understands only numbers, so an ADC (analogue-to-digital converter) measures the voltage and gives a number. A 10-bit ADC gives 2¹⁰ = 1024 levels (0 to 1023). For a 0–5 V range, each step is 5 ÷ 1023 ≈ 4.9 mV.

Some sensors are digital already: they just say ON/OFF (a push switch, a PIR) or send the number themselves.

Qualities of a good sensor

Thresholds, relays and simple automatic control

A threshold is a set value. The controller asks one question again and again: is the reading above or below the threshold? If yes, it switches the output.

repeat forever:
  light = read LDR
  if light < 300: lamp ON
  else: lamp OFF

A microcontroller gives only a tiny current. A relay is an electrically operated switch: a small current in its coil pulls a contact that switches a much bigger current (a pump or a mains lamp).

When the output changes what the sensor measures (a heater warms the room the thermistor is in), we have closed-loop (feedback) control, like an air conditioner holding 24 °C.

Data logging and the Internet of Things

Data logging means taking readings automatically at a fixed time gap (the sampling interval) and storing them. Logging a pond's temperature every 10 minutes for a day gives 6 × 24 = 144 readings. Small intervals catch fast changes but make more data.

In the Internet of Things (IoT) sensors send their data over Wi-Fi or mobile networks to a server, where it can be graphed, alarm messages can be sent, and devices can be switched remotely, like a smart weather station or a smart-farm irrigation system.

Try it: your phone is full of sensors

Open a free sensor app (or the phone's compass and level tools). Cover the top of the phone with your hand and watch the light reading fall. Tilt the phone and watch the accelerometer numbers. Then in the 3D pick each sensor and find its threshold by moving the slider slowly.

Key formulas and definitions

Worked examples

1. Name the sensor and the actuator in an automatic street light.

Sensor: LDR (light). Actuator: the lamp, switched through a relay. The controller turns it on when light falls below the threshold.

2. An ultrasonic echo returns after 0.01 s. Speed of sound = 340 m/s. How far is the object?

Distance = 340 × 0.01 ÷ 2 = 1.7 m. We halve because the sound goes there and back.

3. How many levels does an 8-bit ADC have?

2⁸ = 256 levels (0 to 255).

4. A 10-bit ADC uses 0–5 V. What number does 2.5 V give?

2.5 ÷ 5 × 1023 ≈ 511.5, so about 511 or 512.

5. A logger records temperature every 30 s for 2 hours. How many readings?

2 h = 7200 s; 7200 ÷ 30 = 240 readings.

6. A fan should turn on above 30 °C. Write the control rule and say which sensor is needed.

Sensor: thermistor (temperature). Rule: repeat — read temperature; if it is above 30 °C turn the fan ON, else OFF.

Common mistakes

Practice quiz

1. A sensor changes a physical quantity into:
2. When light on an LDR increases, its resistance:
3. Which device changes an analogue voltage into a number?
4. Which is an actuator?
5. An ultrasonic sensor measures distance by timing:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is a sensor in simple words?

A device that notices something in the world, like light or heat, and turns it into an electrical signal a machine can use.

What is the difference between a sensor and an actuator?

A sensor takes information in (input). An actuator, like a motor or lamp, does an action (output).

What is the difference between analogue and digital sensors?

An analogue sensor gives a smoothly changing voltage; a digital sensor gives fixed values like ON/OFF or a number.

Where this is taught

Spain4º ESOComputational thinking, automation and robotics
Japan高校(専門学科)1〜3年Electromechanics (Mechatronics)
South Korea고등학교 3학년ICT and new materials
FranceTroisièmeTechnical objects and systems
FranceSecondeThemes
FrancePremièreLab physical and chemical sciences (option)
China八年级(初二)Module: IoT practice and exploration
China高一Comp.2 Ch.3 Infrastructure
China高二Sel.1 Electronic control technology (engineering series)
China高二Sel.2 Robot design and making (engineering)
China高二Selective 2 Ch.5 Sensors

Learn first

Learn next

Related lessons

All Computer Science lessons