powered by TinkerSpace.in
Join free
TinkerPy Tutorial ๐Ÿ“˜ Part 1 ยท Starter ยท Chapter 7 of 24

Digital and Analog Inputs

A doorbell button is either pressed or not โ€” but a fan regulator knob can be anywhere in between. Your TinkerPy board can read both kinds: digital inputs that are just ON or OFF, and analog inputs that give a number from 0 to 100.

๐Ÿ“–Key words

Digital input

An input that has only two states: ON or OFF. Buttons, touch pads and motion sensors are digital.

Active low

The module pulls its wire LOW (to 0 volts) when it is ON. Most modules work like this, so it is the usual setting (โ˜…) in Circuit Connections.

Active high

The module makes its wire HIGH when it is ON. The touch sensor and the PIR motion sensor are active high.

Analog input (adc)

An input that gives a number from 0 to 100 instead of just ON or OFF โ€” like a knob or a light level. Only the input pins i1โ€“i4 can do this.

analog_read()

Reads an analog input: level = analog_read(knob) puts a number from 0 to 100 into level.

๐ŸŽฌSee it work

Turn the knob

Drag the knob slider. Watch the analog reading, the volts on the pin and what the Knob light level program does with the lamp.

Three ways to read a pin

Tap each mode to see what it means and which modules use it.

๐Ÿง How it works

Digital inputs: ON or OFF

  • A digital input is like a light switch: it is ON or OFF, nothing in between.
  • In code you ask: if bell_button == ON: โ€” is it pressed right now?
  • Every ON/OFF module has its own meaning of ON: a push button is ON when pressed, a PIR when it sees motion, a flame sensor when it sees fire.
  • Pick the module on a pin in Circuit Connections and it writes the setup line for you, e.g. bell_button = set_input(push_button, active_low).

Active low and active high

  • Inside the module, ON makes the wire either LOW (0 V) or HIGH. The mode tells the board which one means ON.
  • active_low is the usual mode (โ˜…) for most modules: buttons, IR, flame, rain, sound, gasโ€ฆ
  • active_high is the usual mode for the touch sensor and the PIR motion sensor.
  • You never have to work it out yourself โ€” Circuit Connections picks the usual mode and asks โ€œare you sure?โ€ if you change it.
  • If ON and OFF come out the wrong way round when you test your module, switch it to the other mode.

Analog inputs: a number from 0 to 100

  • Some modules donโ€™t just say yes or no โ€” they say how much: how far a knob is turned, how deep the water is, how bright the room is.
  • Set the pin to adc and read it with analog_read(name): you get 0 (nothing) up to 100 (full).
  • The pin measures 0 to 3.3 volts: 0 = 0 V, 50 โ‰ˆ 1.65 V, 100 = 3.3 V.
  • Store the reading in a variable and compare it: if level > 70: โ€ฆ elif level > 30: โ€ฆ
  • In the Simulator an analog input shows up as a 0โ€“100 slider you can drag.

Which pins can do what?

  • ON/OFF inputs: any input pin โ€” i1โ€“i4 on the TinkerPy Mega, i1โ€“i2 on the TinkerPy Mini.
  • A free output pin (o1โ€“o8) can also be an ON/OFF input โ€” like the touch pad on o5 in the second example.
  • Analog (adc) inputs: only the input pins (i1โ€“i4 on Mega, i1โ€“i2 on Mini). Output pins canโ€™t read numbers.
  • In TinkerPy Coder chapter 1 you will code inputs in depth: thresholds, voltage() in volts and readings on a display.

๐Ÿ’ปCode

# Add the libraries you need
import buzzer

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

# ===== Doorbell =====

# This code block executes only once
setup:
    bell_button = set_input(push_button, active_low)
    bell = set_output(buzzer)

# This code block executes forever
loop:
    if bell_button == ON:  # when bell_button is ON
        bell = ON  # turn bell ON
    else:  # otherwise
        bell = OFF  # turn bell OFF

