Teach lesson
Visual programming with a real Arduino (3/4): inputs and decisions
Students use visual blocks to read an input, make a decision with a threshold, and verify LED behavior on a real Arduino board.
Learning Outcomes
Read an input and confirm it changes before deciding an output.
Use an
if / elsecondition with a comparison and a threshold.Distinguish digital and analog input using a real example.
Student activity preview
Activity Content
Preview only. In a class session, students can fill in responses and submit their work to the teacher.
Input, threshold and decision
10 min
An input is information the board reads from the outside world. There are
two types:
- Digital input: only two states, like a button pressed / not pressed
(or HIGH / LOW).
- Analog input: many possible values. On an Arduino Uno board, an analog
reading gives a number from 0 to 1023 (for example, the position of a
potentiometer, which is a knob you turn).
A sensor is a component that produces one of those readings. A threshold
is the cut-off value you use to turn an analog reading (many numbers) into a
yes/no decision. Example: "if the reading is greater than or equal to 500, switch
the LED on; otherwise, switch it off." 500 is the threshold.
Remember the editor structure: Arduino run first runs once at the beginning;
Arduino loop forever repeats without stopping. The input reading and decision
go in the loop forever so the board checks the value many times.
What does it mean to use a threshold of 500 on an analog reading that ranges from 0 to 1023?
Write one difference between a digital input and an analog input.
Read and check the input
16 min
Before deciding, check that you can read the input and that it changes. This
check is key: if the input does not change, no decision will work.
Open
arduino-visual-board.Main route (analog): in Input/Output, use read analog pin# and start with pin A0.
To *see* the reading you can optionally use Comms: add Setup serial 9600 bps and a print block with the reading. If the lab does not show serial output, that is fine: in the next step you will use the LED as the signal.
Verify / compile and Upload into device.
Change the input from the lab controls (turn the potentiometer / move the input control) and check that the reading changes.
If the reading does not change when you move the input, try another analog pin (A0 to A5) until it does. This confirms which pin reads your input.
Fill the table with one row for each input position you test. Use at least
three rows: low, middle, and high; leave the extra rows empty. In each row,
record the input position, the pin used, the reading or visible effect, and
whether it changed when you moved the input.
| Input position / state | Pin used | Reading or observed effect | Changed when you moved the input? |
|---|---|---|---|
Make a decision
22 min
Now add an if / else condition (*Logic* category) that uses your reading and
a threshold to switch the built-in LED on or off.
Add an if / else block.
In the condition, compare your confirmed analog reading with a threshold: for example,
read analog pin# A0≥500. If you confirmed a different analog pin, use that pin instead ofA0.Inside do (if true), put
set built-in LEDto HIGH.Inside else, put
set built-in LEDto LOW.Add
wait 100 millisecondsat the end so the reading repeats steadily.Verify / compile, Upload into device, and test by moving the input.
Calibrate: if the LED does not switch where you want, adjust the threshold (raise or lower the 500) and repeat until the LED turns on and off where you expect.
Your program should look roughly like this:
Reference diagram for the decision. The A0 reading is compared with the threshold, and the LED changes according to the do or else branch.
Arduino loop forever:
if [read analog pin# [A0]] [≥] [500]
do:
[set built-in LED] [HIGH]
else:
[set built-in LED] [LOW]
[wait] 100 milliseconds
This reference uses A0 because it is the usual starting point. If your input
check showed a different analog pin, keep the same program shape but replace
A0 with your confirmed pin.
Explain your full condition: what input you read, what threshold (or state) you compare it with, and what output changes.
Describe two tests: one where the LED switches on and one where it switches off. Include the input position/state and, if you calibrated it, the final threshold.
If it does not change, debug
7 min
If the LED does not change when you move the input, write three ordered checks you would do before asking for help (think about: read pin, threshold value, and whether you uploaded the program).