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
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.
A check like if button == ON: โ the input is either ON or OFF, nothing in between.
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.
Reads an input pin as a number from 0 to 100: level = analog_read(knob). The pin must be set as adc.
Reads the same pin in volts, from 0.00 to 3.30: v = voltage(knob). Needs board firmware 2.24 or newer.
The number where your decision changes, like 30 in if level < 30:.
Drag the knob reading from 0 to 100. Watch the volts and the decision the code makes.
Press Step. The loop reads the knob three times while someone turns it โ watch which branch runs each time.
# 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.
The volume knob on many speakers is a potentiometer โ exactly the knob you read with analog_read().
Many streetlights switch on by themselves at dusk. A light sensor plus one threshold is all it takes!
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.
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, buzzerUse 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, LEDYour 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.
5 quick questions to check yourself โ free with your TinkerPy account.
Log in