A doorbell: the buzzer named bell rings only while the push button is pressed. When you picked Push button on i1 (named bell_button) and Buzzer on o1 (named bell), Circuit Connections wrote the name lines, import buzzer and the two setup lines for you โ€” including the usual mode, active_low.

# Give your pins easy-to-remember names
i1 = set_name("button")
o5 = set_name("touch_pad")
o1 = set_name("lamp")

# ===== Button or touch =====

# This code block executes only once
setup:
    button = set_input(push_button, active_low)
    touch_pad = set_input(touch, active_high)
    lamp = set_output(led)

# This code block executes forever
loop:
    if button == ON or touch_pad == ON:  # when button is ON or touch_pad is ON
        lamp = ON  # turn lamp ON
    else:  # otherwise
        lamp = OFF  # turn lamp OFF

Two digital inputs in two modes: a push button on i1 (active_low) and a touch pad on output pin o5 used as an input (active_high). Pressing either one lights the lamp. The code looks the same for both โ€” the mode only tells the board how each module is wired.

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

# ===== Knob light level =====

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

# This code block executes forever
loop:
    level = analog_read(knob)  # read the knob: a number from 0 to 100
    if level > 70:  # when level > 70
        lamp = pwm(100)  # knob turned high: bright
    elif level > 30:  # otherwise, when level > 30
        lamp = pwm(40)  # knob in the middle: dim
    else:  # otherwise
        lamp = OFF  # turn lamp OFF
    wait(100)  # pause here for 100 milliseconds before continuing

An analog input: a knob (potentiometer) on i2 set to adc. analog_read(knob) gives 0 to 100, and the if / elif / else picks off, dim or bright. Try it in the Simulator by dragging the knob slider.

Open TinkerPy Code

๐ŸŽ‰Fun examples

๐Ÿ›—Lift buttons

Every button in a lift is a digital input โ€” the lift only needs to know โ€œpressedโ€ or โ€œnot pressedโ€ for each floor.

๐ŸŒ€Fan regulator

The knob on a fan regulator is not just ON or OFF โ€” it can be anywhere in between, just like an analog input.

๐Ÿ“ฑClever phone screens

Many phones have a light sensor near the camera. It measures how bright the room is and changes the screen brightness by itself.

๐ŸŒWhere itโ€™s used

๐Ÿ› ๏ธProject ideas

Doorbell button

Put a push button on i1 and a buzzer on o1 and name them in Circuit Connections. Make the buzzer ring while the button is pressed, and add an LED on o2 that lights at the same time.

๐Ÿงฐ You need: push button, buzzer, LED, TinkerPy board or Simulator
Knob-controlled light level

Put a knob (potentiometer) on i2 in adc mode and an LED on o1. Make four levels: off below 25, then pwm(30), pwm(60) and pwm(100). Test every level by dragging the slider in the Simulator.

๐Ÿงฐ You need: potentiometer, LED, TinkerPy board or Simulator

โญPoints to remember

  1. A digital input is only ON or OFF.
  2. An analog input gives a number from 0 to 100.
  3. Ask a digital input with if name == ON:
  4. Read an analog input with analog_read(name).
  5. active_low: ON when the pin is LOW (0 V).
  6. active_high: ON when the pin is HIGH.
  7. Most modules are active_low; touch and PIR are active_high.
  8. Circuit Connections marks the usual mode with a โ˜….
  9. If ON and OFF come out backwards, switch the mode.
  10. Analog (adc) works only on input pins i1โ€“i4 (i1โ€“i2 on Mini).
  11. A free output pin can be an ON/OFF input too.
  12. analog_read 0 = 0 V, 100 = 3.3 V.
  13. In the Simulator, analog inputs are sliders.
๐Ÿคฏ
Did you know?

Inside every computer, phone and TinkerPy board, all information is stored as tiny ON/OFF signals called bits โ€” even photos, songs and this very sentence!

โ“Mini quiz

๐Ÿ”‘
Log in to try the mini quiz

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

Log in