Powered by TinkerSpace
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
Chat with TinkerPy ๐Ÿ‘‹