powered by TinkerSpace.in
Join free
TinkerPy Tutorial 📘 Part 2 · Coder · Chapter 15 of 24

Ultrasonic, DHT11 & Joystick

Three special modules, seven functions: the ultrasonic sensor measures distance like a bat, the DHT11 feels temperature and humidity, and the joystick turns your thumb into numbers. Let’s code all of them!

📖Key words

distance()

The ultrasonic sensor’s reading in centimetres. Its pins are fixed (Mega: i3 TRIG, i4 ECHO), so you write no pin: d = distance().

dht_temperature()

The DHT11’s temperature in °C: t = dht_temperature(weather) for a DHT11 named weather.

dht_humidity()

How much water vapour is in the air, in %: h = dht_humidity(weather).

joystick_x() / joystick_y()

How far the stick is pushed: −100 to 100, 0 in the middle. x is left/right, y is down/up. TinkerPy Mega only.

joystick_direction()

Where the stick points as a letter: 'F' up, 'B' down, 'L', 'R', or 'S' in the middle.

🎬See it work

Echo! Echo!

Drag the wall closer or further. The sensor times the echo — and the warning LED decides.

Three modules, side by side

Tap each module to see its functions, pins and what it gives back.

🧠How it works

Ultrasonic: distance()

  • Switch on Ultrasonic in Circuit Connections → it writes import ultrasonic and the two “cannot be changed” lines.
  • Pins are fixed: i3 TRIG, i4 ECHO on the Mega; i2 TRIG, i1 ECHO on the Mini.
  • d = distance() gives centimetres — about 2 to 400 cm. It gives −1 when nothing is in range.
  • Zones with if / elif: if d > 0 and d < 10: stop! · elif d > 0 and d < 30: careful · else: all clear. The d > 0 part skips the −1 “nothing there” reading.

DHT11: temperature and humidity

  • Pick DHT11 on an input pin and name it (e.g. weather): you get import dht11 and weather = set_input(dht11).
  • dht_temperature(weather) → °C · dht_humidity(weather) → % (both read the same sensor).
  • The DHT11 is slow: read it about every 2 seconds with wait(2000).
  • Use the Mega’s i3 — or i1 if the ultrasonic already has i3 and i4. On a Mini, use i1 or i2.

Joystick: x, y, direction and press

  • Tick the Joystick library (TinkerPy Mega): it adds i1 = set_function(joystick_x), i2 … joystick_y and i3 … joystick_swt. Delete the lines you do not wire.
  • joystick_x(): −100 left … 100 right · joystick_y(): −100 down … 100 up · 0 in the middle.
  • joystick_direction() gives 'F', 'B', 'L', 'R' or 'S' — handy for robots (chapter 4).
  • joystick_pressed() is 1 while you push the stick in, otherwise 0.

💻Code

# Add the libraries you need
import ultrasonic
import buzzer

# Give your pins easy-to-remember names
o1 = set_name("warning")
o2 = set_name("beeper")

# ===== Car parking sensor =====

# This code block executes only once
setup:
    i3 = set_input(ultrasonic_trig)  # cannot be changed
    i4 = set_input(ultrasonic_echo)  # cannot be changed
    warning = set_output(led)
    beeper = set_output(buzzer)

# This code block executes forever
loop:
    d = distance()  # how far the wall is, in cm (-1 = nothing in range)
    if d > 0 and d < 10:  # when d > 0 and d < 10
        warning = ON  # turn warning ON
        beeper = ON  # turn beeper ON
    elif d > 0 and d < 30:  # otherwise, when d > 0 and d < 30
        warning = ON  # turn warning ON
        beeper = ON  # turn beeper ON
        wait(100)  # pause here for 100 milliseconds before continuing
        beeper = OFF  # turn beeper OFF
        wait(300)  # pause here for 300 milliseconds before continuing
    else:  # otherwise
        warning = OFF  # turn warning OFF
        beeper = OFF  # turn beeper OFF

A car parking sensor with three zones: closer than 10 cm the LED and beeper stay on, 10–29 cm the beeper ticks, 30 cm or more (or nothing in range, −1) all is quiet. This is the TinkerPy Mega wiring (i3, i4); on a Mini, Circuit Connections uses i2 and i1 instead.

# Add the libraries you need
import dht11

# Give your pins easy-to-remember names
i3 = set_name("weather")
o1 = set_name("fan")
o2 = set_name("humid_led")

# ===== Classroom comfort meter =====

# This code block executes only once
setup:
    weather = set_input(dht11)
    fan = set_output(fan)
    humid_led = set_output(led)

