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

Coding Outputs: Digital & PWM

ON and OFF are just the start. TinkerPy has a whole Output Function toolbox: make one light the opposite of another, step a fan up and down with buttons, and fade a lamp smoothly โ€” each in a single line of code.

๐Ÿ“–Key words

pwm(n)

Sets an outputโ€™s power from 0 to 100: fan = pwm(60). Numbers outside 0โ€“100 are clamped.

invert()

The opposite ON / OFF of a digital pin: green = invert(red). Works on digital pins only โ€” not pwm, servo or buzzer.

output_increment()

Raises an output once by a step, never above a limit: output_increment(fan, 35, 100, 100).

output_increment_till()

Jumps to a start value, then climbs step by step to an end value โ€” a smooth fade.

output_flip()

Flips an output between two values each time the line runs: first the ON value, then the OFF value.

๐ŸŽฌSee it work

Fade in, fade out

Press Step and watch the brightness of lamp climb and fall, one step at a time.

Why pwm(50) looks half bright

Drag the slider: the ON-time of the wave and the LED brightness change together.

๐Ÿง How it works

ON, OFF and pwm()

  • lamp = ON and lamp = OFF for anything that only needs switching.
  • lamp = pwm(30) when the level matters: brightness, fan speed, pump speed.
  • You can use a variable or a reading: lamp = pwm(level).
  • You learnt how PWM works in TinkerPy Starter chapter 8 โ€” fast ON/OFF pulses; more ON-time = more power.

invert() โ€” always the opposite

  • green = invert(red): green is ON when red is OFF, and OFF when red is ON.
  • lamp = invert(button) follows an input the other way round: ON until the button is pressed.
  • Only for digital pins (ON / OFF) โ€” not pwm, servo, buzzer or analog pins.
  • Reading an output back, like invert(red), needs board firmware 2.26.

One step at a time: increment and decrement

  • output_increment(fan, 35, 100, 100): up by 35, never above 100, the move takes 100 ms.
  • output_decrement(fan, 35, 0, 100): down by 35, never below 0.
  • Each line moves only once โ€” put it under a button to step up or down with every press.
  • Add a short wait(300) after it, or one long press will count as many steps.

Smooth fades and flips

  • output_increment_till(lamp, 0, 100, 5, 40): name, start, end, step, wait between steps.
  • output_decrement_till(lamp, 100, 0, 5, 40) fades back down. If the end is not past the start, nothing happens.
  • Steps = (end โˆ’ start) รท step. Time โ‰ˆ steps ร— wait: 20 steps ร— 40 ms = 0.8 seconds.
  • output_flip(lamp, 0, 80): the first time 80, then 0, then 80โ€ฆ โ€” a toggle in one line.

๐Ÿ’ปCode

# Give your pins easy-to-remember names
i1 = set_name("train_button")
o1 = set_name("red")
o2 = set_name("green")

# ===== Railway crossing lights =====

# This code block executes only once
setup:
    train_button = set_input(push_button, active_low)
    red = set_output(led)
    green = set_output(led)

# This code block executes forever
loop:
    if train_button == ON:  # when train_button is ON
        red = ON  # a train is coming: red ON
    else:  # otherwise
        red = OFF  # turn red OFF
    green = invert(red)  # green is always the opposite of red

Railway crossing lights. While the train button is pressed, red is ON; otherwise it is OFF. green = invert(red) is always the opposite, so exactly one light is on. (Why not just red = train_button? That copies the wire level, and a pressed active-low button reads LOW, so the light would do the opposite!) Reading the output red back needs board firmware 2.26.

# Give your pins easy-to-remember names
i1 = set_name("faster")
i2 = set_name("slower")
o1 = set_name("fan")

# ===== Three-speed fan =====

# This code block executes only once
setup:
    faster = set_input(push_button, active_low)
    slower = set_input(push_button, active_low)
    fan = set_output(fan)
    fan = OFF  # turn fan OFF

