Every inventor began with one small project. Pick one and bring your idea to life today!
Great inventors and great teachers all started with one small step. Take yours today!
The same kits we use in 40+ school labs โ grab yours and build something youโre proud of.
Log in to continue
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!
An output that is only ON or OFF: pump = ON, pump = OFF.
Pulse Width Modulation: the board switches the power ON and OFF very fast. More ON-time = brighter light or faster motor.
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.
The percentage of time the power is ON. pwm(25) means ON for a quarter of the time, OFF for the rest.
Your eyes keep each picture for a tiny moment, so very fast flickers blend into one steady average.
Drag the slider. Watch the ON-time of the wave and the LED brightness change together.
Tap each card to compare a plain digital output with a PWM output.
# 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.
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.
Some laptops have a light that slowly โbreathesโ while asleep. That smooth fade is made with PWM.
Robot cars change speed with PWM: the motor gets fast little pushes, and more ON-time means a faster car.
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 SimulatorAdd 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 SimulatorCinema 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.
5 quick questions to check yourself โ free with your TinkerPy account.
Log in