Teach Remote lab lessons

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.

  • Arduino Board (visual)
  • 55 min
  • Secondary (ages 14–15)
  • English
  • Embedded systems
Arduino Board (visual)
Arduino Board (visual)

Learning Outcomes

  • Read an input and confirm it changes before deciding an output.

  • Use an if / else condition 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.

1

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.

2

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.

  1. Open arduino-visual-board.

  2. Main route (analog): in Input/Output, use read analog pin# and start with pin A0.

  3. 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.

  4. Verify / compile and Upload into device.

  5. Change the input from the lab controls (turn the potentiometer / move the input control) and check that the reading changes.

  6. 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?
3

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.

  1. Add an if / else block.

  2. 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 of A0.

  3. Inside do (if true), put set built-in LED to HIGH.

  4. Inside else, put set built-in LED to LOW.

  5. Add wait 100 milliseconds at the end so the reading repeats steadily.

  6. Verify / compile, Upload into device, and test by moving the input.

  7. 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:

Visual block diagram: inside Arduino loop forever there is an if condition reading analog pin A0 greater than or equal to 500; in do, set built-in LED to HIGH; in else, set built-in LED to LOW; then wait 100 milliseconds.

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.

4

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).