Skip to main content

A lightweight and modern Python library for controlling the LEGO WeDo 2.0 Smart Hub using Bluetooth Low Energy.

Project description

WeDo2 Python Library

A Python library for controlling the LEGO WeDo 2.0 Smart Hub using Bluetooth Low Energy (BLE).
This project uses the bleak library to communicate with motors, sensors, lights and sound functions on the WeDo 2.0 hub.


Features

  • Connect to the WeDo 2.0 hub by name
  • Control motors (run, stop, brake)
  • Read tilt sensor
  • Read distance sensor
  • Control the LED light (preset colors + RGB)
  • Play notes, frequencies and melodies
  • Read battery level
  • Detect connected devices automatically
  • Turn off or disconnect the hub

Installation

pip install wedo2-python

Basic Usage

from wedo2 import WeDo2Hub

hub = WeDo2Hub('WeDo 2.0')

print('Hub name:', hub.name())
print('Battery:', hub.get_battery_level())

hub.disconnect()

Class: WeDo2Hub

Connecting to the hub

hub = WeDo2('WeDo 2.0')

Rename the hub

hub.name('MyHubName')

Read the name of the hub

name = hub.name()
print(name) -> "MyHubName"

Disconnect the hub

hub.disconnect()

Turn off the hub

hub.shut_off()

Read battery level

level = hub.get_battery_level()
print(level)

Motor

Run the motor

hub.motor.run(50, port=1)   # Speed from -100 to 100

Stop the motor

hub.motor.stop(port=1)

Brake the motor

hub.motor.brake(port=1)

Set motor port explicitly

hub.motor.set_port(2)

Distance Sensor

Read distance (0–10 cm)

d = hub.distance_sensor.distance(port=1)
print('Distance:', d)

Read number of triggers

t = hub.distance_sensor.times(port=1)
print('Triggered:', t)

Set sensor port

hub.distance_sensor.set_port(2)

Tilt Sensor

Read tilt direction

position = hub.tilt_sensor.tilt(port=1)
print(position)

Set tilt sensor port

hub.tilt_sensor.set_port(2)

Light

Turn on a preset color

Available colors:
off, pink, purple, blue, sky blue, teal, green, yellow, orange, red, white

hub.light.on('red')

Use integer color

hub.light.on(3)   # blue

RGB color

hub.light.rgb(255, 120, 0)  # orange-ish

Turn off

hub.light.off()

Sound

Play a musical note

Format: C4, D#5, Gb3

hub.sound.note('C4', 500)

Play a beep at frequency

hub.sound.beeb(800, 300)

Play a melody

hub.sound.melody([
    ('C4', 300),
    ('E4', 300),
    ('G4', 500),
    '.',
    ('C5', 800)
])

Raw Commands (Advanced)

Raw output

hub.write_raw_output(b'\x06\x04\x01\x09')

Raw input

hub.write_raw_input(b'\x01\x02\x06\x17\x00\x01\x00\x00\x00\x02\x01')

License

MIT License


Acknowledgements


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wedo2_python-0.1.5.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wedo2_python-0.1.5-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file wedo2_python-0.1.5.tar.gz.

File metadata

  • Download URL: wedo2_python-0.1.5.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for wedo2_python-0.1.5.tar.gz
Algorithm Hash digest
SHA256 22c778a2c802de38262278e983de075564bdeca961eb6388e2739bfcb63cb2eb
MD5 8a4ae8c2fd65a119e9a44357d90e65fb
BLAKE2b-256 48e0d21e654c8983875d1b81356594d2535be922ee0f0f8edaa5b7c1392b7b63

See more details on using hashes here.

File details

Details for the file wedo2_python-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: wedo2_python-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for wedo2_python-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 dda5f4fd59d9fa12f0d79aabeb9d32840b72c79b133500882349d745b2524b4e
MD5 0414071d037b3e40f8d241faefa2d076
BLAKE2b-256 039ca7e2b34f7e83360c8360b290d6a10336c5fd2541d757a3d859dcd3bcd7fc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page