Powered by TinkerSpace
Join free
TinkerPy Tutorial ๐Ÿ“˜ Part 2 ยท Coder ยท Chapter 13 of 24

Coding Inputs: Digital & Analog

In TinkerPy Starter you met buttons, knobs and sensors. Now you will code them like a pro: read a button as ON or OFF, read a knob as a number from 0 to 100 โ€” or even in volts โ€” and turn every reading into a smart decision.

๐Ÿ“–Key words

Digital input condition

A check like if button == ON: โ€” the input is either ON or OFF, nothing in between.

Active low / active high

How a module says โ€œONโ€. Most modules are active_low; PIR motion and touch sensors are active_high. Circuit Connections picks the right one for you.

analog_read()

Reads an input pin as a number from 0 to 100: level = analog_read(knob). The pin must be set as adc.

voltage()

Reads the same pin in volts, from 0.00 to 3.30: v = voltage(knob). Needs board firmware 2.24 or newer.

Threshold

The number where your decision changes, like 30 in if level < 30:.

๐ŸŽฌSee it work

Turn the knob

Drag the knob reading from 0 to 100. Watch the volts and the decision the code makes.

Read, then decide

Press Step. The loop reads the knob three times while someone turns it โ€” watch which branch runs each time.

๐Ÿง How it works

Digital inputs in code

  • A digital input is ON or OFF. Check it with if button == ON: or if button == OFF: โ€” two equals signs mean โ€œis it equal?โ€.
  • What ON means depends on the module: a push button is ON when pressed, a light sensor when it is dark, a soil sensor when it is dry, a flame sensor when it sees fire.
  • Join checks with and / or: if arm_switch == ON and motion == ON: is true only when both are ON.
  • Use elif for a second question โ€” it is checked only when the ones above were false. else catches everything left over.

analog_read() โ€” a number from 0 to 100

  • In Circuit Connections pick an analog module (knob, light level, soil level, water levelโ€ฆ) and its mode adc: you get knob = set_input(potentiometer, adc).
  • level = analog_read(knob) gives 0 (no voltage) up to 100 (full voltage).
  • Analog works only on the input pins: i1โ€“i4 on the TinkerPy Mega, i1โ€“i2 on the Mini.
  • A reading is a number, so you can do maths with it: gap = 1050 - speed * 10.

voltage() โ€” the same pin in volts

  • v = voltage(light) gives the volts on the pin, from 0.00 to 3.30.
  • It is the only place in TinkerPy where you compare with decimals: if v < 1.6:.
  • analog_read and voltage read the same pin โ€” 50 is about 1.65 V. Use whichever is easier to think about.
  • voltage() needs board firmware 2.24 or newer.

Turning readings into decisions

  • Pick a threshold: first show the reading in both conditions (bright and dark, wet and dry), then choose a number between them.
  • Use < or >, not == โ€” a sensor almost never lands on one exact number.
  • Several zones? Put the checks in order with if / elif / else.
  • Store the reading in a variable first (level = analog_read(knob)) so every check uses the same value.

๐Ÿ’ปCode

# Add the libraries you need
import buzzer

# Give your pins easy-to-remember names
i1 = set_name("motion")
i2 = set_name("arm_switch")
o1 = set_name("armed_led")
o2 = set_name("siren")

# ===== Shop door alert =====

# This code block executes only once
setup:
    motion = set_input(pir, active_high)
    arm_switch = set_input(slide_switch, active_low)
    armed_led = set_output(led)
    siren = set_output(buzzer)

# This code block executes forever
loop:
    if arm_switch == ON and motion == ON:  # when arm_switch is ON and motion is ON
        siren = ON  # turn siren ON
        armed_led = ON  # turn armed_led ON
    elif arm_switch == ON:  # otherwise, when arm_switch is ON
        siren = OFF  # turn siren OFF
        armed_led = ON  # turn armed_led ON
    else:  # otherwise
        siren = OFF  # turn siren OFF
        armed_led = OFF  # turn armed_led OFF

A shop door alert with two digital inputs. When the slide switch (arm_switch) is ON the alarm is armed and armed_led glows; if the PIR (motion, active_high) also sees someone, the siren sounds. Circuit Connections wrote the names, the set_input lines with their modes and import buzzer.

# Give your pins easy-to-remember names
i1 = set_name("knob")
o1 = set_name("runner")