# This code block executes forever
loop:
    if faster == ON:  # when faster is ON
        output_increment(fan, 35, 100, 100)  # one step up, never above 100
        wait(300)  # pause here for 300 milliseconds before continuing
    elif slower == ON:  # otherwise, when slower is ON
        output_decrement(fan, 35, 0, 100)  # one step down, never below 0
        wait(300)  # pause here for 300 milliseconds before continuing

A fan with three speeds and two buttons. Each press of faster adds 35 (0 โ†’ 35 โ†’ 70 โ†’ 100, never above 100); slower takes 35 away (never below 0). The wait(300) stops one long press from jumping several speeds.

# Give your pins easy-to-remember names
i1 = set_name("dark")
i2 = set_name("button")
o1 = set_name("lamp")
o2 = set_name("reading_lamp")

# ===== Breathing night light =====

# This code block executes only once
setup:
    dark = set_input(light_sensor, active_low)
    button = set_input(push_button, active_low)
    lamp = set_output(led)
    reading_lamp = set_output(led)

# This code block executes forever
loop:
    if button == ON:  # when button is ON
        output_flip(reading_lamp, 0, 80)  # each press: 80, then 0, then 80 โ€ฆ
        wait(300)  # pause here for 300 milliseconds before continuing
    if dark == ON:  # when dark is ON
        output_increment_till(lamp, 0, 100, 5, 40)  # ramp lamp from 0 up to 100, 5 at a time, 40ms between steps
        output_decrement_till(lamp, 100, 0, 5, 40)  # ramp lamp down from 100 to 0, 5 at a time, 40ms between steps
    else:  # otherwise
        lamp = pwm(0)  # set lamp to 0

A breathing night light. When the light sensor (digital, ON = dark) says it is dark, lamp fades up and down; in daylight it stays off. Each press of button flips the reading_lamp between 80 and 0.

Open TinkerPy Code

๐ŸŽ‰Fun examples

๐Ÿš‚Level crossings

At a railway level crossing the red lights flash for the road while the train has the green โ€” one is always the opposite of the other, just like invert().

๐ŸŒ€Ceiling fan regulators

A fan regulator clicks through fixed speeds โ€” the same idea as output_increment() with a step and a limit.

๐Ÿ˜ดSleeping laptops

Some laptops and phones have a light that slowly breathes while asleep โ€” a fade in and a fade out, again and again.

๐ŸŒWhere itโ€™s used

๐Ÿ› ๏ธProject ideas

Breathing night light

Use a digital light sensor on i1 and an LED on o1. When it is dark the LED fades in and out with output_increment_till and output_decrement_till; try step 2 and wait 30 for a slower, calmer breath.

๐Ÿงฐ You need: TinkerPy board, light sensor module, LED
Fan with three speeds

Two buttons step a small 5 V fan up and down with output_increment and output_decrement. Add an LED that is ON whenever the fan is OFF using lamp = invert(โ€ฆ) on a digital pin.

๐Ÿงฐ You need: TinkerPy board, 2 push buttons, small fan motor with driver, LED

โญPoints to remember

  1. Use ON / OFF for switching, pwm() when the level matters.
  2. pwm() takes 0 to 100; bigger numbers are clamped.
  3. invert() gives the opposite ON / OFF of a digital pin.
  4. invert() does not work on pwm, servo, buzzer or analog pins.
  5. Reading an output with invert(o2) needs firmware 2.26.
  6. output_increment(name, step, max, time) goes up once.
  7. output_decrement(name, step, min, time) goes down once.
  8. Put a wait() after a button step so one press = one step.
  9. output_increment_till(name, start, end, step, wait) fades up.
  10. output_decrement_till fades down from start to end.
  11. Steps = (end โˆ’ start) รท step; time โ‰ˆ steps ร— wait.
  12. output_flip(name, off, on) flips โ€” the ON value comes first.
๐Ÿคฏ
Did you know?

One line, output_increment_till(lamp, 0, 100, 1, 10), makes 100 tiny brightness steps in about one second โ€” so smooth that your eyes see one gentle glow.

โ“Mini quiz

๐Ÿ”‘
Log in to try the mini quiz

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

Log in