# This code block executes forever
loop:
    t = dht_temperature(weather)  # temperature in °C
    h = dht_humidity(weather)  # humidity in %
    if t > 30:  # when t > 30
        fan = ON  # turn fan ON
    else:  # otherwise
        fan = OFF  # turn fan OFF
    if h > 70:  # when h > 70
        humid_led = ON  # turn humid_led ON
    else:  # otherwise
        humid_led = OFF  # turn humid_led OFF
    wait(2000)  # a DHT11 likes a reading about every 2 seconds

A classroom comfort meter: the fan comes on above 30 °C and an LED warns when the air is very humid (above 70%). It reads the DHT11 named weather on i3 every 2 seconds (TinkerPy Mega; on a Mini, put the DHT11 on i1).

# Add the libraries you need
import joystick
import buzzer

# Give your pins easy-to-remember names
o1 = set_name("left_led")
o2 = set_name("right_led")
o4 = set_name("top_led")
o5 = set_name("beeper")

# ===== Joystick light =====

# This code block executes only once
setup:
    i1 = set_function(joystick_x)  # joystick X (left / right) — needs one of i1-i4
    i2 = set_function(joystick_y)  # joystick Y (up / down) — needs one of i1-i4
    i3 = set_function(joystick_swt)  # joystick push switch — any pin, or delete this line
    left_led = set_output(led)
    right_led = set_output(led)
    top_led = set_output(led)
    beeper = set_output(buzzer)

# This code block executes forever
loop:
    d = joystick_direction()  # F, B, L, R or S
    if d == 'L':  # when d == 'L'
        left_led = ON  # turn left_led ON
        right_led = OFF  # turn right_led OFF
    elif d == 'R':  # otherwise, when d == 'R'
        left_led = OFF  # turn left_led OFF
        right_led = ON  # turn right_led ON
    else:  # otherwise
        left_led = OFF  # turn left_led OFF
        right_led = OFF  # turn right_led OFF
    y = joystick_y()  # -100 (down) to 100 (up)
    if y > 0:  # when y > 0
        top_led = pwm(y)  # set top_led to y
    else:  # otherwise
        top_led = OFF  # turn top_led OFF
    if joystick_pressed() == 1:  # when joystick_pressed() == 1
        beeper = ON  # turn beeper ON
    else:  # otherwise
        beeper = OFF  # turn beeper OFF

A joystick-controlled light (TinkerPy Mega). Push left or right and that LED lights; push up and top_led gets brighter with joystick_y(); press the stick in and the buzzer sounds. Ticking the Joystick library wrote the three set_function lines.

Open TinkerPy Code

🎉Fun examples

🦇Bat sonar

Bats shout sounds too high for us to hear and listen for the echo to find insects in the dark — the same trick as your ultrasonic sensor.

🌧️Sticky monsoon days

On humid days the air already holds lots of water vapour, so sweat dries slowly and you feel sticky. A DHT11 can show you the number.

🎮Inside a game controller

Many game controller sticks are two knobs inside — one for left/right and one for up/down — read as numbers, just like joystick_x() and joystick_y().

🌍Where it’s used

🛠️Project ideas

Car parking sensor

Build the three-zone parking sensor on a cardboard garage wall. Change it so the beeper ticks faster as the car gets closer: use a third zone below 20 cm with a shorter wait.

🧰 You need: TinkerPy Mega, ultrasonic sensor, LED, buzzer
Joystick-controlled light

Four LEDs in a plus shape: joystick_direction() lights the LED in the direction you push, and pressing the stick in turns them all on.

🧰 You need: TinkerPy Mega, joystick module, 4 LEDs

⭐Points to remember

  1. distance() needs no pin — the ultrasonic pins are fixed.
  2. Ultrasonic pins: Mega i3 TRIG / i4 ECHO; Mini i2 TRIG / i1 ECHO.
  3. distance() gives cm, about 2–400; −1 means nothing in range.
  4. Use zones, and check d > 0 so −1 is not taken as “very close”.
  5. dht_temperature(name) gives °C.
  6. dht_humidity(name) gives % humidity.
  7. Read the DHT11 about every 2 seconds.
  8. The joystick works on TinkerPy Mega only.
  9. joystick_x(): −100 left to 100 right.
  10. joystick_y(): −100 down to 100 up.
  11. joystick_direction() gives F, B, L, R or S.
  12. joystick_pressed() is 1 while the stick is pushed in.
  13. Circuit Connections writes the import and setup lines.
🤯
Did you know?

Sound travels about 343 metres every second in air, so an echo from a wall 1 metre away comes back in about 6 thousandths of a second — and the sensor times it.

❓Mini quiz

🔑
Log in to try the mini quiz

5 quick questions to check yourself — free with your TinkerPy account.

Log in