# ===== Knob game speed =====

# This code block executes only once
setup:
    knob = set_input(potentiometer, adc)
    runner = set_output(led)

# This code block executes forever
loop:
    speed = analog_read(knob)  # knob position, 0 to 100
    gap = 1050 - speed * 10  # bigger reading = shorter gap
    runner = ON  # turn runner ON
    wait(gap)  # pause here for gap milliseconds before continuing
    runner = OFF  # turn runner OFF
    wait(gap)  # pause here for gap milliseconds before continuing

Knob-controlled game speed. analog_read(knob) gives 0 to 100; the maths turns it into a gap from 1050 ms (knob at 0, slow) down to 50 ms (knob at 100, very fast). The LED named runner blinks at that speed โ€” use it as the speed of a reaction game.

# Add the libraries you need
import buzzer

# Give your pins easy-to-remember names
i2 = set_name("light")
o1 = set_name("lamp")
o2 = set_name("alarm")

# ===== Light-level alarm =====

# This code block executes only once
setup:
    light = set_input(light_sensor, adc)
    lamp = set_output(led)
    alarm = set_output(buzzer)

# This code block executes forever
loop:
    v = voltage(light)  # volts on the light pin, 0.00 to 3.30
    level = analog_read(light)  # the same reading as 0 to 100
    if level < 20:  # when level < 20
        lamp = ON  # turn lamp ON
        alarm = ON  # turn alarm ON
    elif v < 1.6:  # otherwise, when v < 1.6
        lamp = ON  # turn lamp ON
        alarm = OFF  # turn alarm OFF
    else:  # otherwise
        lamp = OFF  # turn lamp OFF
        alarm = OFF  # turn alarm OFF
    wait(200)  # pause here for 200 milliseconds before continuing

A light-level alarm that reads the same light sensor two ways: analog_read() for very dark (below 20, lamp and alarm), and voltage() for dusk (below 1.6 V, lamp only). The light sensor is set as adc so it gives a number, not just ON / OFF. Check your own moduleโ€™s readings first โ€” some modules give high numbers when dark.

Open TinkerPy Code

๐ŸŽ‰Fun examples

๐ŸŽš๏ธVolume knobs

The volume knob on many speakers is a potentiometer โ€” exactly the knob you read with analog_read().

๐ŸŒ†Streetlights that know

Many streetlights switch on by themselves at dusk. A light sensor plus one threshold is all it takes!

๐ŸŽฎGame controllers

Game controller triggers and sticks are read as numbers, not just ON / OFF โ€” that is why a car in a game can go a little or a lot faster.

๐ŸŒWhere itโ€™s used

๐Ÿ› ๏ธProject ideas

Light-level alarm

Put a light sensor (adc) on i1 and a buzzer on o1. Show the reading in a bright room and with your hand over the sensor, pick a threshold, then make the buzzer beep when the box is opened in the dark.

๐Ÿงฐ You need: TinkerPy board, light sensor module, buzzer
Knob-controlled game speed

Use the knob example: the knob sets how fast an LED blinks. Add a button on i2 โ€” the player must press it while the LED is ON. Turn the knob up for a harder level.

๐Ÿงฐ You need: TinkerPy board, potentiometer, push button, LED

โญPoints to remember

  1. A digital input is ON or OFF: if button == ON:
  2. Use == to compare and = to set.
  3. Most modules are active_low; PIR and touch are active_high.
  4. ON means pressed, dark, dry or fire โ€” it depends on the module.
  5. and = both must be true; or = at least one.
  6. elif is checked only when everything above was false.
  7. analog_read(pin) gives 0 to 100.
  8. voltage(pin) gives 0.00 to 3.30 volts (firmware 2.24).
  9. The pin must be set as adc for analog readings.
  10. Analog works only on input pins (Mega i1โ€“i4, Mini i1โ€“i2).
  11. Store a reading in a variable, then decide.
  12. Compare with < or >, not ==.
  13. Test the readings first, then pick your threshold.
๐Ÿคฏ
Did you know?

Your board turns the voltage on an analog pin into a number many times a second โ€” so turning a knob feels instant, even though the code is reading it again and again.

โ“Mini quiz

๐Ÿ”‘
Log in to try the mini quiz

5 quick questions to check yourself โ€” free with your TinkerPy account.

Log in