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

Digital and PWM Outputs

An output can be switched ON or OFF like a room light โ€” or set to any level from 0 to 100 with pwm(), like a fan regulator. The trick behind it? The board switches the power on and off so fast that your eyes canโ€™t keep up!

๐Ÿ“–Key words

Digital output

An output that is only ON or OFF: pump = ON, pump = OFF.

PWM

Pulse Width Modulation: the board switches the power ON and OFF very fast. More ON-time = brighter light or faster motor.

pwm(n)

Sets an output to a level from 0 to 100: lamp = pwm(50) is half power. pwm(100) is the same as ON, pwm(0) the same as OFF.

Duty cycle

The percentage of time the power is ON. pwm(25) means ON for a quarter of the time, OFF for the rest.

Persistence of vision

Your eyes keep each picture for a tiny moment, so very fast flickers blend into one steady average.

๐ŸŽฌSee it work

See the PWM wave

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

ON/OFF or PWM?

Tap each card to compare a plain digital output with a PWM output.

๐Ÿง How it works

Digital outputs: ON or OFF

  • Pick the output module on a pin in Circuit Connections (LED, fan, pump, relayโ€ฆ) and give it a name โ€” it writes pump = set_output(pump) for you.
  • Then switch it in loop: pump = ON โ€ฆ wait(2000) โ€ฆ pump = OFF.
  • Every output pin o1โ€“o8 can do ON / OFF โ€” on both the TinkerPy Mini and Mega.
  • Use ON / OFF whenever the level doesnโ€™t matter โ€” a lock is open or shut, a relay is on or off.

PWM: a level from 0 to 100

  • lamp = pwm(100) โ†’ full power ยท pwm(50) โ†’ half ยท pwm(10) โ†’ a tiny glow ยท OFF โ†’ off.
  • The same idea sets speed: fan = pwm(40) is a slow fan, pwm(100) a fast one.
  • You can put a variable inside: lamp = pwm(i * 5) โ€” the sunrise lamp uses a for loop to climb from 0 to 100.
  • A number bigger than 100 is treated as 100, and below 0 as 0.

How PWM fools our eyes

  • A board pin can only give full power or none. So how can a light be half bright?
  • PWM switches the power ON and OFF many times every second.
  • ON half the time and OFF half the time (pwm(50)) โ†’ your eyes see the average: half brightness.
  • Your eyes are too slow to see the flicker โ€” that is persistence of vision, the same trick that makes cartoons and films look like smooth movement.
  • A motor gets the same quick pushes and turns at an average speed because it canโ€™t start and stop that fast.

Which outputs like PWM?

  • PWM works well: LED, bulb, DC motor, fan, water pump, vibration motor.
  • ON / OFF only: relay, door lock and laser โ€” a relay or lock is either on or off.
  • Own functions: buzzer, servo and RGB LED have their own commands (chapter 10).
  • Smooth fades with output_increment_till() and more output tricks are coded in TinkerPy Coder chapter 2.

๐Ÿ’ปCode

# Give your pins easy-to-remember names
o2 = set_name("pump")

# ===== Plant watering timer =====

# This code block executes only once
setup:
    pump = set_output(pump)

# This code block executes forever
loop:
    pump = ON  # turn pump ON
    wait(2000)  # pause here for 2000 milliseconds before continuing
    pump = OFF  # turn pump OFF
    wait(10000)  # pause here for 10000 milliseconds before continuing

A digital output on a timer: the water pump named pump runs for 2 seconds, then rests for 10 seconds, again and again. The level doesnโ€™t matter here, so plain ON and OFF are perfect.

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

# ===== Bright, medium, dim =====

# This code block executes only once
setup:
    lamp = set_output(led)

# This code block executes forever
loop:
    lamp = pwm(100)  # full brightness
    wait(1000)  # pause here for 1000 milliseconds before continuing
    lamp = pwm(50)  # half as bright
    wait(1000)  # pause here for 1000 milliseconds before continuing
    lamp = pwm(10)  # just a tiny glow
    wait(1000)  # pause here for 1000 milliseconds before continuing

Bright โ†’ medium โ†’ dim, again and again. Here the brightness matters, so we use pwm() instead of ON โ€” and we wrote our own comments to say how bright each step is.

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

# ===== Two-speed fan =====

# This code block executes only once
setup:
    slow_button = set_input(push_button, active_low)
    fast_button = set_input(push_button, active_low)
    fan = set_output(fan)

# This code block executes forever
loop:
    if fast_button == ON:  # when fast_button is ON
        fan = pwm(100)  # fast
    elif slow_button == ON:  # otherwise, when slow_button is ON
        fan = pwm(40)  # slow
    else:  # otherwise
        fan = OFF  # turn fan OFF

A two-speed fan: hold the fast button for pwm(100), the slow button for pwm(40), and let go to switch the fan off. The fast button is checked first, so it wins when both are pressed.

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

# ===== Sunrise lamp =====

# This code block executes only once
setup:
    lamp = set_output(led)
    for i in range(21):  # repeat the steps below 21 times, counting each time in i
        lamp = pwm(i * 5)  # 0, 5, 10 โ€ฆ 100
        wait(300)  # pause here for 300 milliseconds before continuing

# This code block executes forever
loop:
    lamp = ON  # turn lamp ON

A sunrise lamp: in setup the for loop runs 21 times, so i goes 0, 1, 2 โ€ฆ 20 and the brightness climbs 0, 5, 10 โ€ฆ 100 โ€” about 6 seconds in all. Then loop keeps the lamp fully ON.

Open TinkerPy Code

๐ŸŽ‰Fun examples

๐ŸŽฌFilms are a trick too

A film is just many still pictures shown one after another, very fast. Your eyes blend them into movement โ€” the same reason PWM looks steady.

๐Ÿ’ปBreathing laptop light

Some laptops have a light that slowly โ€œbreathesโ€ while asleep. That smooth fade is made with PWM.

๐Ÿš—Robot car speed

Robot cars change speed with PWM: the motor gets fast little pushes, and more ON-time means a faster car.

๐ŸŒWhere itโ€™s used

๐Ÿ› ๏ธProject ideas

Sunrise lamp

Make an LED fade very slowly from 0 to 100 with a for loop (try steps of 1 and a wait of 100 ms), then stay on โ€” a gentle wake-up light for the morning.

๐Ÿงฐ You need: LED, TinkerPy board or Simulator
Two-speed fan

Add a third button to the two-speed fan for a medium speed of pwm(70). Decide which button should win when two are pressed and put that if first.

๐Ÿงฐ You need: 3 push buttons, small fan or DC motor module, TinkerPy board or Simulator

โญPoints to remember

  1. A digital output is just ON or OFF.
  2. pwm() sets any level from 0 to 100.
  3. pwm(100) is the same as ON; OFF is the same as pwm(0).
  4. PWM switches the power on and off very fast.
  5. Duty cycle = the % of time the power is ON.
  6. More ON-time = brighter light or faster motor.
  7. Our eyes see the average โ€” persistence of vision.
  8. Every output pin o1โ€“o8 can do ON / OFF and PWM.
  9. Use ON / OFF when the level doesnโ€™t matter.
  10. Use pwm() when brightness or speed matters.
  11. Relays, locks and lasers are ON / OFF only.
  12. A variable can go inside: lamp = pwm(level).
  13. Numbers above 100 count as 100.
๐Ÿคฏ
Did you know?

Cinema films show 24 still pictures every second, yet we see smooth movement โ€” our eyes and brain blend fast changes together, which is exactly why a PWM light looks steady.

โ“Mini quiz

๐Ÿ”‘
Log in to try the mini quiz

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

Log in